-
Notifications
You must be signed in to change notification settings - Fork 0
/
nigori_test.go
129 lines (114 loc) · 2.96 KB
/
nigori_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
package nigori
import (
"encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPermute(t *testing.T) {
tests := []struct {
name string
password string
expectedPermuted string // Base64 encoded
hasError bool
}{
{
name: "succeed",
password: "CAMSEM3y43hLmgd9Zr8e0U7YsioaIJTpcvWg+uX00KlEOAdJuLlKqGen1P0agzDUVV9fdlqK",
expectedPermuted: "ptImbFuhf1RXqsQte0TrnmJZ1ij9azjQYIrXTheZlJY/xDg9e/QCNfpE5aMj7TagFPVNpy7PeG7jlW4xExVU0Q==",
hasError: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ngr := NewNigori()
params := NewDerivationParams(WithPbkdf2HMACSHA1_1003())
err := ngr.Derivate(params, test.password)
require.NoError(t, err)
permuted, err := ngr.Permute(Password, NigoriKeyName)
if test.hasError {
assert.Error(t, err)
return
}
if assert.NoError(t, err) {
assert.Equal(t, test.expectedPermuted, permuted)
}
})
}
}
func TestEncrypt(t *testing.T) {
tests := []struct {
name string
password string
plaintext string // Base64 encoded
hasError bool
}{
{
name: "succeed",
password: "key",
plaintext: "thisistuhotnuhnoehunteoh",
hasError: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ngr := NewNigori()
params := NewDerivationParams(WithPbkdf2HMACSHA1_1003())
err := ngr.Derivate(params, test.password)
assert.NoError(t, err)
encrypted, err := ngr.Encrypt(test.plaintext)
require.NoError(t, err)
decrypted, err := ngr.Decrypt(encrypted)
require.NoError(t, err)
encoder := base64.StdEncoding
dc, err := encoder.DecodeString(decrypted)
require.NoError(t, err)
decrypted = string(dc)
if test.hasError {
require.Error(t, err)
} else if assert.NoError(t, err) {
assert.Equal(t, test.plaintext, decrypted)
}
})
}
}
func TestDecrypt(t *testing.T) {
tests := []struct {
name string
password string
ciphertext string // Base64 encoded
plaintext string // Base64 encoded
hasError bool
}{
{
name: "failed",
password: "key",
ciphertext: "",
plaintext: "hello",
hasError: true,
}, {
name: "succeed",
password: "key",
ciphertext: "vMOfpOoalnFFTYKzkacJMMFG9F9F7f9d2mNPGL5JhgXKMOkdS9STLb9FY95/D7bZPk0vYkuyonIx68YszLBjCh2qnmjmnmQJF7qRTIeO9Ec=",
plaintext: "thisistuhotnuhnoehunteoh",
hasError: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ngr := NewNigori()
params := NewDerivationParams(WithPbkdf2HMACSHA1_1003())
err := ngr.Derivate(params, test.password)
assert.NoError(t, err)
decrypted, err := ngr.Decrypt(test.ciphertext)
if test.hasError {
require.Error(t, err)
return
} else {
require.NoError(t, err)
}
encoder := base64.StdEncoding
assert.Equal(t, encoder.EncodeToString([]byte(test.plaintext)), decrypted)
})
}
}