-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeypair_test.go
145 lines (132 loc) · 3.09 KB
/
keypair_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// go-multikeypair/keypair_test.go
package multikeypair
import (
"bytes"
crypto_rand "crypto/rand"
//"fmt"
"testing"
//auth "golang.org/x/crypto/nacl/auth"
box "golang.org/x/crypto/nacl/box"
sign "golang.org/x/crypto/nacl/sign"
)
// Ensure that mapping from code to name, and name to code, are proper
// inverses.
func TestCodes(t *testing.T) {
// identity
if Names[Codes[IDENTITY]] != IDENTITY {
t.Fatal("expected name and code to match for identity")
}
// ed25519
if Names[Codes[ED_25519]] != ED_25519 {
t.Fatal("expected name and code to match for ed25519")
}
}
// Encode and decode a keypair with string key material.
func TestEncodeFixed(t *testing.T) {
private := []byte("Wn3Sf5Ke/3:PA:Tm{KCf59Wg6j%/g*#d")
public := []byte("cv-sB6?r*RW8vP5TuMSv_wvw#dV4nUP!@y%u@pmK!P-S2gYVLve!PfdC#kew5Q7U")
code := ED_25519
name := Codes[ED_25519]
mk, err := Encode(private, public, code)
if err != nil {
t.Error(err)
}
kp, err := Decode(mk)
if err != nil {
t.Error(err)
}
validate(t, kp, code, name, public[:], private[:])
}
// Encode and decode a key generated by nacl sign package.
func TestEncodeSign(t *testing.T) {
public, private, err := sign.GenerateKey(crypto_rand.Reader)
if err != nil {
t.Fatal("can't generate key")
}
code := ED_25519
name := Codes[ED_25519]
mk, err := Encode(private[:], public[:], code)
if err != nil {
t.Error(err)
}
kp, err := Decode(mk)
if err != nil {
t.Error(err)
}
validate(t, kp, code, name, public[:], private[:])
}
// Encode and decode a key generated by nacl box package.
func TestEncodeBox(t *testing.T) {
public, private, err := box.GenerateKey(crypto_rand.Reader)
if err != nil {
t.Fatal("can't generate key")
}
code := ED_25519
name := Codes[ED_25519]
mk, err := Encode(private[:], public[:], code)
if err != nil {
t.Error(err)
}
kp, err := Decode(mk)
if err != nil {
t.Error(err)
}
validate(t, kp, code, name, public[:], private[:])
}
// Check that the contents of decoded keypair match the values that were
// used to construct it.
func validate(
t *testing.T,
// A decoded keypair.
kp Keypair,
// The cipher code.
code uint64,
// The cipher name.
name string,
// Public key bytes.
public []byte,
// Private key bytes.
private []byte,
) {
if kp.Code != code {
t.Errorf("code mismatch after decoding: %d != %d", code, kp.Code)
}
if kp.Name != name {
t.Errorf("name mismatch after decoding: %s != %s", name, kp.Name)
}
if bytes.Compare(private, kp.Private) != 0 {
t.Errorf(
"private key mismatch after decoding: %s != %s",
private,
kp.Private,
)
}
if len(private) != len(kp.Private) {
t.Errorf(
"private key length mismatch: %d != %d",
len(private),
len(kp.Private),
)
}
if len(kp.Private) != kp.PrivateLength {
t.Errorf(
"private key length not accurate: %d != %d",
len(kp.Private),
kp.PrivateLength,
)
}
if bytes.Compare(public, kp.Public) != 0 {
t.Errorf(
"public key mismatch after decoding: %s != %s",
public,
kp.Public,
)
}
if len(public) != len(kp.Public) {
t.Errorf(
"public key mismatch after decoding: %d != %d",
len(public),
len(kp.Public),
)
}
}