forked from bearded-web/bearded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passlib.go
65 lines (55 loc) · 1.6 KB
/
passlib.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
package passlib
// passlib is a password hashing library for golang, which provides cross-platform implementations
// of over 3 password hashing algorithms, as well as a framework for managing existing password hashes.
// It’s designed to be useful for a wide range of tasks, from verifying a hash found in /etc/shadow,
// to providing full-strength password hashing for multi-user application.
//
// Inspired by great python library passlib http://pythonhosted.org/passlib/
import (
"fmt"
"strings"
)
const (
Divider string = "$"
DividerB byte = 0x24
)
type Algo interface {
Name() string // unique algorithm name
Encrypt(password string) (string, error)
Verify(password, hash string) (bool, error)
// Compatible(hash string) bool // is hash compatible with this algo?
}
type Context struct {
Default Algo
}
func NewContext() *Context {
return &Context{
Default: NewBcrypt(0),
}
}
type EncOpts struct {
}
type VerOpts struct {
}
func (c *Context) Encrypt(password string, _ ...EncOpts) (string, error) {
algo := c.Default
hash, err := algo.Encrypt(password)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s%s", Divider, algo.Name(), hash), nil
}
func (c *Context) Verify(password string, hash string, _ ...VerOpts) (bool, error) {
algo := c.Default
if strings.HasPrefix(hash, Divider) {
hash = hash[1:]
}
parts := strings.SplitN(hash, Divider, 2)
if len(parts) < 2 {
return false, fmt.Errorf("Bad hash format")
}
if parts[0] != algo.Name() {
return false, fmt.Errorf("Unsupported algorithm: %s", parts[0])
}
return c.Default.Verify(password, fmt.Sprintf("$%s", parts[1]))
}