Python's passlib is quite an amazing library. I'm not sure there's a password library in existence with more thought put into it, or with more support for obscure password formats.
This is a skeleton of a port of passlib to Go. It dogmatically adopts the modular crypt format, which passlib has excellent documentation for.
Currently, it supports:
- Argon2i
- scrypt-sha256
- sha512-crypt
- sha256-crypt
- bcrypt
- passlib's bcrypt-sha256 variant
- pbkdf2-sha512 (in passlib format)
- pbkdf2-sha256 (in passlib format)
- pbkdf2-sha1 (in passlib format)
By default, it will hash using scrypt-sha256 and verify existing hashes using any of these schemes.
There's a default context for ease of use. Most people need only concern
themselves with the functions Hash and Verify:
// Hash a plaintext, UTF-8 password.funcHash(passwordstring) (hashstring, errerror)
// Verifies a plaintext, UTF-8 password using a previously derived hash.// Returns non-nil err if verification fails.//// Also returns an upgraded password hash if the hash provided is// deprecated.funcVerify(password, hashstring) (newHashstring, errerror)Here's a rough skeleton of typical usage.
import"gopkg.in/hlandau/passlib.v1"funcRegisterUser() {
(...)
password:=geta (UTF-8, plaintext) passwordfromsomewherehash, err:=passlib.Hash(password)
iferr!=nil {
// couldn't hash password for some reasonreturn
}
(storehashindatabase, etc.)
}
funcCheckPassword() bool {
password:=getthepasswordtheuserenteredhash:=thehashyoustoredfromthecalltoHash()
newHash, err:=passlib.Verify(password, hash)
iferr!=nil {
// incorrect password, malformed hash, etc.// either way, rejectreturnfalse
}
// The context has decided, as per its policy, that// the hash which was used to validate the password// should be changed. It has upgraded the hash using// the verified password.ifnewHash!="" {
(storenewHashindatabase, replacingoldhash)
}
returntrue
}Since scrypt does not have a pre-existing modular crypt format standard, I made one. It's as follows:
$s2$N$r$p$salt$hash
...where N, r and p are the respective difficulty parameters to scrypt as positive decimal integers without leading zeroes, and salt and hash are base64-encoded binary strings. Note that the RFC 4648 base64 encoding is used (not the one used by sha256-crypt and sha512-crypt).
passlib is partially derived from Python's passlib and so maintains its BSD license.
© 2008-2012 Assurance Technologies LLC. (Python passlib) BSD License
© 2014 Hugo Landau <hlandau@devever.net> BSD License