-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
43 lines (35 loc) · 1.15 KB
/
key.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
package utils
import (
"crypto/rsa"
"fmt"
"io/ioutil"
"github.com/dgrijalva/jwt-go"
)
// ParsePrivKey allow to parse a private key file
// if the path or the content of the file is invalid return an error
// The function return a pointer of rsa.PrivateKey when valid
func ParsePrivKey(AuthKeyFile string) (*rsa.PrivateKey, error) {
keyBytes, err := ioutil.ReadFile(AuthKeyFile)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadFile failed <- %v", err)
}
key, err := jwt.ParseRSAPrivateKeyFromPEM(keyBytes)
if err != nil {
return nil, fmt.Errorf("jwt.ParseRSAPrivateKeyFromPEM failed <- %v", err)
}
return key, nil
}
// ParsePubKey allow to parse a public key file
// if the path or the content of the file is invalid return an error
// The function return a pointer of rsa.PublicKey when valid
func ParsePubKey(AuthPubFile string) (*rsa.PublicKey, error) {
pubBytes, err := ioutil.ReadFile(AuthPubFile)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadFile failed <- %v", err)
}
pub, err := jwt.ParseRSAPublicKeyFromPEM(pubBytes)
if err != nil {
return nil, fmt.Errorf("jwt.ParseRSAPublicKeyFromPEM failed <- %v", err)
}
return pub, nil
}