-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsymmetric_test.go
62 lines (58 loc) · 1.82 KB
/
symmetric_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
package crypto
import (
"fmt"
"reflect"
"testing"
)
type sm4CbcTestData struct {
key []byte
iv []byte
in []byte
out []byte
}
var testData1 = []sm4CbcTestData{
{
key: []byte{0x7b, 0xea, 0x0a, 0xa5, 0x45, 0x8e, 0xd1, 0xa3, 0x7d, 0xb1, 0x65, 0x2e, 0xfb, 0xc5, 0x95, 0x05},
iv: []byte{0x70, 0xb6, 0xe0, 0x8d, 0x46, 0xee, 0x82, 0x24, 0x45, 0x60, 0x0b, 0x25, 0xc4, 0x71, 0xfa, 0xba},
in: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
out: []byte{0xca, 0x55, 0xc5, 0x15, 0x0b, 0xf7, 0xf4, 0x6f, 0xc9, 0x89, 0x2a, 0xce, 0x49, 0x78, 0x93, 0x03},
},
{
key: []byte{0x7b, 0xea, 0x0a, 0xa5, 0x45, 0x8e, 0xd1, 0xa3, 0x7d, 0xb1, 0x65, 0x2e, 0xfb, 0xc5, 0x95, 0x05},
iv: []byte{0x70, 0xb6, 0xe0, 0x8d, 0x46, 0xee, 0x82, 0x24, 0x45, 0x60, 0x0b, 0x25, 0xc4, 0x71, 0xfa, 0xba},
in: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
out: []byte{0x95, 0xe1, 0xec, 0x3b, 0x56, 0x4a, 0x46, 0x71, 0xe7, 0xd6, 0xb1, 0x10, 0xe9, 0x09, 0x0b, 0x1b, 0xb7, 0xb5, 0x9e, 0x8d, 0x74, 0x47, 0x1e, 0x70, 0x86, 0x04, 0x6b, 0xe8, 0x78, 0x00, 0x45, 0x32},
},
}
func TestNewCipher(t *testing.T) {
CryptoType = CRYPTO_P256_SH3_AES
//CryptoType=CRYPTO_SM2_SM3_SM4
block, err := NewCipher(testData1[0].key)
if err != nil {
fmt.Println(err)
}
d0 := make([]byte, 16)
block.Encrypt(d0, testData1[0].key)
fmt.Printf("d0 = %x\n", d0)
d1 := make([]byte, 16)
block.Decrypt(d1, d0)
fmt.Printf("d1 = %x\n", d1)
if sa := testCompare(testData1[0].key, d1); sa != true {
fmt.Printf("Error data!")
}
}
func testCompare(key1, key2 []byte) bool {
if len(key1) != len(key2) {
return false
}
for i, v := range key1 {
if i == 1 {
fmt.Println("type of v", reflect.TypeOf(v))
}
a := key2[i]
if a != v {
return false
}
}
return true
}