...
1 package crypto_test
2
3 import (
4 "bytes"
5 "math/rand"
6 "testing"
7
8 "edge-infra.dev/pkg/edge/iam/crypto"
9 )
10
11 func TestEncryption(t *testing.T) {
12 data := []byte("Hello, world!")
13 key := createEncryptionKey()
14
15 ciphertext, _ := crypto.Encrypt(data, key)
16 plaintext, _ := crypto.Decrypt(ciphertext, key)
17 if !bytes.Equal(data, plaintext) {
18 t.Errorf("input data does not match up with decrypted data")
19 }
20 }
21
22 func TestJSONEncryption(t *testing.T) {
23 data := []byte(`{"number": 2,"title": "test","url": "https://google.com"}`)
24 key := createEncryptionKey()
25
26 encryptedJSON, _ := crypto.EncryptJSON(data, key)
27 plaintext, _ := crypto.DecryptJSON(encryptedJSON, key)
28 if !bytes.Equal(data, plaintext) {
29 t.Errorf("input data does not match up with decrypted data")
30 }
31 }
32
33 func createEncryptionKey() []byte {
34 charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
35 length := 32
36 key := make([]byte, length)
37 for i := range key {
38 key[i] = charset[rand.Intn(len(charset))]
39 }
40
41 return key
42 }
43
View as plain text