/
nacl.go
46 lines (36 loc) · 1019 Bytes
/
nacl.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
package libpass
import (
"crypto/rand"
"golang.org/x/crypto/nacl/box"
"golang.org/x/crypto/nacl/sign"
)
func (kf *KeyFile) Sign(data []byte) (b []byte) {
return sign.Sign(nil, data, &kf.Client.SignPriv)
}
func (kf *KeyFile) Open(b []byte) (data []byte, ok bool) {
return sign.Open(nil, b, &kf.Phone.SignPub)
}
func (kf *KeyFile) Seal(data []byte) (b []byte) {
if kf.precomputed {
kf.precomputed = true
box.Precompute(&kf.sharedKey, &kf.Phone.BoxPub, &kf.Client.BoxPriv)
}
var nonce [24]byte
n, err := rand.Read(nonce[:])
if err != nil {
panic(err)
}
if n != 24 {
panic("crypto/rand did not read 24 bytes into nonce")
}
return box.SealAfterPrecomputation(nonce[:], data, &nonce, &kf.sharedKey)
}
func (kf *KeyFile) Unseal(b []byte) (data []byte, ok bool) {
if kf.precomputed {
kf.precomputed = true
box.Precompute(&kf.sharedKey, &kf.Phone.BoxPub, &kf.Client.BoxPriv)
}
var nonce [24]byte
copy(nonce[:], b)
return box.OpenAfterPrecomputation(nil, b[24:], &nonce, &kf.sharedKey)
}