forked from alexandrevicenzi/unchained
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md5.go
110 lines (89 loc) · 2.93 KB
/
md5.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 md5
import (
"crypto/hmac"
"crypto/md5"
"errors"
"fmt"
"io"
"strings"
)
// Errors returned by UnsaltedMD5PasswordHasher and/or MD5PasswordHasher.
var (
ErrHashComponentMismatch = errors.New("unchained/md5: hashed password components mismatch")
ErrAlgorithmMismatch = errors.New("unchained/md5: algorithm mismatch")
ErrSaltContainsDollarSing = errors.New("unchained/md5: salt contains dollar sign ($)")
ErrSaltIsEmpty = errors.New("unchained/md5: salt is empty")
)
// UnsaltedMD5PasswordHasher implements a simple MD5 password hasher.
type UnsaltedMD5PasswordHasher struct {
// Algorithm identifier.
Algorithm string
}
// Encode turns a plain-text password into a hash.
func (h *UnsaltedMD5PasswordHasher) Encode(password string) (string, error) {
hasher := md5.New()
io.WriteString(hasher, password)
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}
// Verify if a plain-text password matches the encoded digest.
func (h *UnsaltedMD5PasswordHasher) Verify(password string, encoded string) (bool, error) {
if len(encoded) == 37 && strings.HasPrefix(encoded, "md5$$") {
encoded = encoded[5:]
}
newencoded, err := h.Encode(password)
if err != nil {
return false, err
}
return hmac.Equal([]byte(newencoded), []byte(encoded)), nil
}
// MD5PasswordHasher implements Salted MD5 password hasher.
type MD5PasswordHasher struct {
// Algorithm identifier.
Algorithm string
}
// Encode turns a plain-text password into a hash.
func (h *MD5PasswordHasher) Encode(password string, salt string) (string, error) {
if len(salt) == 0 {
return "", ErrSaltIsEmpty
}
if strings.Contains(salt, "$") {
return "", ErrSaltContainsDollarSing
}
hasher := md5.New()
io.WriteString(hasher, salt)
io.WriteString(hasher, password)
return fmt.Sprintf("%s$%s$%x", h.Algorithm, salt, hasher.Sum(nil)), nil
}
// Verify if a plain-text password matches the encoded digest.
func (h *MD5PasswordHasher) Verify(password string, encoded string) (bool, error) {
s := strings.Split(encoded, "$")
if len(s) != 3 {
return false, ErrHashComponentMismatch
}
algorithm, salt := s[0], s[1]
if algorithm != h.Algorithm {
return false, ErrAlgorithmMismatch
}
newencoded, err := h.Encode(password, salt)
if err != nil {
return false, err
}
return hmac.Equal([]byte(newencoded), []byte(encoded)), nil
}
// NewUnsaltedMD5PasswordHasher is an incredibly insecure algorithm
// that should never be used. It stores unsalted MD5 hashes without
// the algorithm prefix, also hashes with an empty salt.
//
// This algorithm is implemented because Django used to store passwords this way
// and to accept such password hashes.
func NewUnsaltedMD5PasswordHasher() *UnsaltedMD5PasswordHasher {
return &UnsaltedMD5PasswordHasher{
Algorithm: "unsalted_md5",
}
}
// NewMD5PasswordHasher secures password hashing using Salted MD5 algorithm (not recommended).
func NewMD5PasswordHasher() *MD5PasswordHasher {
return &MD5PasswordHasher{
Algorithm: "md5",
}
}