-
Notifications
You must be signed in to change notification settings - Fork 9
/
aes-ecb-cipher_test.go
195 lines (175 loc) · 4.17 KB
/
aes-ecb-cipher_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"crypto/aes"
"crypto/md5"
"fmt"
"os"
"strings"
"testing"
)
const (
md5Origin = "51afcde9f56a132096c0496cc95eb24b"
format = "3"
id = "3135556"
mediaVersion = "5"
finAnswer = "9c2ca4649cc23e7905f09324e9fe1d24505a18b97267b56b8deefecb1d62686d2f5a0bea21e1d6dbd9c8f34c691e12dc83cac650c014d41f69d381b0ce749ff5d38c5e89c566677c9cd24555e6c2bc02"
)
func ErrChecker(t *testing.T, ErrMsg string, err error) {
if err != nil {
t.Fatalf("%s: %v", ErrMsg, err)
}
}
func OnErrorChecker(t *testing.T, err *OnError) {
if err != nil {
t.Fatalf("%s: %v", err.Message, err.Error)
}
}
func Equals(t *testing.T, myanswer, expected string) {
if myanswer != expected {
t.Errorf("Expected %s but I get %s", expected, myanswer)
}
}
func TestECBCipher(t *testing.T) {
inPart := bytes.Replace(
[]byte(
"df4797bef68491542a6963b58bac773d¤51afcde9f56a132096c0496cc95eb24b¤3¤3135556¤5¤"),
[]byte("¤"),
[]byte{164},
-1,
)
tt := []struct {
name string
key []byte
in []byte
out string
}{
{
name: "Check Cipher output",
key: []byte("jo6aey6haid2Teih"),
in: []byte("abc"),
out: "90a14c0c4d0104397e5590415b7be2db", // answer got from nodejs
},
{
name: "Check Cipher output",
key: []byte("jo6aey6haid2Teih"),
in: []byte("hellow World!"),
out: "598ad60562de8421a8223645a44e733e", // asnwer got from nodejs
},
{
name: "Check Cipher output",
key: []byte("jo6aey6haid2Teih"),
in: inPart,
out: finAnswer, // asnwer got from nodejs
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
input := Pad(tc.in) // Test the Pad function
if len(input)%aes.BlockSize != 0 {
t.Error("Input size is not multiple of block size")
}
block, err := aes.NewCipher(tc.key)
ErrChecker(t, "Key Error", err)
ciphertext := make([]byte, len(input))
mode := NewECBEncrypter(block)
mode.CryptBlocks(ciphertext, input)
Equals(t, fmt.Sprintf("%x", ciphertext), tc.out)
})
}
}
func TestMD5(t *testing.T) {
x := bytes.Replace(
[]byte("51afcde9f56a132096c0496cc95eb24b¤3¤3135556¤5"), []byte("¤"), []byte{164}, -1)
tt := []struct {
name string
in []byte
out string
}{
{
name: "Check md5 Output",
in: []byte("abc"),
out: "900150983cd24fb0d6963f7d28e17f72", // answer got from nodejs
},
{
name: "Check md5 Output",
in: []byte("hellow World!asddasdsadsada"),
out: "880480d8b39ae846cefe8a60fe80f120", // asnwer got from nodejs
},
{
name: "Check md5 Output",
in: x,
out: "df4797bef68491542a6963b58bac773d", // asnwer got from nodejs
},
{
name: "Check md5 Output",
in: []byte("3135556"),
out: "29a15fc70fb278009ab6988ce9a422e8", // asnwer got from nodejs
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
md5Sum := md5.Sum(tc.in)
out := fmt.Sprintf("%x", md5Sum)
Equals(t, out, tc.out)
})
}
}
func TestBlowFish(t *testing.T) {
tt := []struct {
name string
in string
out string
}{
{
name: "Test Blowfish output",
in: "3135556",
out: "llfk9f,7e%u`<d49", // asnwer got from nodejs
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
BFKey := GetBlowFishKey(tc.in)
Equals(t, BFKey, tc.out)
})
}
}
// func TestGetUrlDownload(t *testing.T) {
// tt := []struct {
// Name string
// Expected string
// TrackID string
// }{
// {
// Name: "1. Get the correct Download Url",
// Expected: LoadEnv("LINK1"),
// TrackID: "3135556",
// },
// {
// Name: "2. Get the correct Download Url",
// Expected: LoadEnv("LINK2"),
// TrackID: "476921142",
// },
// }
// for _, tc := range tt {
// t.Run(tc.Name, func(t *testing.T) {
// // Test the most important behavior
// cfg.UserToken = "UserToken Here"
// client, err := Login()
// OnErrorChecker(t, err)
// downloadURL, _, client, err := GetUrlDownload(tc.TrackID, client)
// OnErrorChecker(t, err)
// Equals(t, downloadURL, tc.Expected)
// })
// }
// }
// LoadEnv loads env var
func LoadEnv(str string) string {
for _, value := range os.Environ() {
pair := strings.Split(value, "=")
if pair[0] == str {
return pair[1]
}
}
return ""
}