-
Notifications
You must be signed in to change notification settings - Fork 5
/
aes_test.go
68 lines (59 loc) · 1.44 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
package codec
import (
"encoding/hex"
"fmt"
"testing"
"time"
)
func TestAES(t *testing.T) {
var (
paykey = fmt.Sprintf(`%032d`, time.Now().UnixMicro())
)
crypto := NewAES(`AES-256-ECB`)
plaintext := `admpub.com`
crypted := crypto.Encode(plaintext, paykey)
if crypto.Decode(crypted, paykey) != plaintext {
t.Fatal(`failed`)
}
//t.Fatal(crypto.Decode(crypted, paykey))
}
func TestFixedKeyDefault(t *testing.T) {
pwd := `123`
key := Md5bytes([]byte(pwd))
key2 := Md5str(pwd)
if string(key) != key2 {
t.Fatalf(`%s != %s`, key, key2)
}
t.Logf(`md5key: %s`, key)
fixedKey := FixedKeyDefault(16, key)
t.Logf(`fixedKey: %s`, hex.EncodeToString(fixedKey))
pwd = `1234`
key = Md5bytes([]byte(pwd))
t.Logf(`md5key: %s`, key)
fixedKey = FixedKeyDefault(16, key)
t.Logf(`fixedKey: %s`, hex.EncodeToString(fixedKey))
}
func TestGenAESKey(t *testing.T) {
aesKey := newAESKey(KeyAES128)
aesKey.SetKeyFixer(FixedKeyByWhitespacePrefix)
key := aesKey.GenKey([]byte(`2`))
t.Logf(`key: [%s]`, key)
if string(key) != ` 2` {
t.Fatal(`failed`)
}
key = aesKey.GenKey([]byte(`123`))
t.Logf(`key: [%s]`, key)
if string(key) != ` 123` {
t.Fatal(`failed`)
}
key = aesKey.GenKey([]byte(``))
t.Logf(`key: [%s]`, key)
if string(key) != ` ` {
t.Fatal(`failed`)
}
key = aesKey.GenKey([]byte(`12345678901234567890`))
t.Logf(`key: [%s]`, key)
if string(key) != `1234567890123456` {
t.Fatal(`failed`)
}
}