-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
41 lines (34 loc) · 953 Bytes
/
load.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
package sign
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"os"
)
// LoadPrivateKeyFromFile loads a EC private key from file
func LoadPrivateKeyFromFile(filePath string) (*ecdsa.PrivateKey, error) {
pkBytes, err := os.ReadFile("./private.pem")
if err != nil {
return nil, fmt.Errorf("os read file: %w", err)
}
pemBlock, _ := pem.Decode(pkBytes)
privateKey, err := x509.ParseECPrivateKey(pemBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("x509 parse private key: %w", err)
}
return privateKey, nil
}
func LoadPrivateKey(file string) (*ecdsa.PrivateKey, error) {
decoded, err := base64.StdEncoding.DecodeString(file)
if err != nil {
return nil, fmt.Errorf("base64 url decode: %w", err)
}
pemBlock, _ := pem.Decode(decoded)
privateKey, err := x509.ParseECPrivateKey(pemBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("x509 parse private key: %w", err)
}
return privateKey, nil
}