/
pbkdf2.go
108 lines (90 loc) · 2.77 KB
/
pbkdf2.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
package pbkdf2
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"hash"
"strconv"
"strings"
"golang.org/x/crypto/pbkdf2"
)
// Errors returned by PBKDF2Hasher.
var (
ErrHashComponentUnreadable = errors.New("unchained/pbkdf2: unreadable component in hashed password")
ErrHashComponentMismatch = errors.New("unchained/pbkdf2: hashed password components mismatch")
ErrAlgorithmMismatch = errors.New("unchained/pbkdf2: algorithm mismatch")
ErrSaltContainsDollarSing = errors.New("unchained/pbkdf2: salt contains dollar sign ($)")
)
// PBKDF2Hasher implements PBKDF2 password hasher.
type PBKDF2Hasher struct {
// Algorithm identifier.
Algorithm string
// Defines the number of rounds used to encode the password.
Iterations int
// Defines the length of the hash in bytes.
Size int
// Defines the hash function used to encode the password.
Digest func() hash.Hash
}
// Encode turns a plain-text password into a hash.
func (h *PBKDF2Hasher) Encode(password string, salt string, iterations int) (string, error) {
if strings.Contains(salt, "$") {
return "", ErrSaltContainsDollarSing
}
if iterations <= 0 {
iterations = h.Iterations
}
hash := pbkdf2.Key([]byte(password), []byte(salt), iterations, h.Size, h.Digest)
b64Hash := base64.StdEncoding.EncodeToString(hash)
return fmt.Sprintf("%s$%d$%s$%s", h.Algorithm, iterations, salt, b64Hash), nil
}
// Verify if a plain-text password matches the encoded digest.
func (h *PBKDF2Hasher) Verify(password string, encoded string) (bool, error) {
s := strings.Split(encoded, "$")
if len(s) != 4 {
return false, ErrHashComponentMismatch
}
algorithm, iterations, salt := s[0], s[1], s[2]
if algorithm != h.Algorithm {
return false, ErrAlgorithmMismatch
}
i, err := strconv.Atoi(iterations)
if err != nil {
return false, ErrHashComponentUnreadable
}
newencoded, err := h.Encode(password, salt, i)
if err != nil {
return false, err
}
return hmac.Equal([]byte(newencoded), []byte(encoded)), nil
}
// NewPBKDF2SHA1Hasher secures password hashing using the PBKDF2 algorithm.
//
// Alternate PBKDF2 hasher which uses SHA1, the default PRF
// recommended by PKCS #5.
//
// This is compatible with other implementations of PBKDF2,
// such as openssl's PKCS5_PBKDF2_HMAC_SHA1().
func NewPBKDF2SHA1Hasher() *PBKDF2Hasher {
return &PBKDF2Hasher{
Algorithm: "pbkdf2_sha1",
Iterations: 216000,
Size: sha1.Size,
Digest: sha1.New,
}
}
// NewPBKDF2SHA256Hasher secures password hashing using the PBKDF2 algorithm.
//
// Configured to use PBKDF2 + HMAC + SHA256.
// The result is a 64 byte binary string.
func NewPBKDF2SHA256Hasher() *PBKDF2Hasher {
return &PBKDF2Hasher{
Algorithm: "pbkdf2_sha256",
Iterations: 216000,
Size: sha256.Size,
Digest: sha256.New,
}
}