Documentation ¶
Overview ¶
Package passlib provides a simple password hashing and verification interface abstracting multiple password hashing schemes.
Most people need concern themselves only with the functions Hash and Verify, which uses the default context and sensible defaults.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultSchemes = []abstract.Scheme{ scrypt.SHA256Crypter, sha2crypt.Crypter256, sha2crypt.Crypter512, bcryptsha256.Crypter, bcrypt.Crypter, }
The default schemes, most preferred first. The first scheme will be used to hash passwords, and any of the schemes may be used to verify existing passwords. The contents of this value may change with subsequent releases.
Functions ¶
func Hash ¶
Hashes a UTF-8 plaintext password using the default context and produces a password hash. Chooses the preferred password hashing scheme based on the configured policy. The default policy is sensible.
Example (Signup) ¶
User signup example.
// User signup example. // ... signup code ... // Get the password the user chose by whatever means. password := getSignupPassword() username := getSignupUsername() hash, err := Hash(password) if err != nil { // couldn't hash password for some reason return } // hash now contains a hash in modular crypt form. // Store hash in database, etc. storeHashInDatabase(username, hash)
Output:
func NeedsUpdate ¶
Uses the default context to determine whether a stub or hash needs updating.
func Verify ¶
Verifies a UTF-8 plaintext password using a previously derived password hash and the default context. Returns nil err only if the password is valid.
If the hash is determined to be deprecated based on policy, and the password is valid, the password is hashed using the preferred password hashing scheme and returned in newHash. You should use this to upgrade any stored password hash in your database.
newHash is empty if the password was invalid or no upgrade is required.
You should treat any non-nil err as a password verification error.
Example (Login) ¶
User login example.
// User login example. // Get the password for the user we have stored in the database. hash := getUserHashFromDatabase() // Get the plaintext password the user tried to login with. password := getLoginPassword() newHash, err := Verify(password, hash) if err != nil { // Incorrect password, malformed hash, etc. return } if newHash != "" { // passlib thinks we should upgrade to a new stronger hash. // ... store the new hash in the database ... } // ... log the user in ...
Output:
func VerifyNoUpgrade ¶ added in v1.0.7
Like Verify, but never upgrades.
Types ¶
type Context ¶
type Context struct { // Slice of schemes to use, most preferred first. // // If left uninitialized, a sensible default set of schemes will be used. // // An upgrade hash (see the newHash return value of the Verify method of the // abstract.Scheme interface) will be issued whenever a password is validated // using a scheme which is not the first scheme in this slice. Schemes []abstract.Scheme }
A password hashing context, that uses a given set of schemes to hash and verify passwords.
var DefaultContext Context
The default context, which uses sensible defaults. Most users should not reconfigure this. The defaults may change over time, so you may wish to reconfigure the context or use a custom context if you want precise control over the hashes used.
func (*Context) Hash ¶
Hashes a UTF-8 plaintext password using the context and produces a password hash.
If stub is "", one is generated automaticaly for the preferred password hashing scheme; you should specify stub as "" in almost all cases.
The provided or randomly generated stub is used to deterministically hash the password. The returned hash is in modular crypt format.
If the context has not been specifically configured, a sensible default policy is used. See the fields of Context.
func (*Context) NeedsUpdate ¶
Determines whether a stub or hash needs updating according to the policy of the context.
func (*Context) Verify ¶
Verifies a UTF-8 plaintext password using a previously derived password hash and the default context. Returns nil err only if the password is valid.
If the hash is determined to be deprecated based on the context policy, and the password is valid, the password is hashed using the preferred password hashing scheme and returned in newHash. You should use this to upgrade any stored password hash in your database.
newHash is empty if the password was not valid or if no upgrade is required.
You should treat any non-nil err as a password verification error.
func (*Context) VerifyNoUpgrade ¶ added in v1.0.7
Like Verify, but does not hash an upgrade password when upgrade is required.
Directories ¶
Path | Synopsis |
---|---|
Package abstract contains the abstract description of the Scheme interface, plus supporting error definitions.
|
Package abstract contains the abstract description of the Scheme interface, plus supporting error definitions. |
hash
|
|
bcrypt
Package bcrypt implements the bcrypt password hashing mechanism.
|
Package bcrypt implements the bcrypt password hashing mechanism. |
bcryptsha256
Package bcryptsha256 implements bcrypt with a SHA256 prehash in a format that is compatible with Python passlib's equivalent bcrypt-sha256 scheme.
|
Package bcryptsha256 implements bcrypt with a SHA256 prehash in a format that is compatible with Python passlib's equivalent bcrypt-sha256 scheme. |
scrypt
Package scrypt implements the scrypt password hashing mechanism, wrapped in the modular crypt format.
|
Package scrypt implements the scrypt password hashing mechanism, wrapped in the modular crypt format. |
scrypt/raw
Package raw provides a raw implementation of the modular-crypt-wrapped scrypt primitive.
|
Package raw provides a raw implementation of the modular-crypt-wrapped scrypt primitive. |
sha2crypt
Package sha2crypt implements sha256-crypt and sha512-crypt.
|
Package sha2crypt implements sha256-crypt and sha512-crypt. |
sha2crypt/raw
Package raw provides a raw implementation of the sha256-crypt and sha512-crypt primitives.
|
Package raw provides a raw implementation of the sha256-crypt and sha512-crypt primitives. |