-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_test.go
114 lines (103 loc) · 2.68 KB
/
aes_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package aes
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"time"
)
type Test struct {
Key string `json:"key"`
Message string `json:"message"`
Encrypted string `json:"encrypted"`
EncryptedTTL string `json:"encryptedTTL"`
NetworkData string `json:"networkData"`
}
func TestGenerateKey(t *testing.T) {
key := GenerateKey()
if len(key) < 10 || len(key) > 128 {
t.Errorf("seems key not generate well")
}
}
func TestEncryptionDecryption(t *testing.T) {
key := GenerateKey()
aes := NewAasaamAES(key)
message := "Sample message"
encrypted := aes.EncryptTTL(message, 1)
if encrypted == "" {
t.Errorf("encryption failed")
}
sameMessage := aes.DecryptTTL(encrypted)
if sameMessage != message {
t.Errorf("decryption failed")
}
time.Sleep(time.Second * 2)
expiredMessage := aes.DecryptTTL(encrypted)
if expiredMessage != "" {
t.Errorf("decryptionttl failed")
}
}
func TestDecryptionFailed(t *testing.T) {
key1 := GenerateKey()
key2 := GenerateKey()
aes1 := NewAasaamAES(key1)
aes2 := NewAasaamAES(key2)
message := "Sample message"
encrypted1 := aes1.Encrypt(message)
decrypted2 := aes2.Decrypt(encrypted1)
decrypted3 := aes2.DecryptTTL(encrypted1)
if decrypted2 != "" {
t.Errorf("Decryption must failed")
}
if decrypted3 != "" {
t.Errorf("decryption must failed")
}
if aes2.Decrypt("") != "" {
t.Errorf("decryption empty must be empty")
}
if aes2.DecryptTTL("") != "" {
t.Errorf("decryption empty must be empty")
}
if aes2.Decrypt(" ") != "" {
t.Errorf("decryption near empty must be empty")
}
if aes2.DecryptTTL(" ") != "" {
t.Errorf("decryption near empty must be empty")
}
}
func TestEncryptionDecryptionCross(t *testing.T) {
jsonFile, _ := os.Open("./test.json")
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var test Test
err := json.Unmarshal(byteValue, &test)
if err != nil {
t.Error(err)
}
aes := NewAasaamAES(test.Key)
sameMessage := aes.Decrypt(test.Encrypted)
if sameMessage != test.Message {
t.Errorf("cross language decryption failed")
}
sameMessageTTL := aes.DecryptTTL(test.EncryptedTTL)
if sameMessageTTL != test.Message {
t.Errorf("cross language decryption failed")
}
}
func TestClientHash(t *testing.T) {
jsonFile, _ := os.Open("./test.json")
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var test Test
err := json.Unmarshal(byteValue, &test)
if err != nil {
t.Error(err)
}
clientDataSender := []string{"1.1.1.1", "user-agent"}
clientDataSenderKey := GenerateHashKey(test.Key, clientDataSender)
aes := NewAasaamAES(clientDataSenderKey)
sameMessage := aes.Decrypt(test.NetworkData)
if sameMessage != test.Message {
t.Errorf("cross language decryption failed")
}
}