-
Notifications
You must be signed in to change notification settings - Fork 6
/
ed25519.go
50 lines (40 loc) · 1.16 KB
/
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
41
42
43
44
45
46
47
48
49
50
package putty
import (
"crypto/ed25519"
"fmt"
)
func (k Key) readED25519PublicKey() (*ed25519.PublicKey, error) {
var pub struct {
Header string
Bytes []byte
}
err := unmarshal(k.PublicKey, &pub, false)
if err != nil {
return nil, err
}
if pub.Header != k.Algo {
return nil, fmt.Errorf("invalid header inside public key: %q: expected %q", pub.Header, k.Algo)
}
if len(pub.Bytes) != ed25519.PublicKeySize {
return nil, fmt.Errorf("public key unexpected length: %d, expected %d", len(pub.Bytes), ed25519.PublicKeySize)
}
return (*ed25519.PublicKey)(&pub.Bytes), nil
}
func (k Key) readED25519PrivateKey() (*ed25519.PrivateKey, error) {
publicKey, err := k.readED25519PublicKey()
if err != nil {
return nil, err
}
var priv []byte
err = unmarshal(k.PrivateKey, &priv, k.Encryption != "none")
if err != nil {
return nil, err
}
var privateKey ed25519.PrivateKey
privateKey = append(privateKey, priv...)
privateKey = append(privateKey, *publicKey...)
if len(privateKey) != ed25519.PrivateKeySize {
return nil, fmt.Errorf("private key unexpected length: %d, expected %d", len(privateKey), ed25519.PrivateKeySize)
}
return &privateKey, nil
}