-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
62 lines (50 loc) · 1.31 KB
/
identity.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
package dashi
import (
"jjavery/dashi/internal/sodium"
)
type Identity struct {
SecretKey []byte
PublicKey []byte
X25519SecretKey []byte
X25519PublicKey []byte
}
func NewIdentity() (*Identity, error) {
secretKey, publicKey, err := sodium.CreateSignKeypair()
if err != nil {
return nil, err
}
return NewIdentityFromKeyPair(secretKey, publicKey)
}
func NewIdentityFromSecretKeyString(secretKeyString string) (*Identity, error) {
secretKey, err := b32d(secretKeyString)
if err != nil {
return nil, err
}
publicKey, err := sodium.ConvertEd25519SecretKeyToPublicKey(secretKey)
if err != nil {
return nil, err
}
return NewIdentityFromKeyPair(secretKey, publicKey)
}
func NewIdentityFromKeyPair(secretKey []byte, publicKey []byte) (*Identity, error) {
x25519SecretKey, err := sodium.ConvertEd25519SecretKeyToX25519(secretKey)
if err != nil {
return nil, err
}
x25519PublicKey, err := sodium.ConvertEd25519PublicKeyToX25519(publicKey)
if err != nil {
return nil, err
}
return &Identity{
SecretKey: secretKey,
PublicKey: publicKey,
X25519SecretKey: x25519SecretKey,
X25519PublicKey: x25519PublicKey,
}, nil
}
func (identity *Identity) String() string {
return b32(identity.PublicKey)
}
func (identity *Identity) SecrectKeyString() string {
return b32(identity.SecretKey)
}