-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpadding.go
More file actions
41 lines (34 loc) · 1022 Bytes
/
padding.go
File metadata and controls
41 lines (34 loc) · 1022 Bytes
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
package codec
import "bytes"
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
func ZeroUnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
if length > unpadding {
return origData[:(length - unpadding)]
}
return []byte{}
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
if length > unpadding {
return origData[:(length - unpadding)]
}
return []byte{}
}
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
return PKCS5Padding(ciphertext, blockSize)
}
func PKCS7UnPadding(origData []byte) []byte {
return PKCS5UnPadding(origData)
}