-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubkit_test.go
90 lines (74 loc) · 1.65 KB
/
pubkit_test.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package pubkit
import (
"bytes"
"testing"
)
func TestSealOpen(t *testing.T) {
// generate public/private key pairs
aPub, _ := MustGenerateKeys()
bPub, bPrv := MustGenerateKeys()
// encrypt for both 'a' and 'b' with public keys
want := []byte("hello")
secret, err := Seal(want, aPub, bPub)
if err != nil {
t.Error(err)
}
// decrypt for 'b' with private key
doc, err := Open(secret, bPrv)
if err != nil {
t.Error(err)
}
if bytes.Compare(doc, want) != 0 {
t.Errorf("got %s, want %s", doc, want)
}
}
func TestUpdate(t *testing.T) {
// generate public/private key pairs
aPub, aPrv := MustGenerateKeys()
// encrypt for 'a' with public keys
want := []byte("hello")
secret, err := Seal(want, aPub)
if err != nil {
t.Error(err)
}
// update data
want = []byte("hello-updated")
secret, err = Update(secret, aPrv, want)
// decrypt for 'a' with private key
doc, err := Open(secret, aPrv)
if err != nil {
t.Error(err)
}
if bytes.Compare(doc, want) != 0 {
t.Errorf("got %s, want %s", doc, want)
}
}
func TestAppend(t *testing.T) {
// generate public/private key pairs
aPub, aPrv := MustGenerateKeys()
bPub, bPrv := MustGenerateKeys()
// encrypt for 'a'
want := []byte("hello")
secret, err := Seal(want, aPub)
if err != nil {
t.Error(err)
}
// append 'b' pub key
modsecret, err := Append(secret, aPrv, bPub)
if err != nil {
t.Error(err)
}
// decrypt for 'b' with private key
doc, err := Open(modsecret, bPrv)
if err != nil {
t.Error(err)
}
// decrypt for 'a' with private key
doc, err = Open(modsecret, aPrv)
if err != nil {
t.Error(err)
}
if bytes.Compare(doc, want) != 0 {
t.Errorf("got %s, want %s", doc, want)
}
}