- Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathed25519_utils.go
64 lines (52 loc) · 1.38 KB
/
ed25519_utils.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
package jwt
import (
"crypto"
"crypto/ed25519"
"crypto/x509"
"encoding/pem"
"errors"
)
var (
ErrNotEdPrivateKey=errors.New("key is not a valid Ed25519 private key")
ErrNotEdPublicKey=errors.New("key is not a valid Ed25519 public key")
)
// ParseEdPrivateKeyFromPEM parses a PEM-encoded Edwards curve private key
funcParseEdPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) {
varerrerror
// Parse PEM block
varblock*pem.Block
ifblock, _=pem.Decode(key); block==nil {
returnnil, ErrKeyMustBePEMEncoded
}
// Parse the key
varparsedKeyinterface{}
ifparsedKey, err=x509.ParsePKCS8PrivateKey(block.Bytes); err!=nil {
returnnil, err
}
varpkey ed25519.PrivateKey
varokbool
ifpkey, ok=parsedKey.(ed25519.PrivateKey); !ok {
returnnil, ErrNotEdPrivateKey
}
returnpkey, nil
}
// ParseEdPublicKeyFromPEM parses a PEM-encoded Edwards curve public key
funcParseEdPublicKeyFromPEM(key []byte) (crypto.PublicKey, error) {
varerrerror
// Parse PEM block
varblock*pem.Block
ifblock, _=pem.Decode(key); block==nil {
returnnil, ErrKeyMustBePEMEncoded
}
// Parse the key
varparsedKeyinterface{}
ifparsedKey, err=x509.ParsePKIXPublicKey(block.Bytes); err!=nil {
returnnil, err
}
varpkey ed25519.PublicKey
varokbool
ifpkey, ok=parsedKey.(ed25519.PublicKey); !ok {
returnnil, ErrNotEdPublicKey
}
returnpkey, nil
}