-
Notifications
You must be signed in to change notification settings - Fork 3
/
crypto_test.go
169 lines (141 loc) · 4.58 KB
/
crypto_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package superdog
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"testing"
)
func TestEncrypt(t *testing.T) {
DefaultKeyProvider = &DevKeyProvider{DisableWarn: true}
val := []byte("Test Value...")
ln := len(val) 36
b, err := Encrypt("test", val, val)
if err != nil {
t.Fatal(err)
}
if len(b) != ln {
t.Fatal("Encrypted value should be proper length")
}
}
func TestDecrypt(t *testing.T) {
DefaultKeyProvider = &DevKeyProvider{DisableWarn: true}
val := []byte("Test Value")
b, err := Encrypt("test", val, val)
if err != nil {
t.Fatal(err)
}
decrypted, err := Decrypt("test", b, b)
if err != nil {
t.Fatal("Error decrypting value", err)
}
if !bytes.Equal(val, decrypted) {
t.Fatal("Expected decrypted value to match original value", string(val), string(decrypted))
}
}
func TestReencrypt(t *testing.T) {
DefaultKeyProvider = &DevKeyProvider{DisableWarn: true, KeyVersion: 2}
original := "Test Value"
val := []byte(original)
b, err := EncryptWithVersion("test", 1, val, val)
if err != nil {
t.Fatal(err)
}
b, err = Reencrypt("test", b, b)
if err != nil {
t.Fatal("Error reencrypt key.", err)
}
key, err := DefaultKeyProvider.GetKey("test", 2)
if err != nil {
t.Fatal("Error retrieving key", err)
}
decrypted, err := key.Decrypt(b, b[8:])
if err != nil {
t.Fatal("Error decrypting value", err)
}
if !bytes.Equal([]byte(original), decrypted) {
t.Fatal("Expected decrypted value to match original value", original, string(decrypted))
}
}
func TestHash(t *testing.T) {
DefaultSaltProvider = &DevSaltProvider{DisableWarn: true, SaltVersion: 2}
val := []byte("Test")
h, err := Hash("fields/test", val)
if err != nil {
t.Fatal("Error hashing value", err)
}
expected := sha256.Sum256(append([]byte("DEV SALT fields/test 2"), val...))
if !bytes.Equal([]byte(base64.StdEncoding.EncodeToString(expected[:])), h) {
t.Fatal("Value failed to hash properly")
}
}
func TestHashString(t *testing.T) {
DefaultSaltProvider = &DevSaltProvider{DisableWarn: true, SaltVersion: 3}
val := "Test"
h, err := HashString("fields/test", val)
if err != nil {
t.Fatal("Error hashing value", err)
}
expected := sha256.Sum256(append([]byte("DEV SALT fields/test 3"), []byte(val)...))
if base64.StdEncoding.EncodeToString(expected[:]) != h {
t.Fatal("Value failed to hash properly")
}
}
func TestHashWithVersion(t *testing.T) {
DefaultSaltProvider = &DevSaltProvider{DisableWarn: true, SaltVersion: 1}
val := []byte("Test")
h, err := HashWithVersion("fields/test", 2, val)
if err != nil {
t.Fatal("Error hashing value", err)
}
expected := sha256.Sum256(append([]byte("DEV SALT fields/test 2"), val...))
if !bytes.Equal([]byte(base64.StdEncoding.EncodeToString(expected[:])), h) {
t.Fatal("Value failed to hash properly")
}
}
func TestCurrentHashes(t *testing.T) {
DefaultSaltProvider = &DevSaltProvider{DisableWarn: true, SaltVersion: 1}
val := []byte("Test")
hashes, err := CurrentHashes("fields/test", val)
if err != nil {
t.Fatal("Error hashing value", err)
}
if len(hashes) != 3 {
t.Fatal("Expected 3 hashes to be returned")
}
expected := sha256.Sum256(append([]byte("DEV SALT fields/test 1"), val...))
expected2 := sha256.Sum256(append([]byte("DEV SALT fields/test 2"), val...))
expected3 := sha256.Sum256(append([]byte("DEV SALT fields/test 3"), val...))
if !bytes.Equal([]byte(base64.StdEncoding.EncodeToString(expected[:])), hashes[0]) {
t.Fatal("Value failed to hash properly")
}
if !bytes.Equal([]byte(base64.StdEncoding.EncodeToString(expected2[:])), hashes[1]) {
t.Fatal("Value failed to hash properly")
}
if !bytes.Equal([]byte(base64.StdEncoding.EncodeToString(expected3[:])), hashes[2]) {
t.Fatal("Value failed to hash properly")
}
}
func TestCurrentHashesString(t *testing.T) {
DefaultSaltProvider = &DevSaltProvider{DisableWarn: true, SaltVersion: 1}
val := "Test"
valBytes := "Test"
hashes, err := CurrentHashesString("fields/test", val)
if err != nil {
t.Fatal("Error hashing value", err)
}
if len(hashes) != 3 {
t.Fatal("Expected 3 hashes to be returned")
}
expected := sha256.Sum256(append([]byte("DEV SALT fields/test 1"), valBytes...))
expected2 := sha256.Sum256(append([]byte("DEV SALT fields/test 2"), valBytes...))
expected3 := sha256.Sum256(append([]byte("DEV SALT fields/test 3"), valBytes...))
if base64.StdEncoding.EncodeToString(expected[:]) != hashes[0] {
t.Fatal("Value failed to hash properly")
}
if base64.StdEncoding.EncodeToString(expected2[:]) != hashes[1] {
t.Fatal("Value failed to hash properly")
}
if base64.StdEncoding.EncodeToString(expected3[:]) != hashes[2] {
t.Fatal("Value failed to hash properly")
}
}