-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplain_hasher.go
49 lines (41 loc) · 1.03 KB
/
plain_hasher.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
package hasher
import (
"bytes"
)
// PlainHasher stored password as plain text
type PlainHasher struct {
Salt *string // Not used
Iter *int // Not used
Password *[]byte
}
// NewPlainHasher returns a new plain hasher instance
func NewPlainHasher(iterations *int, salt *string, hashedPassword *[]byte) Hasher {
return &PlainHasher{
Iter: iterations,
Salt: salt,
Password: hashedPassword,
}
}
// Code returns internal plain hasher code
func (h PlainHasher) Code() string {
return TypePlain
}
// Hash a password
func (h *PlainHasher) Hash(password string) []byte {
return []byte(password)
}
// SetPassword sets a password
func (h *PlainHasher) SetPassword(password string) {
_password := h.Hash(password)
h.Password = &_password
}
// Check if password is equal stored password
func (h *PlainHasher) Check(plain string) bool {
return bytes.Compare(h.Hash(plain), *h.Password) == 0
}
func (h *PlainHasher) String() string {
if h.Password == nil {
panic("password is not set")
}
return string(*h.Password)
}