-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKCS5_test.go
110 lines (99 loc) · 2.82 KB
/
PKCS5_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package PKCS5_test
import (
"bytes"
"fmt"
"github.com/trylife/go-PKCS5"
"testing"
)
type PaddingCase struct {
Original []byte
ResultPadded []byte
BlockSize int
}
var cases = []PaddingCase{
0: {
Original: []byte("trylife"),
ResultPadded: []byte{116, 114, 121, 108, 105, 102, 101, 1},
BlockSize: 8,
},
1: {
Original: []byte("DDDDDD"),
ResultPadded: []byte{68, 68, 68, 68, 68, 68, 2, 2},
BlockSize: 8,
},
2: {
Original: []byte("DDDDDDDD"),
ResultPadded: []byte{68, 68, 68, 68, 68, 68, 68, 68, 8, 8, 8, 8, 8, 8, 8, 8},
BlockSize: 8,
},
3: {
Original: []byte("DDDDDDDDDD"),
ResultPadded: []byte{68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 6, 6, 6, 6, 6, 6},
BlockSize: 8,
},
4: {
Original: []byte("DDDDDD"),
ResultPadded: []byte{68, 68, 68, 68, 68, 68, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,},
BlockSize: 32,
},
}
func TestPaddingAndRemove(t *testing.T) {
for index, c := range cases {
res := PKCS5.RemovePadding(PKCS5.Padding(c.Original, c.BlockSize))
if !bytes.Equal(res, c.Original) {
t.Log("case: ", index)
t.Log("case.Original: ", c.Original)
t.Log("case.Original->string: ", string(c.Original))
t.Log("case.PaddingResult: ", c.ResultPadded)
t.Log("case.PaddingResult->string: ", string(c.ResultPadded))
t.Log("case.BlockSize: ", c.BlockSize)
t.Log("Result: ", res)
t.Log("Result->string: ", string(res))
t.Error("origin != RemovePadding(padding(origin))")
} else {
}
}
}
func TestPadding(t *testing.T) {
for index, c := range cases {
result := PKCS5.Padding(c.Original, c.BlockSize)
t.Log("case: ", index)
t.Log("case.Original: ", c.Original)
t.Log("case.Original->string: ", string(c.Original))
t.Log("case.PaddingResult: ", c.ResultPadded)
t.Log("case.PaddingResult->string: ", string(c.ResultPadded))
t.Log("case.BlockSize: ", c.BlockSize)
t.Log("Result: ", result)
t.Log("Result->string: ", string(result))
if !bytes.Equal(result, c.ResultPadded) {
t.Error("result != c.ResultPadding")
} else {
}
}
}
func TestRemovePadding(t *testing.T) {
for index, c := range cases {
result := PKCS5.RemovePadding(c.ResultPadded)
t.Log("case: ", index)
t.Log("case.Original: ", c.Original)
t.Log("case.Original->string: ", string(c.Original))
t.Log("case.PaddingResult: ", c.ResultPadded)
t.Log("case.PaddingResult->string: ", string(c.ResultPadded))
t.Log("case.BlockSize: ", c.BlockSize)
t.Log("Result: ", result)
t.Log("Result->string: ", string(result))
if !bytes.Equal(result, c.Original) {
t.Error("result != c.Original")
} else {
}
}
}
func Example_() {
padded := PKCS5.Padding([]byte("hi"), 8)
original := PKCS5.RemovePadding(padded)
fmt.Println(padded)
fmt.Println(original)
// Output:
// [104 105 6 6 6 6 6 6]
// [104 105]
}