-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
rsa.go
105 lines (92 loc) · 2.76 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package codec
import (
"crypto/rsa"
"encoding/pem"
"errors"
"os"
"path/filepath"
"sync"
"github.com/admpub/ccs-gm/x509"
"github.com/admpub/license_gen/lib"
"github.com/webx-top/codec"
"github.com/webx-top/com"
"github.com/webx-top/echo"
)
var (
rsaDefaultKey *codec.RSA
rsaPublicKeyBytes []byte
rsaPublicKeyBase64 string
rsaBits = 2048
rsaOnce sync.Once
)
// RSAInitialize 初始化默认私钥
func RSAInitialize() {
keyFile := filepath.Join(echo.Wd(), `data`, `rsa`, `default.pem`)
if !com.FileExists(keyFile) {
if err := com.MkdirAll(filepath.Dir(keyFile), os.ModePerm); err != nil {
panic(`RSAInitialize: MkdirAll: ` + err.Error())
}
err := lib.GenerateCertificate(keyFile+`.pub`, keyFile, rsaBits)
if err != nil {
panic(`RSAInitialize: GenerateCertificate: ` + err.Error())
}
}
rsaKey, err := lib.ReadPrivateKeyFromFile(keyFile)
if err != nil {
panic(`RSAInitialize: ReadPrivateKeyFromFile(` + keyFile + `): ` + err.Error())
}
rsaPrivateKey, _ := codec.NewRSAPrivateKey(nil)
rsaPrivateKey.SetPrivateKey(rsaKey)
rsaPublicKeyBytes, err = RSAPublicKeyToBytes(&rsaKey.PublicKey)
if err != nil {
panic(`RSAInitialize: RSAPublicKeyToBytes: ` + err.Error())
}
rsaPublicKeyBase64 = string(pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: rsaPublicKeyBytes,
}))
rsaPublicKey, _ := codec.NewRSAPublicKey(nil)
rsaPublicKey.SetPublicKey(&rsaKey.PublicKey)
rsaDefaultKey = codec.NewRSA()
rsaDefaultKey.SetPrivateKey(rsaPrivateKey).SetPublicKey(rsaPublicKey)
}
// RSAKey 默认私钥
func RSADefaultKey() *codec.RSA {
rsaOnce.Do(RSAInitialize)
return rsaDefaultKey
}
// RSAEncrypt 私钥加密
func RSAEncrypt(input []byte) ([]byte, error) {
return RSADefaultKey().PublicKey().Encrypt(input)
}
// RSADecrypt 私钥解密
func RSADecrypt(input []byte) ([]byte, error) {
return RSADefaultKey().PrivateKey().Decrypt(input)
}
// RSASignMd5 使用RSAWithMD5算法签名
func RSASignMd5(data []byte) ([]byte, error) {
return RSADefaultKey().PrivateKey().SignMd5(data)
}
// RSASignSha1 使用RSAWithSHA1算法签名
func RSASignSha1(data []byte) ([]byte, error) {
return RSADefaultKey().PrivateKey().SignSha1(data)
}
// RSASignSha256 使用RSAWithSHA256算法签名
func RSASignSha256(data []byte) ([]byte, error) {
return RSADefaultKey().PrivateKey().SignSha256(data)
}
// RSAPublicKeyToBytes marshals a public key to the bytes
func RSAPublicKeyToBytes(publicKey *rsa.PublicKey) ([]byte, error) {
if publicKey == nil {
return nil, errors.New("invalid public key. It must be different from nil")
}
return x509.MarshalPKIXPublicKey(publicKey)
}
func RSADefaultPublicKeyBytes() []byte {
RSADefaultKey()
return rsaPublicKeyBytes
}
func RSADefaultPublicKeyBase64() string {
RSADefaultKey()
return rsaPublicKeyBase64
}