forked from apexskier/cryptoPadding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkcs7.go
More file actions
46 lines (42 loc) · 1.33 KB
/
pkcs7.go
File metadata and controls
46 lines (42 loc) · 1.33 KB
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
package cryptoPadding
import (
"bytes"
"errors"
"fmt"
)
// PKCS7 implements PKCS#7 padding, described at
// http://tools.ietf.org/html/rfc5652#section-6.3.
type PKCS7 struct{}
// Pad adds padding, with each padded byte being the total number of bytes
// added.
//
// Example for a blocksize of 8:
// -> [DD DD DD DD DD 03 03 03]
func (padding PKCS7) Pad(data []byte, blockSize int) (output []byte, err error) {
if blockSize < 1 || blockSize >= 256 {
return output, fmt.Errorf("blocksize is out of bounds: %v", blockSize)
}
var paddingBytes = padSize(len(data), blockSize)
paddingSlice := bytes.Repeat([]byte{byte(paddingBytes)}, paddingBytes)
output = append(data, paddingSlice...)
return output, nil
}
// Unpad removes padding.
func (padding PKCS7) Unpad(data []byte, blockSize int) (output []byte, err error) {
var dataLen = len(data)
if dataLen%blockSize != 0 {
return output, errors.New("data's length isn't a multiple of blockSize")
}
var paddingBytes = int(data[dataLen-1])
if paddingBytes > blockSize || paddingBytes <= 0 {
return output, fmt.Errorf("invalid padding found: %v", paddingBytes)
}
var pad = data[dataLen-paddingBytes : dataLen-1]
for _, v := range pad {
if int(v) != paddingBytes {
return output, errors.New("invalid padding found")
}
}
output = data[0 : dataLen-paddingBytes]
return output, nil
}