-
Notifications
You must be signed in to change notification settings - Fork 634
Expand file tree
/
Copy pathcrypto_test.go
More file actions
109 lines (86 loc) · 2.51 KB
/
crypto_test.go
File metadata and controls
109 lines (86 loc) · 2.51 KB
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
package crypto
import (
"testing"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
)
func TestEncryptedStringPositive(t *testing.T) {
id := uuid.Must(uuid.NewV4()).String()
es, err := NewEncryptedString(id, []byte("data"), "key-id", "pwFoiPyybQMqNmYVN0gUnpbfpGQV2sDv9vp0ZAxi_Y4")
assert.NoError(t, err)
assert.Equal(t, es.KeyID, "key-id")
assert.Equal(t, es.Algorithm, "aes-gcm-hkdf")
assert.Len(t, es.Data, 20)
assert.Len(t, es.Nonce, 12)
dec := ParseEncryptedString(es.String())
assert.NotNil(t, dec)
assert.Equal(t, dec.Algorithm, "aes-gcm-hkdf")
assert.Len(t, dec.Data, 20)
assert.Len(t, dec.Nonce, 12)
decrypted, err := dec.Decrypt(id, map[string]string{
"key-id": "pwFoiPyybQMqNmYVN0gUnpbfpGQV2sDv9vp0ZAxi_Y4",
})
assert.NoError(t, err)
assert.Equal(t, []byte("data"), decrypted)
}
func TestParseEncryptedStringNegative(t *testing.T) {
negativeExamples := []string{
"not-an-encrypted-string",
// not json
"{{",
// not parsable json
`{"key_id":1}`,
`{"alg":1}`,
`{"data":"!!!"}`,
`{"nonce":"!!!"}`,
// not valid
`{}`,
`{"key_id":"key_id"}`,
`{"key_id":"key_id","alg":"different","data":"AQAB=","nonce":"AQAB="}`,
}
for _, example := range negativeExamples {
assert.Nil(t, ParseEncryptedString(example))
}
}
func TestEncryptedStringDecryptNegative(t *testing.T) {
id := uuid.Must(uuid.NewV4()).String()
// short key
_, err := NewEncryptedString(id, []byte("data"), "key-id", "short_key")
assert.Error(t, err)
// not base64
_, err = NewEncryptedString(id, []byte("data"), "key-id", "!!!")
assert.Error(t, err)
es, err := NewEncryptedString(id, []byte("data"), "key-id", "pwFoiPyybQMqNmYVN0gUnpbfpGQV2sDv9vp0ZAxi_Y4")
assert.NoError(t, err)
dec := ParseEncryptedString(es.String())
assert.NotNil(t, dec)
_, err = dec.Decrypt(id, map[string]string{
// empty map
})
assert.Error(t, err)
// short key
_, err = dec.Decrypt(id, map[string]string{
"key-id": "AQAB",
})
assert.Error(t, err)
// key not base64
_, err = dec.Decrypt(id, map[string]string{
"key-id": "!!!",
})
assert.Error(t, err)
// bad key
_, err = dec.Decrypt(id, map[string]string{
"key-id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
})
assert.Error(t, err)
// bad tag for AEAD failure
dec.Data[len(dec.Data)-1] += 1
_, err = dec.Decrypt(id, map[string]string{
"key-id": "pwFoiPyybQMqNmYVN0gUnpbfpGQV2sDv9vp0ZAxi_Y4",
})
assert.Error(t, err)
}
func TestSecureToken(t *testing.T) {
assert.Equal(t, len(SecureAlphanumeric(22)), 22)
assert.Equal(t, len(SecureAlphanumeric(7)), 8)
}