-
Notifications
You must be signed in to change notification settings - Fork 18
/
Crypto_test.go
104 lines (99 loc) · 2.36 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
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
package S
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/rand"
)
func TestStringAES(t *testing.T) {
for z := 1; z <= 33; z++ {
pass := RandomPassword(int64(z))
for y := 1; y < 64; y++ {
t.Run(fmt.Sprintf("keyLen=%d_textLen=%d", z, y), func(t *testing.T) {
plain := RandomPassword(int64(y))
crypt := EncryptAES(plain, pass)
fmt.Println(len(crypt))
decrypt := ``
ok := DecryptAES(crypt, pass, &decrypt)
assert.True(t, ok)
assert.Equal(t, plain, decrypt)
})
}
}
}
func TestStructAES(t *testing.T) {
type Foo struct {
A string
B int
C int8
D int16
E int32
F int64
G uint
H uint8
I uint16
J uint32
K uint64
L float32
M float64
N time.Time
}
for z := 1; z <= 33; z++ {
pass := RandomPassword(int64(z))
t.Run(fmt.Sprintf("keyLen=%d", z), func(t *testing.T) {
plain := Foo{
A: RandomPassword(int64(z)),
B: rand.Int(),
C: int8(rand.Int()),
D: int16(rand.Int()),
E: int32(rand.Int()),
F: rand.Int63(),
G: uint(rand.Uint32()),
H: uint8(rand.Uint32()),
I: uint16(rand.Uint32()),
J: rand.Uint32(),
K: rand.Uint64(),
L: rand.Float32(),
M: rand.Float64(),
N: time.Unix(time.Now().Unix(), 0),
// time.Now always have wall and ext that always deserialized wrongly
}
crypt := EncryptAES(plain, pass)
fmt.Println(len(crypt))
decrypt := Foo{}
ok := DecryptAES(crypt, pass, &decrypt)
assert.True(t, ok)
assert.Equal(t, plain, decrypt)
})
}
}
func TestMapAES(t *testing.T) {
for z := 1; z <= 33; z++ {
pass := RandomPassword(int64(z))
t.Run(fmt.Sprintf("keyLen=%d", z), func(t *testing.T) {
plain := map[string]any{
`A`: RandomPassword(int64(z)),
// msgpack always have integer size, so we cannot use int or uint
`C`: int8(rand.Int()),
`D`: int16(rand.Int()),
`E`: int32(rand.Int()),
`F`: rand.Int63(),
`H`: uint8(rand.Uint32()),
`I`: uint16(rand.Uint32()),
`J`: rand.Uint32(),
`K`: rand.Uint64(),
`L`: rand.Float32(),
`M`: rand.Float64(),
`N`: time.Unix(time.Now().Unix(), 0),
// time.Now always have wall and ext that always deserialized wrongly
}
crypt := EncryptAES(plain, pass)
fmt.Println(len(crypt))
decrypt := map[string]any{}
ok := DecryptAES(crypt, pass, &decrypt)
assert.True(t, ok)
assert.Equal(t, plain, decrypt)
})
}
}