-
Notifications
You must be signed in to change notification settings - Fork 0
/
balloon.go
68 lines (58 loc) · 1.14 KB
/
balloon.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
package balloon
import (
"crypto/sha512"
"encoding/hex"
"hash"
"github.com/nogoegst/balloon"
)
const (
// see: https://godoc.org/github.com/nogoegst/balloon
defaultSpaceCost = 16
defaultTimeCost = 16
defaultParallelism = 1
)
var (
defaultHashFn = sha512.New
)
// Balloon is struct to create hash.
type Balloon struct {
HashFn func() hash.Hash
SpaceCost uint64
TimeCost uint64
Parallelism uint64 // p
}
// Hash creates hased text from password.
func (b Balloon) Hash(password, salt string) string {
return hex.EncodeToString(balloon.BalloonM(
b.getHashFn(),
[]byte(password),
[]byte(salt),
b.getSpaceCost(),
b.getTimeCost(),
b.getParallelism(),
))
}
func (b Balloon) getHashFn() func() hash.Hash {
if b.HashFn == nil {
return defaultHashFn
}
return b.HashFn
}
func (b Balloon) getSpaceCost() uint64 {
if b.SpaceCost == 0 {
return defaultSpaceCost
}
return b.SpaceCost
}
func (b Balloon) getTimeCost() uint64 {
if b.TimeCost == 0 {
return defaultTimeCost
}
return b.TimeCost
}
func (b Balloon) getParallelism() uint64 {
if b.Parallelism == 0 {
return defaultParallelism
}
return b.Parallelism
}