-
Notifications
You must be signed in to change notification settings - Fork 48
/
des_test.go
55 lines (46 loc) · 1.39 KB
/
des_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
package openssl
import (
"encoding/base64"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDesEncrypt(t *testing.T) {
src := []byte("123456")
// DES-ECB, PKCS7_PADDING
key := []byte("12345123")
dst, err := DesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "RJK5Sd4AS44=")
}
func TestDesECBDecrypt(t *testing.T) {
src, err := base64.StdEncoding.DecodeString("RJK5Sd4AS44=")
assert.NoError(t, err)
// DES-ECB, PKCS7_PADDING
key := []byte("12345123")
dst, err := DesECBDecrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
}
func TestDesCBCEncrypt(t *testing.T) {
src := []byte("123456")
iv := []byte("67890678")
// DES-ECB, PKCS7_PADDING
key := []byte("12345123")
dst, err := DesCBCEncrypt(src, key, iv, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "fPHNaq8PdWA=")
}
func TestDesCBCDecrypt(t *testing.T) {
src, err := base64.StdEncoding.DecodeString("fPHNaq8PdWA=")
assert.NoError(t, err)
iv := []byte("67890678")
// DES-ECB, PKCS7_PADDING
key := []byte("12345123")
dst, err := DesCBCDecrypt(src, key, iv, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
}