/
gophpass.go
217 lines (168 loc) · 4.88 KB
/
gophpass.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
// Package gophpass is implementing Drupal 8 phpass algorithm in go
// This package rovides the ability to create and validate by phpass hashed
// passwords.
// For more details: http://www.openwall.com/phpass/
// This code is a port of the Drupal 8 implimentation of phpass:
// https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Password%21PhpassHashedPassword.php/8.2.x
//
// The code is not 100% complete.
package gophpass
// import bcrypt
import (
"fmt"
"bytes"
"crypto/rand"
"crypto/sha512"
"crypto/md5"
"errors"
"io"
)
// constants
const (
MinHashCount = 7
MaxHashCount = 30
DefaultHashCount = 15
HashLength = 55
SaltLength = 6
ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
)
// hashed struct
type hashed struct {
hash []byte
salt []byte
count uint // allowed range is MinCount to MaxCount
}
// InvalidCountError errors
var InvalidCountError = errors.New("Invalid Count");
var InvalidSaltError = errors.New("Invalid Salt");
var countLog2 uint = 16
// HashedPassword is wrapper for hashPassword
func HashedPassword(password string, cLog2 uint) (string, error) {
countLog2 = enforceLog2Boundaries(cLog2)
p, err := hashPassword([]byte(password))
if err != nil {
return "", err
}
return string(p.hash), nil
}
func enforceLog2Boundaries(cLog2 uint) (uint) {
if cLog2 < MinHashCount {
return MinHashCount
}
if cLog2 > MaxHashCount {
return MaxHashCount
}
return cLog2
}
// internal function to hash password
func hashPassword(password []byte) (*hashed, error) {
if countLog2 < MinHashCount {
countLog2 = DefaultHashCount
}
p := new(hashed)
p.count = countLog2
newSalt, err := generateSalt(countLog2)
if err != nil {
return nil, err
}
p.salt = newSalt
hash, err := encrypt(password, p.salt)
if err != nil {
return nil, err
}
p.hash = hash
return p, err
}
// generate salt
func generateSalt(hashCount uint) ([]byte, error) {
// new buffer
rs := bytes.NewBuffer(make([]byte, 0, 61))
// append $S$
rs.WriteString("$S$")
// parse const
constBytes := []byte(ITOA64)
rs.WriteByte(constBytes[hashCount])
unencodedSalt := make([]byte, SaltLength)
_, err := io.ReadFull(rand.Reader, unencodedSalt)
if err != nil {
return nil, err
}
encodedSalt := base64Encode(unencodedSalt)
fmt.Printf("generateSalt :: encodedSalt: %s\n", encodedSalt)
_, err = rs.Write(encodedSalt)
if err != nil {
return nil, err
}
fmt.Printf("generateSalt RETURN: %s\n", rs)
return rs.Bytes(), nil
}
func validateSalt(salt []byte) bool {
firstChar := string(salt[0:1])
thirdChar := string(salt[2:3])
if firstChar != "$" || thirdChar != "$" {
return false
}
return true
}
func getCountLog2(setting []byte) (uint) {
roundsChar := setting[3:4] // a carachter saved in hash after $S$... The position of this char in alphabet is log2 of rounds number
ITOA64asByte := []byte(ITOA64)
roundsLog2 := uint(bytes.Index(ITOA64asByte, roundsChar))
return roundsLog2 // if 'E' is placed after $S$
}
// password crypt
func encrypt(password []byte, setting []byte) ([]byte, error) {
// setting is output of generateSalt() or it's stored hash
// we pull only the first 12 characters
setting = setting[0:12]
if !validateSalt(setting) {
return nil, InvalidSaltError
}
countLog2 := getCountLog2(setting) // @TODO: implement return depending on innput argument
salt := setting[4:12]
data := append(salt, password...)
checksum := sha512.Sum512(data)
var i, count uint64
count = 1 << countLog2
i = 0
for count > 0 {
data = append(checksum[:], password...)
checksum = sha512.Sum512(data)
count--
i++
}
output := append(setting, base64Encode(checksum[0:64])...)
return output[:55], nil
}
// Check password hash with stored hash
func Check(password string, hash string) bool {
storedHash := hash
if hash[:2] == "U$" {
// Drupal 7 md5 hash
storedHash = hash[1:]
passwordString := md5.New()
io.WriteString(passwordString, password)
password = string(passwordString.Sum(nil))
}
hashType := storedHash[:3]
switch hashType {
case "$S$":
// Drupal 7/8 sha512 hashes
passwordByte := []byte(password)
storedHashByte := []byte(storedHash)
computedHash, _ := encrypt(passwordByte, storedHashByte)
fmt.Printf("*** storedHash %s \n", storedHash)
fmt.Printf("*** computedHash %s \n", computedHash)
if(storedHash == string(computedHash)) {
return true
}
return false
case "$H$":
case "$P$":
fmt.Printf("md5 hashes\n")
break
default:
return false
}
return false
}