forked from Nanitor/pwhash
/
sha512_crypt.go
236 lines (209 loc) · 5.21 KB
/
sha512_crypt.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// (C) Copyright 2012, Jeramey Crawford <jeramey@antihe.ro>. All
// rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sha512_crypt implements Ulrich Drepper's SHA512-Crypt
// password hashing algorithm.
//
// The specification for this algorithm can be found here:
// http://www.akkadia.org/drepper/SHA-crypt.txt
package sha512_crypt
import (
"bytes"
"crypto/rand"
"crypto/sha512"
"fmt"
"strconv"
. "github.com/desertbit/pwhash/common"
)
const (
MagicPrefix = "$6$"
RandomSalt = ""
RoundsDefault = 5000
RoundsMax = 999999999
RoundsMin = 1000
SaltLenMax = 16
SaltLenMin = 1
)
// GenerateSalt creates a random salt parameter string with the random
// bytes being of the length provided, and the rounds parameter set as
// specified.
//
// If the length is greater than SaltLenMax, a string of that length
// will be returned instead. Similarly, if length is less than
// SaltLenMin, a string of that length will be returned instead.
//
// If rounds is equal to RoundsDefault, then the 'rounds=' part of the
// salt parameter string is elided.
func GenerateSalt(length, rounds int) string {
if length > SaltLenMax {
length = SaltLenMax
} else if length < SaltLenMin {
length = SaltLenMin
}
rlen := (length * 6 / 8)
if (length*6)%8 != 0 {
rlen += 1
}
if rounds < RoundsMin {
rounds = RoundsMin
} else if rounds > RoundsMax {
rounds = RoundsMax
}
buf := make([]byte, rlen)
rand.Read(buf)
salt := Hash64(buf)
if rounds == RoundsDefault {
return fmt.Sprintf("%s%s", MagicPrefix, salt)
}
return fmt.Sprintf("%srounds=%d$%s", MagicPrefix, rounds, salt)
}
// Crypt takes key and salt strings and performs the SHA512-crypt
// hashing algorithm on them, returning a full hash string suitable
// for storage and later password verification.
//
// If the salt string is the value RandomSalt, a randomly-generated
// salt parameter string will be generated with a length of SaltLenMax
// and RoundsDefault number of rounds.
func Crypt(keystr, saltstr string) string {
var key, salt []byte
var rounds, keyLen, saltLen int
var roundsdef bool = false
key = []byte(keystr)
keyLen = len(key)
if saltstr == "" {
saltstr = GenerateSalt(SaltLenMax, RoundsDefault)
}
saltbytes := []byte(saltstr)
if !bytes.HasPrefix(saltbytes, []byte(MagicPrefix)) {
return "invalid prefix"
}
salttoks := bytes.Split(saltbytes, []byte{'$'})
numtoks := len(salttoks)
if numtoks < 3 {
return "invalid salt format"
}
if bytes.HasPrefix(salttoks[2], []byte("rounds=")) {
roundsdef = true
pr, err := strconv.ParseInt(string(salttoks[2][7:]), 10, 32)
if err != nil {
return "invalid rounds"
}
rounds = int(pr)
if rounds < RoundsMin {
rounds = RoundsMin
} else if rounds > RoundsMax {
rounds = RoundsMax
}
salt = salttoks[3]
} else {
rounds = RoundsDefault
salt = salttoks[2]
}
if len(salt) > 16 {
salt = salt[0:16]
}
saltLen = len(salt)
B := sha512.New()
B.Write(key)
B.Write(salt)
B.Write(key)
Bsum := B.Sum(nil)
A := sha512.New()
A.Write(key)
A.Write(salt)
cnt := keyLen
for ; cnt > 64; cnt -= 64 {
A.Write(Bsum)
}
A.Write(Bsum[0:cnt])
for cnt = keyLen; cnt > 0; cnt >>= 1 {
if (cnt & 1) != 0 {
A.Write(Bsum)
} else {
A.Write(key)
}
}
Asum := A.Sum(nil)
P := sha512.New()
for cnt = 0; cnt < keyLen; cnt++ {
P.Write(key)
}
Psum := P.Sum(nil)
Pseq := make([]byte, 0, keyLen)
for cnt = keyLen; cnt > 64; cnt -= 64 {
Pseq = append(Pseq, Psum...)
}
Pseq = append(Pseq, Psum[0:cnt]...)
S := sha512.New()
for cnt = 0; cnt < (16 + int(Asum[0])); cnt++ {
S.Write(salt)
}
Ssum := S.Sum(nil)
Sseq := make([]byte, 0, saltLen)
for cnt = saltLen; cnt > 64; cnt -= 64 {
Sseq = append(Sseq, Ssum...)
}
Sseq = append(Sseq, Ssum[0:cnt]...)
Csum := Asum
for cnt = 0; cnt < rounds; cnt++ {
C := sha512.New()
if (cnt & 1) != 0 {
C.Write(Pseq)
} else {
C.Write(Csum)
}
if (cnt % 3) != 0 {
C.Write(Sseq)
}
if (cnt % 7) != 0 {
C.Write(Pseq)
}
if (cnt & 1) != 0 {
C.Write(Csum)
} else {
C.Write(Pseq)
}
Csum = C.Sum(nil)
}
buf := bytes.NewBuffer(make([]byte, 0, 123))
buf.WriteString(MagicPrefix)
if roundsdef {
buf.WriteString(fmt.Sprintf("rounds=%d$", rounds))
}
buf.Write(salt)
buf.WriteByte('$')
buf.Write(Hash64([]byte{
Csum[42], Csum[21], Csum[0],
Csum[1], Csum[43], Csum[22],
Csum[23], Csum[2], Csum[44],
Csum[45], Csum[24], Csum[3],
Csum[4], Csum[46], Csum[25],
Csum[26], Csum[5], Csum[47],
Csum[48], Csum[27], Csum[6],
Csum[7], Csum[49], Csum[28],
Csum[29], Csum[8], Csum[50],
Csum[51], Csum[30], Csum[9],
Csum[10], Csum[52], Csum[31],
Csum[32], Csum[11], Csum[53],
Csum[54], Csum[33], Csum[12],
Csum[13], Csum[55], Csum[34],
Csum[35], Csum[14], Csum[56],
Csum[57], Csum[36], Csum[15],
Csum[16], Csum[58], Csum[37],
Csum[38], Csum[17], Csum[59],
Csum[60], Csum[39], Csum[18],
Csum[19], Csum[61], Csum[40],
Csum[41], Csum[20], Csum[62],
Csum[63],
}))
return buf.String()
}
// Verify hashes a key using the same salt parameters as the given
// hash string, and if the results match, it returns true.
func Verify(key, hash string) bool {
nhash := Crypt(key, hash)
if hash == nhash {
return true
}
return false
}