-
Notifications
You must be signed in to change notification settings - Fork 2
/
sign.go
65 lines (52 loc) · 1.12 KB
/
sign.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
package gtp
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io"
"os"
)
// LoadPublicKeyFile 加载公钥文件
func LoadPublicKeyFile(filePath string) (*rsa.PublicKey, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return ReadPublicKey(f)
}
// LoadPrivateKeyFile 加载私钥文件
func LoadPrivateKeyFile(filePath string) (*rsa.PrivateKey, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return ReadPrivateKey(f)
}
// ReadPublicKey 读取公钥
func ReadPublicKey(reader io.Reader) (*rsa.PublicKey, error) {
bs, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
block, _ := pem.Decode(bs)
pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, err
}
return pubKey, nil
}
// ReadPrivateKey 读取私钥
func ReadPrivateKey(reader io.Reader) (*rsa.PrivateKey, error) {
bs, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
block, _ := pem.Decode(bs)
privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privKey, nil
}