-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_test.go
46 lines (34 loc) · 1.1 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
package cryptography_test
import (
"bytes"
"testing"
"github.com/go-universal/cryptography"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSymmetric(t *testing.T) {
data := []byte("my name is John doe")
key, err := cryptography.NewSimpleKey(cryptography.Simple32)
require.NoError(t, err)
driver := cryptography.NewSymmetric(key, cryptography.SHA256)
// Encrypt
encrypted, err := driver.EncryptBase64(data)
require.NoError(t, err)
// Decrypt
raw, err := driver.DecryptBase64(encrypted)
require.NoError(t, err)
assert.True(t, bytes.Equal(raw, data), "decrypted data should match original")
}
func TestAsymmetric(t *testing.T) {
data := []byte("my name is John doe")
key, err := cryptography.NewRSAKey(cryptography.RSA2048)
require.NoError(t, err)
driver := cryptography.NewAsymmetric(*key, cryptography.SHA256)
// Encrypt
encrypted, err := driver.EncryptBase64(data)
require.NoError(t, err)
// Decrypt
raw, err := driver.DecryptBase64(encrypted)
require.NoError(t, err)
assert.True(t, bytes.Equal(raw, data), "decrypted data should match original")
}