forked from dromara/dongle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha384_test.go
executable file
·112 lines (97 loc) · 3.89 KB
/
sha384_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
111
112
package dongle
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncrypt_BySha384_FromString_ToString(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
encodeMode string // 编码模式
expected string // 期望值
}{
{"", "hex", ""},
{"", "base32", ""},
{"", "base64", ""},
{"", "xxx", ""},
{"hello world", "xxx", ""},
{"hello world", "hex", "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd"},
{"hello world", "base32", "7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32==="},
{"hello world", "base64", "/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9"},
}
for index, test := range tests {
e := Encrypt.FromString(test.input).BySha384()
assert.Nil(e.Error)
assert.Equal(test.expected, e.ToString(test.encodeMode), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_BySha384_FromString_ToBytes(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
encodeMode string // 编码模式
expected []byte // 期望值
}{
{"", "hex", []byte{}},
{"", "base32", []byte{}},
{"", "base64", []byte{}},
{"", "xxx", []byte{}},
{"hello world", "xxx", []byte("")},
{"hello world", "hex", []byte("fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")},
{"hello world", "base32", []byte("7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32===")},
{"hello world", "base64", []byte("/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9")},
}
for index, test := range tests {
e := Encrypt.FromString(test.input).BySha384()
assert.Nil(e.Error)
assert.Equal(test.expected, e.ToBytes(test.encodeMode), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_BySha384_FromBytes_ToString(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input []byte // 输入值
encodeMode string // 编码模式
expected string // 期望值
}{
{[]byte(""), "hex", ""},
{[]byte(""), "base32", ""},
{[]byte(""), "base64", ""},
{[]byte("hello world"), "xxx", ""},
{[]byte("hello world"), "hex", "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd"},
{[]byte("hello world"), "base32", "7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32==="},
{[]byte("hello world"), "base64", "/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9"},
}
for index, test := range tests {
e := Encrypt.FromBytes(test.input).BySha384()
assert.Nil(e.Error)
assert.Equal(test.expected, e.ToString(test.encodeMode), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncrypt_BySha384_FromBytes_ToBytes(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input []byte // 输入值
encodeMode string // 编码模式
expected []byte // 期望值
}{
{[]byte(""), "hex", []byte{}},
{[]byte(""), "base32", []byte{}},
{[]byte(""), "base64", []byte{}},
{[]byte("hello world"), "xxx", []byte("")},
{[]byte("hello world"), "hex", []byte("fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")},
{[]byte("hello world"), "base32", []byte("7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32===")},
{[]byte("hello world"), "base64", []byte("/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9")},
}
for index, test := range tests {
e := Encrypt.FromBytes(test.input).BySha384()
assert.Nil(e.Error)
assert.Equal(test.expected, e.ToBytes(test.encodeMode), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_BySha384_FromFile(t *testing.T) {
file := "./demo.txt"
e := Encrypt.FromFile(file).BySha384()
assert.Equal(t, invalidFileError(file), e.Error, "Should catch an exception")
}