/
ed25519.go
40 lines (31 loc) · 901 Bytes
/
ed25519.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
package crypto
import (
"crypto/ed25519"
"encoding/hex"
"github.com/iotaledger/hive.go/ierrors"
)
var (
ErrInvalidKeyLength = ierrors.New("invalid key length")
)
// ParseEd25519PublicKeyFromString parses an ed25519 public key from a string.
func ParseEd25519PublicKeyFromString(key string) (ed25519.PublicKey, error) {
keyBytes, err := hex.DecodeString(key)
if err != nil {
return nil, err
}
if len(keyBytes) != ed25519.PublicKeySize {
return nil, ErrInvalidKeyLength
}
return ed25519.PublicKey(keyBytes), nil
}
// ParseEd25519PrivateKeyFromString parses an ed25519 private key from a string.
func ParseEd25519PrivateKeyFromString(key string) (ed25519.PrivateKey, error) {
keyBytes, err := hex.DecodeString(key)
if err != nil {
return nil, err
}
if len(keyBytes) != ed25519.PrivateKeySize {
return nil, ErrInvalidKeyLength
}
return ed25519.PrivateKey(keyBytes), nil
}