Password hashing primitives. Each hasher computes a salted, iterated
digest and exposes its salt / iteration count / digest so the
companion marshaller
package can serialize the result in a Django-style
algorithm$iterations$salt$hash format.
go get github.com/go-passwd/hasherhshr, err := hasher.New(hasher.TypeSHA512)
if err != nil {
log.Fatal(err)
}
hshr.SetPassword(plainTextPassword)
if hshr.Check(userInput) {
// password matches
}SetPassword generates a random salt and uses DefaultIter iterations
when neither is set. To reuse a hash from storage, build the hasher
with the original salt and iteration count:
iter := 2048
salt := "..."
password := []byte{...}
hshr, err := hasher.NewWithParams(hasher.TypeSHA256, &iter, &salt, &password)
if err != nil {
log.Fatal(err)
}
hshr.Check(userInput)| Type | Constant |
|---|---|
| Plain text | TypePlain |
| MD5 | TypeMD5 |
| SHA-1 | TypeSHA1 |
| SHA-224 | TypeSHA224 |
| SHA-256 | TypeSHA256 |
| SHA-384 | TypeSHA384 |
| SHA-512 | TypeSHA512 |
| SHA-512/224 | TypeSHA512_224 |
| SHA-512/256 | TypeSHA512_256 |
PlainHasher ignores salt and iterations.
Implement the Hasher interface (and optionally marshaller.Marshalable
for serialization) and call hasher.Register:
hasher.Register("custom", func(iter *int, salt *string, password *[]byte) hasher.Hasher {
return &MyHasher{Salt: salt, Iter: iter, Password: password}
})