forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt_test.go
91 lines (76 loc) · 2.37 KB
/
encrypt_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
package goutil
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMd5(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, md5: %s", b, Md5(b))
}
func TestSha1(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, sha1: %s", b, Sha1(b))
}
func TestSha256(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, Sha256: %s", b, Sha256(b))
}
func TestSha512(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, Sha512: %s", b, Sha512(b))
}
func TestFnv1aToUint(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, Fnv1aToUint64: %d", b, Fnv1aToUint64(b))
t.Logf("text: %s, Fnv1aToUint32: %d", b, Fnv1aToUint32(b))
}
var (
_cipherkey = []byte("1234567890abcdef")
_plaintext = []byte("text1234")
)
func TestAESEncrypt(t *testing.T) {
ciphertext := AESEncrypt(_cipherkey, _plaintext)
t.Logf("ciphertext hex: %s", ciphertext)
r, err := AESDecrypt(_cipherkey, ciphertext)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
ciphertext = AESEncrypt(_cipherkey, _plaintext, true)
t.Logf("ciphertext base64: %s", ciphertext)
r, err = AESDecrypt(_cipherkey, ciphertext, true)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
}
func TestAESCBCEncrypt(t *testing.T) {
ciphertext := AESCBCEncrypt(_cipherkey, _plaintext)
t.Logf("ciphertext hex: %s", ciphertext)
r, err := AESCBCDecrypt(_cipherkey, ciphertext)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
ciphertext = AESCBCEncrypt(_cipherkey, _plaintext, true)
t.Logf("ciphertext base64: %s", ciphertext)
r, err = AESCBCDecrypt(_cipherkey, ciphertext, true)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
}
func TestAESCTREncrypt(t *testing.T) {
ciphertext := AESCTREncrypt(_cipherkey, _plaintext)
t.Logf("ciphertext hex: %s", ciphertext)
r, err := AESCTRDecrypt(_cipherkey, ciphertext)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
ciphertext = AESCTREncrypt(_cipherkey, _plaintext, true)
t.Logf("ciphertext base64: %s", ciphertext)
r, err = AESCTRDecrypt(_cipherkey, ciphertext, true)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
}
func TestPading(t *testing.T) {
blockSize := 16
padded := hexEncode(pkcs5Padding(_plaintext, blockSize))
t.Log(string(padded))
r, err := hexDecode(padded)
assert.NoError(t, err)
unpaded, err := pkcs5Unpadding(r)
assert.NoError(t, err)
assert.Equal(t, _plaintext, unpaded)
}