/
helpers.go
52 lines (43 loc) · 1.19 KB
/
helpers.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
package riposo
import (
"github.com/riposo/riposo/pkg/identity"
"github.com/riposo/riposo/pkg/slowhash"
)
// HelpersOptions are used to configure helpers.
type HelpersOptions struct {
Identity string // type of identity generator to use
SlowHash string // type of slowhash to use
}
// Helpers provide access to elementary helper functions.
type Helpers struct {
slowHash slowhash.Generator
nextID identity.Factory
}
// NewHelpers inits helpers.
func NewHelpers(opt HelpersOptions) (*Helpers, error) {
slowHash, err := slowhash.Get(opt.SlowHash)
if err != nil {
return nil, err
}
nextID, err := identity.Get(opt.Identity)
if err != nil {
return nil, err
}
return CustomHelpers(slowHash, nextID), nil
}
// CustomHelpers inits custom helpers.
func CustomHelpers(slowHash slowhash.Generator, nextID identity.Factory) *Helpers {
return &Helpers{
slowHash: slowHash,
nextID: nextID,
}
}
// SlowHash applies a password-hash to a plain string
// returning a cryptographically secure, hashed string.
func (e *Helpers) SlowHash(plain string) (string, error) {
return e.slowHash(plain)
}
// NextID returns a new globally unique ID.
func (e *Helpers) NextID() string {
return e.nextID()
}