-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrongbox_test.go
More file actions
145 lines (118 loc) · 4.75 KB
/
strongbox_test.go
File metadata and controls
145 lines (118 loc) · 4.75 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
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
package strongbox
import (
"bytes"
"path/filepath"
"testing"
)
func TestRSA(t *testing.T) {
s, err := New(filepath.Join("keys", "public.pem"), filepath.Join("keys", "private.pem"), "boost facile", true)
if err != nil {
t.Fatalf("new a strongbox should be successed, but got an error: %s\n", err.Error())
}
// data from https://github.com/spikex/strongbox encrypt
data := []byte(`ntQtR7MWYLfJ00udZITlP5wmnsE9tYzzlwWhXBum22DW3kjCzCHoZH7RKl0+1taKXMfsDSuylOjPSrwR3iapEStPdLnefhYafIUztnsnP43fjXMJsNm4FZitfVq4bTxBnJ8SRZnN3ATtdECfmyGjuTQviHN/kUhFKVVYuXTVaLY=`)
output, err := s.RSADecrypt(data)
if err != nil {
t.Fatalf("rsa decrypt should be successed, but got an error: %s\n", err.Error())
}
expected := []byte("Shhhh")
if !bytes.Equal(output, expected) {
t.Fatalf("rsa decrypt expected[%s], but got[%s]\n", expected, output)
}
encrypt, err := s.RSAEncrypt(expected)
if err != nil {
t.Fatalf("rsa encrypt should be successed, but got an error: %s\n", err.Error())
}
output, err = s.RSADecrypt(encrypt)
if err != nil {
t.Fatalf("rsa decrypt should be successed, but got an error: %s\n", err.Error())
}
if !bytes.Equal(output, expected) {
t.Fatalf("rsa decrypt expected[%s], but got[%s]\n", expected, output)
}
s.Base64 = false
encrypt, err = s.RSAEncrypt(expected)
if err != nil {
t.Fatalf("rsa encrypt should be successed, but got an error: %s\n", err.Error())
}
output, err = s.RSADecrypt(encrypt)
if err != nil {
t.Fatalf("rsa decrypt should be successed, but got an error: %s\n", err.Error())
}
if !bytes.Equal(output, expected) {
t.Fatalf("rsa decrypt expected[%s], but got[%s]\n", expected, output)
}
}
func TestRSANoPassword(t *testing.T) {
s, err := New(filepath.Join("keys", "np_public.pem"), filepath.Join("keys", "np_private.pem"), "", true)
if err != nil {
t.Fatalf("new a strongbox should be successed, but got an error: %s\n", err.Error())
}
data := []byte(`M71jFZPjm1Xtpeii2ZTr683PamO+YOvEKIVLJCsYLodBtww1f8C/rt/TELCFIip46FS5upzE2nhg+Jh5IALi75BenDuHGNnHnmJgieIhn/eKAR3vkqGTxZppTFPYACP9XmW/kMyNZDukaDnhjF+M8jBAkVDBQIf94i1HydVl8A4=`)
output, err := s.RSADecrypt(data)
if err != nil {
t.Fatalf("rsa decrypt should be successed, but got an error: %s\n", err.Error())
}
expected := []byte("suchuangji@gmail.com")
if !bytes.Equal(output, expected) {
t.Fatalf("rsa decrypt expected[%s], but got[%s]\n", expected, output)
}
encrypt, err := s.RSAEncrypt(expected)
if err != nil {
t.Fatalf("rsa encrypt should be successed, but got an error: %s\n", err.Error())
}
output, err = s.RSADecrypt(encrypt)
if err != nil {
t.Fatalf("rsa decrypt should be successed, but got an error: %s\n", err.Error())
}
if !bytes.Equal(output, expected) {
t.Fatalf("rsa decrypt expected[%s], but got[%s]\n", expected, output)
}
s.Base64 = false
encrypt, err = s.RSAEncrypt(expected)
if err != nil {
t.Fatalf("rsa encrypt should be successed, but got an error: %s\n", err.Error())
}
output, err = s.RSADecrypt(encrypt)
if err != nil {
t.Fatalf("rsa decrypt should be successed, but got an error: %s\n", err.Error())
}
if !bytes.Equal(output, expected) {
t.Fatalf("rsa decrypt expected[%s], but got[%s]\n", expected, output)
}
}
func TestCBC(t *testing.T) {
s, err := New(filepath.Join("keys", "public.pem"), filepath.Join("keys", "private.pem"), "boost facile", true)
if err != nil {
t.Fatalf("new a strongbox should be successed, but got an error: %s\n", err.Error())
}
// data from https://github.com/spikex/strongbox encrypt
data := []byte(`vHhGqwGZQbR2naewHsKvfQ==`)
key := []byte(`kERVX82TC4SbBrlmpbBQuIvjvwNefLMJUv9ITEUlYGniwna35nvgw+HcAwIpPH3sAVbkrOPDLTSGMOuOTfgVZZxNuEnKBUYot46jz6LNIw1QgIRn2ZHIYwJpFW0awtF9U5INgJ7xPOfTx2Q714IDgkf0IWCegSosZJRGB9JRjjY=`)
iv := []byte(`X5Eb5dBTmZ0r+ggVGQR9g/jfXuFc87KzvEYmH8UyA0w0DhIc2GEL134oabgvuUyIHZNqOJZzmQZ2hyfd8UIDFW96Z0TCPcCVfZVRUOLsAHYaxHi/JMm7wZTlBSjoc1TEAHYPEQbTj27ozhQ+X4Gk0ignpI7wVWQNfE7gB+ttUR4=`)
output, err := s.CBCDecrypt(data, key, iv)
if err != nil {
t.Fatalf("cbc decrypt should be successed, but got an error: %s\n", err.Error())
}
expected := []byte("Shhhh")
if !bytes.Equal(output, expected) {
t.Fatalf("cbc decrypt expected[%s], but got[%s]\n", expected, output)
}
encrypt, key, iv, err := s.CBCEncrypt(expected)
if err != nil {
t.Fatalf("cbc encrypt should be successed, but got an error: %s\n", err.Error())
}
output, err = s.CBCDecrypt(encrypt, key, iv)
if err != nil {
t.Fatalf("cbc decrypt should be successed, but got an error: %s\n", err.Error())
}
s.Base64 = false
encrypt, key, iv, err = s.CBCEncrypt(expected)
if err != nil {
t.Fatalf("cbc encrypt should be successed, but got an error: %s\n", err.Error())
}
output, err = s.CBCDecrypt(encrypt, key, iv)
if err != nil {
t.Fatalf("cbc decrypt should be successed, but got an error: %s\n", err.Error())
}
}