This repository has been archived by the owner on May 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
padding.go
56 lines (51 loc) · 1.6 KB
/
padding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2016 David Stainton
//
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package sphinxmixcrypto
import (
"bytes"
"encoding/binary"
"errors"
)
var (
// ErrInvalidBlockSize indicates block size <= 0
ErrInvalidBlockSize = errors.New("invalid block size")
// ErrInvalidData indicates zero size data
ErrInvalidData = errors.New("invalid data, empty")
// ErrInvalidPadding indicates an invalid padded input
ErrInvalidPadding = errors.New("invalid padding on input")
// ErrInvalidPadOffset indicates a bad padding offset
ErrInvalidPadOffset = errors.New("invalid padding offset")
// ErrInputTooBig indicates the input data is too big
ErrInputTooBig = errors.New("input too big")
)
// AddPadding returns src with padding appended
func AddPadding(src []byte, blockSize int) ([]byte, error) {
if blockSize <= 0 {
return nil, ErrInvalidBlockSize
}
if src == nil || len(src) == 0 {
return nil, ErrInvalidData
}
if len(src) > blockSize-2 {
return nil, ErrInputTooBig
}
offset := blockSize - len(src)
padtext := bytes.Repeat([]byte{byte(0)}, offset-2)
out := append(src, padtext...)
paddingBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(paddingBytes, uint16(offset))
out = append(out, paddingBytes...)
return out, nil
}
// RemovePadding returns src with padding removed
func RemovePadding(src []byte) ([]byte, error) {
length := uint16(len(src))
unpadding := binary.LittleEndian.Uint16(src[length-2:])
if unpadding > length {
return nil, ErrInvalidPadding
}
return src[:(length - unpadding)], nil
}