-
Notifications
You must be signed in to change notification settings - Fork 0
/
padding.go
54 lines (48 loc) · 1.62 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
package insecurecrypto
import (
"errors"
"fmt"
)
func pkcs7Padding(input []byte, targetLength byte) ([]byte, error) {
if int(targetLength) < len(input) {
panic("padding target length less than input length!")
}
paddingLength := targetLength - byte(len(input))
for i := byte(0); i < paddingLength; i++ {
input = append(input, byte(paddingLength))
}
return input, nil
}
// Adds PKCS7 padding.
func AddPadding(input []byte, blockSize byte) ([]byte, error) {
// We only need to pad the last block.
lastBlockLength := byte(len(input) % int(blockSize))
lastBlock := input[len(input)-int(lastBlockLength):]
paddedLastBlock, err := pkcs7Padding(lastBlock, blockSize)
if err != nil {
return nil, err
}
// The output is first N-1 blocks and the padded last block
paddedInput := append(input[0:len(input)-int(lastBlockLength)], paddedLastBlock...)
// Basic Checks
if (len(paddedInput) % int(blockSize)) != 0 {
panic("Length of padded input is not a multiple of block size.")
}
if len(paddedInput) == len(input) {
panic("Length of padded input is same as length of input.")
}
return paddedInput, nil
}
// Removes PKCS7 padding.
func RemovePadding(input []byte) ([]byte, error) {
amountOfPadding := input[len(input)-1]
if (int(amountOfPadding) > len(input)) || (int(amountOfPadding) == 0) {
return nil, errors.New("invalid padding")
}
for i := byte(1); i <= amountOfPadding; i++ {
if input[len(input)-int(i)] != amountOfPadding {
return nil, fmt.Errorf("invalid padding at byte %d. Expected %x, Got %x", len(input)-1, amountOfPadding, input[len(input)-int(i)])
}
}
return input[:len(input)-int(amountOfPadding)], nil
}