-
Notifications
You must be signed in to change notification settings - Fork 22
/
cipher.go
61 lines (45 loc) · 1.2 KB
/
cipher.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
57
58
59
60
61
package pbes1
import(
"fmt"
"encoding/asn1"
"github.com/deatil/go-cryptobin/tool"
)
// 加密接口
type Cipher interface {
// oid
OID() asn1.ObjectIdentifier
// 值大小
KeySize() int
// 加密, 返回: [加密后数据, 参数, error]
Encrypt(key, plaintext []byte) ([]byte, []byte, error)
// 解密
Decrypt(key, params, ciphertext []byte) ([]byte, error)
}
var ciphers = make(map[string]func() Cipher)
// 添加加密
func AddCipher(oid asn1.ObjectIdentifier, cipher func() Cipher) {
ciphers[oid.String()] = cipher
}
// 获取加密
func GetCipher(oid string) (Cipher, error) {
cipher, ok := ciphers[oid]
if !ok {
return nil, fmt.Errorf("pkcs/cipher: unsupported cipher (OID: %s)", oid)
}
newCipher := cipher()
return newCipher, nil
}
// ===============
var newPadding = tool.NewPadding()
// 明文补码算法
func pkcs7Padding(text []byte, blockSize int) []byte {
return newPadding.PKCS7Padding(text, blockSize)
}
// 明文减码算法
func pkcs7UnPadding(src []byte) ([]byte, error) {
return newPadding.PKCS7UnPadding(src)
}
// 随机生成字符
func genRandom(num int) ([]byte, error) {
return tool.GenRandom(num)
}