-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (96 loc) · 2.26 KB
/
main.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
package main
import (
"fmt"
"time"
"github.com/zekroTutorials/hashing/pkg"
"github.com/zekroTutorials/hashing/pkg/impl"
)
var (
// A set of some example passwords for following demonstration.
// Some passwords are duplicates to demonstrate the difference
// between raw and salted/peppered hashing.
passwords = []string{
"pw1",
"pw1",
"my_password",
"123456",
"123456",
"My_5up3r_5t0ng_PASSWORD_!",
}
)
// pair packs a password with its
// generated hash string.
type pair struct {
Password string
Hash string
}
// more returns v if v is larger
// than i, otherwise i is returned.
func more(i, v int) int {
if v > i {
return v
}
return i
}
// timeit takes the current time before and
// atfer executing the passed action. The
// duration between them is then returned.
func timeit(action func()) time.Duration {
start := time.Now()
action()
return time.Since(start)
}
// computeHashes takes a given hasher
// instance and computes the set of
// predefined passwords with it.
//
// The generated hashes are printed
// together with the used passwords.
// Also, the computation time is recorded
// and output for the hash generation and
// validation.
func computeHashes(hasher pkg.Hasher) {
var maxLenPw, maxLenHash int
pairs := make([]pair, len(passwords))
tookForHasing := timeit(func() {
for i, pw := range passwords {
hash := hasher.Generate(pw)
pairs[i].Password = pw
pairs[i].Hash = hash
maxLenPw = more(maxLenPw, len(pw))
maxLenHash = more(maxLenHash, len(hash))
}
})
tookForValidation := timeit(func() {
for _, p := range pairs {
if !hasher.Validate(p.Password, p.Hash) {
panic("validation failed")
}
}
})
fmt.Printf(
"----------------------------------------\n"+
" %s\n"+
"----------------------------------------\n",
hasher.GetName())
format := fmt.Sprintf("%%-%ds - %%%ds\n", maxLenPw, maxLenHash)
for _, p := range pairs {
fmt.Printf(format, p.Password, p.Hash)
}
fmt.Printf(
"\nTime for Hashing: %s\n"+
"Time for Validation: %s\n\n",
tookForHasing.String(),
tookForValidation.String())
}
func main() {
hashers := []pkg.Hasher{
impl.Sha256Raw{},
impl.Sha256Salted{},
impl.Sha256Peppered{},
impl.Argon2id{},
}
for _, hasher := range hashers {
computeHashes(hasher)
}
}