-
Notifications
You must be signed in to change notification settings - Fork 0
/
des_test.go
52 lines (49 loc) · 1.25 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
package cryptoutil
import (
"crypto/des"
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDES(t *testing.T) {
randIV := func() []byte {
t := [des.BlockSize]byte{}
_, _ = rand.Read(t[:])
return t[:]
}
iv := randIV()
testFn := func(key, iv []byte) {
// bytes
data := []byte("foo")
encoded, err := DESEncrypt(data, key, iv)
assert.Nil(t, err)
decoded, err := DESDecrypt(encoded, key, iv)
assert.Nil(t, err)
assert.Equal(t, data, decoded)
// str
dataStr := "foo"
encodedStr, err := DESEncryptString(dataStr, key, iv)
assert.Nil(t, err)
decodedStr, err := DESDecryptString(encodedStr, key, iv)
assert.Nil(t, err)
assert.Equal(t, dataStr, decodedStr)
}
testFn(GenerateDESKey(), iv)
testTripleFn := func(key, iv []byte) {
// bytes
data := []byte("foo")
encoded, err := TripleDESEncrypt(data, key, iv)
assert.Nil(t, err)
decoded, err := TripleDESDecrypt(encoded, key, iv)
assert.Nil(t, err)
assert.Equal(t, data, decoded)
// str
dataStr := "foo"
encodedStr, err := TripleDESEncryptString(dataStr, key, iv)
assert.Nil(t, err)
decodedStr, err := TripleDESDecryptString(encodedStr, key, iv)
assert.Nil(t, err)
assert.Equal(t, dataStr, decodedStr)
}
testTripleFn(GenerateTripleDESKey(), iv)
}