-
Notifications
You must be signed in to change notification settings - Fork 1
/
padding.go
38 lines (30 loc) · 935 Bytes
/
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
package padding
import (
"bytes"
)
type Padding interface {
Padding(ciphertext []byte, blockSize int) []byte
UnPadding(origData []byte) []byte
}
type pkcs struct{}
var PKCS = pkcs{}
func (pkcs) Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func (pkcs) UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// func PKCS7Padding(src []byte, blockSize int) []byte {
// padding := blockSize - len(src)%blockSize
// padtext := bytes.Repeat([]byte{byte(padding)}, padding)
// return append(src, padtext...)
// }
// func PKCS7UnPadding(src []byte, blockSize int) ([]byte, error) {
// length := len(src)
// unpadding := int(src[length-1])
// return src[:(length - unpadding)], nil
// }