forked from vuvuzela/vuvuzela
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxkey.go
66 lines (55 loc) · 1.27 KB
/
boxkey.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
65
66
package vuvuzela
import (
"encoding/json"
"fmt"
"io"
"github.com/davidlazar/go-crypto/encoding/base32"
"golang.org/x/crypto/nacl/box"
)
type BoxKey [32]byte
func GenerateBoxKey(rand io.Reader) (publicKey, privateKey *BoxKey, err error) {
pub, priv, err := box.GenerateKey(rand)
return (*BoxKey)(pub), (*BoxKey)(priv), err
}
func (k *BoxKey) Key() *[32]byte {
return (*[32]byte)(k)
}
func (k *BoxKey) String() string {
return base32.EncodeToString(k[:])
}
func KeyFromString(s string) (*BoxKey, error) {
key := new(BoxKey)
b, err := base32.DecodeString(s)
if err != nil {
return nil, fmt.Errorf("base32 decode error: %s", err)
}
if copy(key[:], b) < 32 {
return nil, fmt.Errorf("short key")
}
return key, nil
}
func (k *BoxKey) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
}
func (k *BoxKey) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
bs, err := base32.DecodeString(s)
if err != nil {
return fmt.Errorf("base32 decode error: %s", err)
}
if copy(k[:], bs) < 32 {
return fmt.Errorf("short key")
}
return nil
}
type BoxKeys []*BoxKey
func (keys BoxKeys) Keys() []*[32]byte {
xs := make([]*[32]byte, len(keys))
for i := range keys {
xs[i] = (*[32]byte)(keys[i])
}
return xs
}