Crypt::Passphrase - A module for managing passwords in a cryptographically agile manner
version 0.019
my $authenticator = Crypt::Passphrase->new( encoder => 'Argon2', validators => [ 'Bcrypt', 'SHA1::Hex' ], ); my ($hash) = $dbh->selectrow_array("SELECT password_hash FROM users WHERE name = ?", {}, $user); if (!$authenticator->verify_password($password, $hash)) { die "Invalid password"; } elsif ($authenticator->needs_rehash($hash)) { my $new_hash = $authenticator->hash_password($password); $dbh->do("UPDATE users SET password_hash = ? WHERE name = ?", {}, $new_hash, $user); }
This module manages the passwords in a cryptographically agile manner. Following Postel's principle, it allows you to define a single scheme that will be used for new passwords, but several schemes to check passwords with. It will be able to tell you if you should rehash your password, not only because the scheme is outdated, but also because the desired parameters have changed.
Note that this module doesn't depend on any backend, your application will have to depend on one or more of the backends listed under "SEE ALSO"
Crypt::Passphrase->new(%args encoder => 'Bcrypt', validators => [ 'SHA1::Hex' ], )
This creates a new Crypt::Passphrase object. It takes three named arguments:
Crypt::Passphrase
encoder
A Crypt::Passphrase object has a single encoder. This can be passed in three different ways:
A simple string
The name of the encoder class. If the value starts with a +, the + will be removed and the remainder will be taken as a fully-qualified package name. Otherwise, Crypt::Passphrase:: will be prepended to the value.
+
Crypt::Passphrase::
The class will be loaded, and constructed without arguments.
A hash
The module entry will be used to load a new Crypt::Passphrase module as described above, the other arguments will be passed to the constructor. This is the recommended option, as it gives you full control over the password parameters.
module
A Crypt::Passphrase::Encoder object
This will be used as-is.
This argument is mandatory.
validators
This is a list of additional validators for passwords. These values can each either be the same an encoder value, except that the last entry may also be a coderef that takes the password and the hash as its arguments and returns a boolean value.
This argument is optional and defaults to an empty list. The encoder is always considered as a validator and thus doesn't need to be specified.
normalization
This sets the unicode normalization form used for the password. Valid values are 'C' (composed; the the default), 'D' (decomposed), 'KC' (legacy composed) and 'KD' (legacy decomposed). You should probably not change this unless it's necessary for compatibility with something else, you should definitely not change this on an existing database as that will break passwords affected by normalization.
'C'
'D'
'KC'
'KD'
$passphrase->hash_password($password)
This will hash a $password with the encoder cipher, and return it (in crypt format). This will generally use a salt, and as such will return a different value each time even when called with the same password.
$password
$passphrase->verify_password($password, $hash)
This will check a $password satisfies a certain $hash and returns success or not. It will always return false if $hash isn't defined.
$hash
$passphrase->needs_rehash($hash)
This will check if a hash needs to be rehashed, either because it's in the wrong cipher or because the parameters are insufficient.
Calling this only ever makes sense after a password has been verified.
$passphrase->recode_hash($hash)
This recodes a hash if possible. This is mainly relevant when upgrading to a new pepper, but can also be relevant when a cipher has multiple known encodings (e.g. scrypt).
$passphrase->curry_with_hash($hash)
This creates a Crypt::Passphrase::PassphraseHash object for the hash, effectively currying Crypt::Passphrase with that hash. This can be useful for plugging Crypt::Passphrase into some frameworks (e.g. ORMs) that require a singular object to contain everything you need to match passwords against.
Crypt::Passphrase::PassphraseHash
While encoders generally allow for a default configuration, I would strongly encourage anyone to research what settings work for your application. It is generally a trade-off between usability/resources and security.
If your application is deployed by different people than it's developed by it may be helpful to have the configuration for Crypt::Passphrase part of your application configuration file and not be hardcoded so that your users can choose the right settings for them.
Crypt::Passphrase considers passwords to be text, and as such you should ensure any password input is decoded if it contains any non-ascii characters. Crypt::Passphrase will take care of both normalizing and encoding such input.
Hashing passwords is by its nature a heavy operations. It can be abused by malignant actors who want to try to DOS your application. It may be wise to do some form of DOS protection such as a proof-of-work scheme or a captcha.
In some situations, it may be appropriate to have different password settings for different users (e.g. set them more strict for administrators than for ordinary users).
The following encoders are currently available on CPAN:
Crypt::Passphrase::Argon2
This is a state-of-the-art memory-hard password hashing algorithm, recommended for higher-end parameters. Winner of the Password Hash Competition of 2015.
Crypt::Passphrase::Bcrypt
And older but still safe password hashing algorithm, recommended for lower-end parameters or if you need to be compatible with BSD system passwords.
Crypt::Passphrase::Yescrypt
Another state-of-the-art memory-hard password hashing algorithm. Finalist of the Password Hash Competition of 2015 and used in some recent Linux distributions for user passwords.
Crypt::Passphrase::PBKDF2
A FIPS-standardized hashing algorithm. Only recommended when FIPS-compliance is required.
Crypt::Passphrase::Linux
An implementation of SHA-512, SHA256 and MD5 based crypt(). Recommended if you need to be compatible with standard Linux system passwords.
crypt()
Crypt::Passphrase::Scrypt
A first-generation memory-hard algorithm, Argon2 is recommended instead if you want a memory-hard algorithm.
Crypt::Passphrase::System
Your system's crypt implementation. Support for various algorithms varies between platforms and platform versions, and while on some platforms it's a good backend one should not rely on this for a portable result.
crypt
Crypt::Passphrase::Argon2::AES
A peppering implementation that AES encrypts an argon2 hash. Recommended when wanting to pepper with argon2 as it allows offline repeppering and offers strong cryptographic guarantees.
Crypt::Passphrase::Pepper::Simple
A meta-encoder that adds peppering to your passwords by pre-hashing the inputs. Recommended when wanting to pepper with hashes other than argon2 as it can be combined with any encoder.
Additionally, the following integrations are supported
A number of integrations of Crypt::Passphrase exist:
DBIx::Class::CryptColumn
Mojolicious::Plugin::Passphrase
Dancer2::Plugin::CryptPassphrase
Leon Timmermans <fawaka@gmail.com>
This software is copyright (c) 2021 by Leon Timmermans.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Crypt::Passphrase, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Crypt::Passphrase
CPAN shell
perl -MCPAN -e shell install Crypt::Passphrase
For more information on module installation, please visit the detailed CPAN module installation guide.