-
Notifications
You must be signed in to change notification settings - Fork 68
/
rsa.go
87 lines (75 loc) · 2 KB
/
rsa.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package dongle
import (
"crypto"
"github.com/golang-module/dongle/rsa"
)
// defines rsa hash enum type.
// 定义 rsa 哈希算法枚举类型
type rsaHash crypto.Hash
// rsa hash constants
// rsa 哈希算法枚举值
const (
MD5 rsaHash = 2 + iota
SHA1
SHA224
SHA256
SHA384
SHA512
RIPEMD160 = 19
)
// ByRsa encrypts by rsa with public key or private key.
// 通过 rsa 公钥或私钥加密
func (e Encrypter) ByRsa(rsaKey interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
keyPair := rsa.NewKeyPair()
keyPair.SetPublicKey(interface2bytes(rsaKey))
keyPair.SetPrivateKey(interface2bytes(rsaKey))
if keyPair.IsPrivateKey() {
e.dst, e.Error = keyPair.EncryptByPrivateKey(e.src)
return e
}
e.dst, e.Error = keyPair.EncryptByPublicKey(e.src)
return e
}
// ByRsa decrypts by rsa with private key or public key.
// 通过 rsa 私钥或公钥解密
func (d Decrypter) ByRsa(rsaKey interface{}) Decrypter {
if len(d.src) == 0 || d.Error != nil {
return d
}
keyPair := rsa.NewKeyPair()
keyPair.SetPublicKey(interface2bytes(rsaKey))
keyPair.SetPrivateKey(interface2bytes(rsaKey))
if keyPair.IsPublicKey() {
d.dst, d.Error = keyPair.DecryptByPublicKey(d.src)
return d
}
d.dst, d.Error = keyPair.DecryptByPrivateKey(d.src)
return d
}
// ByRsa signs by rsa with private key.
// 通过 rsa 私钥签名
func (s Signer) ByRsa(privateKey interface{}, hash rsaHash) Signer {
if len(s.src) == 0 || s.Error != nil {
return s
}
keyPair := rsa.NewKeyPair()
keyPair.SetPrivateKey(interface2bytes(privateKey))
keyPair.SetHash(crypto.Hash(hash))
s.dst, s.Error = keyPair.SignByPrivateKey(s.src)
return s
}
// ByRsa verify sign by rsa with public key.
// 通过 rsa 公钥验签
func (v Verifier) ByRsa(publicKey interface{}, hash rsaHash) Verifier {
if len(v.src) == 0 || v.Error != nil {
return v
}
keyPair := rsa.NewKeyPair()
keyPair.SetPublicKey(interface2bytes(publicKey))
keyPair.SetHash(crypto.Hash(hash))
v.Error = keyPair.VerifyByPublicKey(v.src, v.sign)
return v
}