-
Notifications
You must be signed in to change notification settings - Fork 4
/
rsa.go
47 lines (38 loc) · 1.15 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
package jwt
import (
"crypto/rsa"
"errors"
"os"
"github.com/dgrijalva/jwt-go"
"github.com/smallstep/cli/crypto/pemutil"
)
var (
ErrInvalidPrivateKeyFile = errors.New("invalid private key file")
)
// LoadRSAPrivateKeyFromPEM 从PEM私钥文件 keyFile 与密码文件 passFile 中加载 RSA 私钥
func LoadRSAPrivateKeyFromPEM(keyFile, passFile string) (*rsa.PrivateKey, error) {
key, err := pemutil.Read(keyFile, pemutil.WithPasswordFile(passFile))
if err != nil {
return nil, err
}
if sk, ok := key.(*rsa.PrivateKey); ok {
return sk, nil
}
return nil, ErrInvalidPrivateKeyFile
}
// LoadRSAPrivateKey 从私钥的字节序列中加载 RSA 私钥
func LoadRSAPrivateKey(key []byte) (*rsa.PrivateKey, error) {
return jwt.ParseRSAPrivateKeyFromPEM(key)
}
// LoadRSAPublicKeyFromPEM 从PEM公钥文件 file 中加载 RSA 公钥
func LoadRSAPublicKeyFromPEM(file string) (*rsa.PublicKey, error) {
f, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return LoadRSAPublicKey(f)
}
// LoadRSAPublicKey 从字节序列中加载 RSA 公钥
func LoadRSAPublicKey(key []byte) (*rsa.PublicKey, error) {
return jwt.ParseRSAPublicKeyFromPEM(key)
}