/
hog.go
235 lines (178 loc) · 3.79 KB
/
hog.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
/*
Package hog is a wrapper around Go `crypto` package for quick hashing of passwords
with salts appended.
Currently only supporting `md5`, `sha1`, and `sha256` packages for both the
salt and the combination.
Examples
Create an instance of `Combination` and supply the hash function type
constant (MD5, SHA1, SHA256) and a password string. Without providing
a salt, it will be created with SHA1 with `Salt.Size = 16` and append to
the password before being hashed.
package main
import (
"fmt"
"github.com/jochasinga/hog"
)
func main() {
pw := hog.Combination{
Func: hog.SHA1,
Secret: "supersecretpassword",
}
hogged := pw.New() // return a []byte of hashed combination
fmt.Println(string(hogged)) // 0a12a5f5046bdea95f1ba8f8a570d7361922c0a9
}
A `Salt` can be created separately before using in the `Combination`
func main() {
password := "superStrongPassword321"
salt := hog.Salt{
Func: hog.MD5,
Secret: password,
Size: 16,
}
bsalt := salt.New()
pw := hog.Combination{
Func: hog.SHA256,
Secret: password,
Salt: bsalt
}
hogged := pw.New()
}
Or use helper functions to create hash
func main() {
// Same variables as previous
bsalt := hog.CreateHash(salt)
hogged := hog.CreateHash(pw)
// or just create default combination from password string
fin := hog.CreateHashFromString(password)
}
*/
package hog
import (
"bytes"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"io"
"os"
)
type Hash uint
const (
MD5 Hash = 1 + iota // import crypto/md5
SHA1 // import crypto/sha1
SHA256 // import crypto/sha256
)
var hashes = [...]string{
"MD5",
"SHA1",
"SHA256",
}
func (h Hash) String() string {
return hashes[h]
}
type Hog interface {
New() []byte
}
type Salt struct {
Func Hash
Size uint
Secret string
}
type Combination struct {
Func Hash
Secret string
Salt []byte
}
func (s Salt) New() []byte {
fn := s.Func
var size uint
switch fn {
case MD5:
size = md5.Size
case SHA1:
size = sha1.Size
case SHA256:
size = sha256.Size
default:
size = sha1.Size
}
buf := make([]byte, s.Size, s.Size+size)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
fmt.Printf("random read failed: %v", err)
os.Exit(1)
}
var h hash.Hash
switch fn {
case MD5:
h = md5.New()
case SHA1:
h = sha1.New()
case SHA256:
h = sha256.New()
default:
h = sha1.New()
}
h.Write(buf)
h.Write([]byte(s.Secret))
return h.Sum(buf)
}
func (c Combination) New() []byte {
if len(c.Salt) == 0 {
c.Salt = generateSalt(c.Secret, 16)
}
salt := c.Salt
comb := c.Secret + string(salt)
fn := c.Func
var pwhash hash.Hash
switch fn {
case MD5:
pwhash = md5.New()
case SHA1:
pwhash = sha1.New()
case SHA256:
pwhash = sha256.New()
default:
pwhash = sha1.New()
}
io.WriteString(pwhash, comb)
return pwhash.Sum(nil)
}
/* Helper Functions */
// CreateHash will create a hashed `[]byte` from a `Salt` or `Combination`
// instance.
func CreateHash(h Hog) []byte {
return h.New()
}
// CreateHashFromString will create a hashed combination based on a provided
// password string.
func CreateHashFromString(secret string) []byte {
salt := generateSalt(secret, 16)
c := Combination{
Func: SHA1,
Secret: secret,
Salt: salt,
}
return c.New()
}
// Match two `Hog` instance i.e. password matching
func Match(h1, h2 Hog) bool {
match := bytes.Equal(h1.New(), h2.New())
return match
}
// Private function
func generateSalt(secret string, saltSize uint) []byte {
sc := []byte(secret)
buf := make([]byte, saltSize, saltSize+sha1.Size)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
fmt.Printf("random read failed: %v", err)
os.Exit(1)
}
hash := sha1.New()
hash.Write(buf)
hash.Write(sc)
return hash.Sum(buf)
}