-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt_test.go
59 lines (47 loc) · 1.07 KB
/
encrypt_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
package gocrypto_test
import (
"bytes"
"testing"
"github.com/mekramy/gocrypto"
)
func TestEncrpt(t *testing.T) {
data := []byte("my name is John doe")
t.Run("Symmetric", func(t *testing.T) {
key, err := gocrypto.NewSimpleKey(gocrypto.Simple32)
if err != nil {
t.Fatal(err)
}
driver := gocrypto.NewSymmetric(key, gocrypto.SHA256)
// Encrypt
encrypted, err := driver.EncryptBase64(data)
if err != nil {
t.Fatal(err)
}
// Decrypt
raw, err := driver.DecryptBase64(encrypted)
if err != nil {
t.Fatal(err)
} else if !bytes.Equal(raw, data) {
t.Fatal("decrypted data invalid")
}
})
t.Run("Asymmetric", func(t *testing.T) {
key, err := gocrypto.NewRSAKey(gocrypto.RSA2048)
if err != nil {
t.Fatal(err)
}
driver := gocrypto.NewAsymmetric(*key, gocrypto.SHA256)
// Encrypt
encrypted, err := driver.EncryptBase64(data)
if err != nil {
t.Fatal(err)
}
// Decrypt
raw, err := driver.DecryptBase64(encrypted)
if err != nil {
t.Fatal(err)
} else if !bytes.Equal(raw, data) {
t.Fatal("decrypted data invalid")
}
})
}