-
Notifications
You must be signed in to change notification settings - Fork 2
/
keys.go
49 lines (37 loc) · 953 Bytes
/
keys.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
package passtor
import (
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/ed25519"
)
const (
// SYMMKEYSIZE size in bytes for a symmetric key
SYMMKEYSIZE = chacha20poly1305.KeySize
)
// PublicKey type
type PublicKey = ed25519.PublicKey
// PrivateKey type
type PrivateKey = ed25519.PrivateKey
// SymmetricKey format
type SymmetricKey [SYMMKEYSIZE]byte
func generateSymmetricKey() (SymmetricKey, error) {
key, err := RandomBytes(SYMMKEYSIZE)
if err != nil {
return SymmetricKey{}, err
}
symmK := BytesToSymmetricKey(key)
return symmK, nil
}
func Generate() (PublicKey, PrivateKey, SymmetricKey, error) {
pk, sk, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, nil, SymmetricKey{}, err
}
symmKey, err := generateSymmetricKey()
if err != nil {
return nil, nil, SymmetricKey{}, err
}
return pk, sk, symmKey, nil
}
func SeedToPrivateKey(seed []byte) PrivateKey {
return ed25519.NewKeyFromSeed(seed)
}