summaryrefslogtreecommitdiff
path: root/pkg/scrypto/aes256_test.go
blob: d07221d984c1b8704c71f234112a6f2f83a0ae6c (plain)
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 scrypto

import (
	"encoding/hex"
	"slices"
	"testing"
)

func TestAES256KeyHex(t *testing.T) {
	testKeyHex := "28278b7c0a25f01d3cab639633b9487f9ea1e9a2176dc9595a3f01323aa44284"
	testKey, _ := hex.DecodeString(testKeyHex)

	var key AES256Key
	if err := key.FromHex(testKeyHex); err != nil {
		t.Errorf("got unexpected error %+v", err)
	}
	if slices.Compare(testKey, key[:]) != 0 {
		t.Errorf("got %v, wanted %v", testKey, key[:])
	}
	if testKeyHex != key.Hex() {
		t.Errorf("got %v, wanted %v", testKeyHex, key.Hex())
	}
}

func TestAES256(t *testing.T) {
	keyHex := "28278b7c0a25f01d3cab639633b9487f9ea1e9a2176dc9595a3f01323aa44284"
	var key AES256Key
	if err := key.FromHex(keyHex); err != nil {
		t.Errorf("got unexpected error %+v", err)
	}

	data := []byte("Hello world!")
	encryptedData, err := key.EncryptAES256(data)
	if err != nil {
		t.Errorf("got unexpected error when encrypting data %+v", err)
	}

	decryptedData, err := key.DecryptAES256(encryptedData)
	if err != nil {
		t.Errorf("got unexpected error when decrypting data %+v", err)
	}

	if slices.Compare(data, decryptedData) != 0 {
		t.Errorf("got %v, wanted %v", decryptedData, data)
	}
}

func TestAES256InvalidData(t *testing.T) {
	keyHex := "28278b7c0a25f01d3cab639633b9487f9ea1e9a2176dc9595a3f01323aa44284"
	var key AES256Key
	if err := key.FromHex(keyHex); err != nil {
		t.Errorf("got unexpected error when converting data from base64: %+v", err)
	}

	iv := make([]byte, AES256IVSize)
	if _, err := key.DecryptAES256(append(iv, []byte("foo")...)); err == nil {
		t.Error("decrypting operation should have failed")
	}
}