File: helpers_hmac_test.go

package info (click to toggle)
golang-github-bougou-go-ipmi 0.7.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,880 kB
  • sloc: makefile: 38
file content (125 lines) | stat: -rw-r--r-- 3,064 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ipmi

import (
	"fmt"
	"testing"
)

func Test_encryptAES(t *testing.T) {
	cases := []struct {
		name            string
		ipmiRequestBody []byte
		iv              []byte
		key             []byte
		expected        []byte
	}{
		{
			name: "test1",
			ipmiRequestBody: []byte{
				0x20, 0x18, 0xc8, 0x81, 0x04, 0x3b, 0x04, 0x3c, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x07,
			},
			iv: []byte{
				0x00, 0xdf, 0x5e, 0x2a, 0x2b, 0x37, 0x2e, 0x80, 0x7a, 0xe5, 0x5b, 0x0c, 0x37, 0x3c, 0x37, 0x69,
			},
			key: []byte{
				0x12, 0x0e, 0x6b, 0x20, 0xe1, 0xe5, 0x2d, 0x13, 0xa0, 0x4a, 0x2b, 0xb8, 0x3d, 0x0d, 0x38, 0xa1,
			},
			expected: []byte{
				0x47, 0x9c, 0x2f, 0x65, 0xfb, 0x59, 0x75, 0x19, 0x71, 0xa2, 0x96, 0xa3, 0x77, 0x15, 0x55, 0x69,
			},
		},
		{
			name: "test2",
			ipmiRequestBody: []byte{
				0x20, 0x18, 0xc8, 0x81, 0x04, 0x3b, 0x04, 0x3c, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x07,
			},
			iv: []byte{
				0x00, 0xdf, 0x5e, 0x2a, 0x2b, 0x37, 0x2e, 0x80, 0x7a, 0xe5, 0x5b, 0x0c, 0x37, 0x3c, 0x37, 0x69,
			},
			key: []byte{
				0x12, 0x0e, 0x6b, 0x20, 0xe1, 0xe5, 0x2d, 0x13, 0xa0, 0x4a, 0x2b, 0xb8, 0x3d, 0x0d, 0x38, 0xa1,
			},
			expected: []byte{
				0x47, 0x9c, 0x2f, 0x65, 0xfb, 0x59, 0x75, 0x19, 0x71, 0xa2, 0x96, 0xa3, 0x77, 0x15, 0x55, 0x69,
			},
		},
	}

	for _, v := range cases {
		got, err := encryptAES(v.ipmiRequestBody, v.key, v.iv)
		if err != nil {
			t.Errorf("encryptAES failed, err: %s", err)
		}
		if !isByteSliceEqual(got, v.expected) {
			t.Errorf("got does not match expected, got: %v, want: %v", got, v.expected)
		}
	}
}

func Test_AES_Decrypt(t *testing.T) {
	ivStr := "1234567890123456"
	cipherKey := "12345678901234567890123456789012"

	plainText := "abcdefghijklmnopqrstuvwxyzABCDEF"
	cipherText, err := encryptAES([]byte(plainText), []byte(cipherKey), []byte(ivStr))
	if err != nil {
		t.Error(err)
	}

	p, err := decryptAES([]byte(cipherText), []byte(cipherKey), []byte(ivStr))
	if err != nil {
		t.Error(err)
	}

	if len(plainText) != len(p) {
		t.Error("Not equal")
	}

	for k, v := range []byte(plainText) {
		if v != p[k] {
			t.Errorf("not equal at %d", k)
		}
	}
}

func Test_RC4_Encrypt(t *testing.T) {
	ivStr := "1234567890123456"
	cipherKey := "12345678901234567890123456789012"

	plainText := "abcdefghijklmnopqrstuvwxyzABCDEF"
	cipherText, err := encryptRC4([]byte(plainText), []byte(cipherKey), []byte(ivStr))
	if err != nil {
		t.Error(err)
	}

	fmt.Println(string(cipherText))
	fmt.Println(cipherText)
}

func Test_RC4_Decrypt(t *testing.T) {
	ivStr := "1234567890123456"
	cipherKey := "12345678901234567890123456789012"

	plainText := "abcdefghijklmnopqrstuvwxyzABCDEF"
	cipherText, err := encryptRC4([]byte(plainText), []byte(cipherKey), []byte(ivStr))
	if err != nil {
		t.Error(err)
	}

	p, err := decryptRC4([]byte(cipherText), []byte(cipherKey), []byte(ivStr))
	if err != nil {
		t.Error(err)
	}

	fmt.Println(string(p))

	if len(plainText) != len(p) {
		t.Error("Not equal")
	}

	for k, v := range []byte(plainText) {
		if v != p[k] {
			t.Errorf("not equal at %d", k)
		}
	}
}