-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_test.go
180 lines (153 loc) · 5.11 KB
/
hex_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package marshaller
import (
"testing"
"github.com/go-passwd/hasher"
"github.com/stretchr/testify/assert"
)
var (
iter = 10
salt = "salt"
)
const (
password = "password"
passwordPlain = "plain$0$$70617373776f7264"
passwordMD5 = "md5$10$salt$1446572e0a1e0d275b6d1ec51d61d5b1"
passwordSHA1 = "sha1$10$salt$0c567ff217a99b516d5634b2cb92282b21c47831"
passwordSHA224 = "sha224$10$salt$dbe00a849ca8e1dce3a43746805241d291bb04aa52668ac0ab6ccf00"
passwordSHA256 = "sha256$10$salt$f71b3ce092de12327025cd57fa3cf74c6a6eecc2cb8cb7514dd2fc57698e734e"
passwordSHA384 = "sha384$10$salt$4a36c382e1bfc7c7decada7ff17754f1fd51350d3f73a3a109ae7f5220bff4ca81d8a9ad70e6174b18b1751ea01425a0"
passwordSHA512 = "sha512$10$salt$39ab7c979fea0c9ff5bb58b0d6009a19594892910ec2bcbc37eacb803f60bb31546b7ff8f8cb3364b08ddedbbb7ef08aa46988e1656432f0a3d25e8cd3a5ad58"
passwordSHA512_224 = "sha512_224$10$salt$9295ecc8e4e80586a8a4fd67c4259d77edcaee16b4b83426ec79658d"
passwordSHA512_256 = "sha512_256$10$salt$28ffd44b7cd5649e707a7290c3e84c2db985e529d1abf86c74185bb259aa2151"
)
var m = HexMarshaller{
Separator: "$",
}
func TestHexMarshaller_Marshal_plain(t *testing.T) {
h := hasher.PlainHasher{}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordPlain, s)
}
func TestHexMarshaller_Unmarshal_plain(t *testing.T) {
h, err := m.Unmarshal(passwordPlain)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_md5(t *testing.T) {
h := hasher.MD5Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordMD5, s)
}
func TestHexMarshaller_Unmarshal_md5(t *testing.T) {
h, err := m.Unmarshal(passwordMD5)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_sha1(t *testing.T) {
h := hasher.SHA1Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordSHA1, s)
}
func TestHexMarshaller_Unmarshal_sha1(t *testing.T) {
h, err := m.Unmarshal(passwordSHA1)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_sha224(t *testing.T) {
h := hasher.SHA224Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordSHA224, s)
}
func TestHexMarshaller_Unmarshal_sha224(t *testing.T) {
h, err := m.Unmarshal(passwordSHA224)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_sha256(t *testing.T) {
h := hasher.SHA256Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordSHA256, s)
}
func TestHexMarshaller_Unmarshal_sha256(t *testing.T) {
h, err := m.Unmarshal(passwordSHA256)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_sha384(t *testing.T) {
h := hasher.SHA384Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordSHA384, s)
}
func TestHexMarshaller_Unmarshal_sha384(t *testing.T) {
h, err := m.Unmarshal(passwordSHA384)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_sha512(t *testing.T) {
h := hasher.SHA512Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordSHA512, s)
}
func TestHexMarshaller_Unmarshal_sha512(t *testing.T) {
h, err := m.Unmarshal(passwordSHA512)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_sha512_224(t *testing.T) {
h := hasher.SHA512_224Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordSHA512_224, s)
}
func TestHexMarshaller_Unmarshal_sha512_224(t *testing.T) {
h, err := m.Unmarshal(passwordSHA512_224)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Marshal_sha512_256(t *testing.T) {
h := hasher.SHA512_256Hasher{Iter: &iter, Salt: &salt}
h.SetPassword(password)
s, err := m.Marshal(&h)
assert.Nil(t, err)
assert.Equal(t, passwordSHA512_256, s)
}
func TestHexMarshaller_Unmarshal_sha512_256(t *testing.T) {
h, err := m.Unmarshal(passwordSHA512_256)
assert.Nil(t, err)
assert.True(t, h.Check(password))
}
func TestHexMarshaller_Unmarshal_error(t *testing.T) {
m := HexMarshaller{
Separator: ":",
}
h, err := m.Unmarshal(passwordSHA512)
assert.NotNil(t, err)
assert.Nil(t, h)
m = HexMarshaller{
Separator: "$",
}
h, err = m.Unmarshal("sha512$10$salt$q")
assert.NotNil(t, err)
assert.Nil(t, h)
h, err = m.Unmarshal("sha512$99999999999999999999999999999999999999999999999999999999999999999$salt$39ab7c979fea0c9ff5bb58b0d6009a19594892910ec2bcbc37eacb803f60bb31546b7ff8f8cb3364b08ddedbbb7ef08aa46988e1656432f0a3d25e8cd3a5ad58")
assert.NotNil(t, err)
assert.Nil(t, h)
h, err = m.Unmarshal("sha5$20$salt$39ab7c979fea0c9ff5bb58b0d6009a19594892910ec2bcbc37eacb803f60bb31546b7ff8f8cb3364b08ddedbbb7ef08aa46988e1656432f0a3d25e8cd3a5ad58")
assert.NotNil(t, err)
assert.Nil(t, h)
}