-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto_test.go
62 lines (53 loc) · 1.46 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package crypto_utils
import (
"encoding/base64"
"fmt"
"github.com/BRUHItsABunny/crypto-utils/padding"
"math/rand"
"testing"
"time"
)
func TestAesECBCrypto(t *testing.T) {
// We want to be able to encrypt and get expected data and we want to decrypt and get expected data
key := make([]byte, 16)
rand.Seed(time.Now().UnixNano())
_, err := rand.Read(key)
if err != nil {
t.Error(err)
}
data := []byte("TestData")
encData, err := AesECBEncrypt(padding.SupportedPaddings["pkcs7"], data, key)
if err != nil {
t.Error(err)
}
fmt.Println(base64.StdEncoding.EncodeToString(encData))
data2, err := AesECBDecrypt(padding.SupportedPaddings["pkcs7"], encData, key)
if err != nil {
t.Error(err)
}
fmt.Println(base64.StdEncoding.EncodeToString(data))
fmt.Println(base64.StdEncoding.EncodeToString(data2))
}
func TestAesCBCDecrypt(t *testing.T) {
key, err := base64.StdEncoding.DecodeString("D3W+fwkwLpPjePQp+VPrRbQZsuQXlwrxMIJtmIssGig=")
if err != nil {
t.Error(err)
}
IV, err := base64.StdEncoding.DecodeString("raRw6/OKlPDgHRMwZQ2f9w==")
if err != nil {
t.Error(err)
}
data, err := base64.StdEncoding.DecodeString("S5hGFVN6PbGw+pxY7SZ7ko06Nc5ksSs4EKdcDQW3VgI=")
if err != nil {
t.Error(err)
}
expected := "{\"text\":\"this is v1\"}"
result, err := AesCBCDecrypt(padding.SupportedPaddings["pkcs7"], data, key, IV)
if err != nil {
t.Error(err)
}
if string(result) != expected {
fmt.Println(string(result))
t.Error("result != expected")
}
}