Crypt::Util - A lightweight Crypt/Digest convenience API
use Crypto::Util; # also has a Sub::Exporter to return functions wrapping a default instance
my $util = Crypto::Util->new;
$util->default_key("my secret");
# MAC or cipher+digest based tamper resistent encapsulation
# (uses Storable on $data if necessary)
my $tamper_resistent_string = $util->tamper_proof( $data );
my $verified = $util->thaw_tamper_proof( $untrusted_string, key => "another secret" );
# If the encoding is unspecified, base32 is used
# (hex if base32 is unavailable)
my $encoded = $util->encode_string( $bytes );
my $hash = $util->digest( $bytes, digest => "md5" );
die "baaaad" unless $util->verify_hash(
hash => $hash,
data => $bytes,
digest => "md5",
);
This module provides an easy, intuitive and forgiving API for wielding crypto-fu.
This module is designed to have an easy API to allow easy but responsible use of the more low level Crypt:: and Digest:: modules on CPAN. Therefore, patches to improve ease-of-use are very welcome.
Dependency hell is avoided using a fallback mechanism that tries to choose an algorithm based on an overridable list.
For "simple" use install Crypt::Util and your favourite digest, cipher and cipher mode (CBC, CFB, etc).
To ensure predictable behavior the fallback behavior can be disabled as necessary.
To ensure that your hashes and strings are compatible with the Crypt::Util manpage
deployments on other machines (where different Crypt/Digest modules are
available, etc) you should use disable_fallback.
Then either set the default ciphers, or always explicitly state the cipher.
If you are only encrypting and decrypting with the same installation, and new cryptographic modules are not being installed, the hashes/ciphertexts should be compatible without disabling fallback.
NOTE: nothing is exported by default.
the Crypt::Util manpage also presents an optional exported api using the Sub::Exporter manpage.
Unlike typical exported APIs, there is no class level default instance shared by all the importers, but instead every importer gets its own instance.
For example:
package A;
use Crypt::Util qw/:all/;
default_key("moose");
my $ciphertext = encrypt_string($plain);
package B;
use Crypt::Util qw/:all/;
default_key("elk");
my $ciphertext = encrypt_string($plain);
In this example every importing package has its own implicit instance, and the
default_key function will in fact not share the value.
You can get the instance using the exported_instance function, which is just
the identity method.
The export tags supported are: crypt (encryption and tamper proofing related
functions), digest (digest and MAC related functions), encoding (various
encoding and decoding functions), and params which give you functions for
handling default values.
The tamper_proof method is in an intermittent state, in that the data
parameter's API is not completely finalized.
It is safer to use tamper_proof_string; its API is expected to remain the
same in future versions as well.
See TODO for more information about the data types that will be supported in the future.
When thawing, the verify_digest or verify_mac methods will be used, with
fatal defaulting to on unless explicitly disabled in the parameters.
This method accepts the following parameters:
By default this parameter is true, unless default_tamper_proof_unencrypted(),
has been enabled.
A true value implies that all the parameters
which are available to encrypt_string() are also available. If a
negative value is specified, MAC mode is used, and the additional
parameters of mac_digest_string() may also be specified to this method.
The data to encrypt. If this is a reference the Storable manpage will be used to serialize the data.
If the string is encrypted then all the parameters of encrypt_string and
digest_string are also available.
If the string is not encrypted, then all the parameters of mac_digest_string
are also available.
All of the parameters which may be supplied to process_key(),
cipher_object and maybe_encode are also available to these methods.
The following parameters may be used:
The string to be en/decrypted can either be supplied first, creating an odd number of arguments, or as a named parameter.
The following arguments may be specified:
This disables mungung. See also default_use_literal_key.
Can be used to force a key size, even if the cipher specifies another size.
If not specified, the key size chosen will depend
Used to determine the key size.
Available parameters are:
The cipher algorithm to use.
The mode of operation (cbc, cfb).
See http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation for an explanation of this.
Delegates to digest_object. All parameters which can be used by
digest_object may also be used here.
The following arguments are available:
The string to be digested can either be supplied first, creating an odd number of arguments, or as a named parameter.
Delegates to digest_object. All parameters which can be used by
digest_object may also be used here.
The following parameters are accepted:
A string containing the hash to verify.
The digested string.
If true, errors will be fatal. The default is false, which means that failures will return undef.
In addition, the parameters which can be supplied to digest_string()
may also be supplied to this method.
The digest algorithm to use.
Returns an object using the Digest manpage.
The following parameters are accepted:
The encoding may be a symbolic type (uri, printable) or a concrete type (none, hex, base64, base32).
Delegates to mac_object. All parameters which can be used by mac_object
may also be used here.
Delegates to mac_object. All parameters which can be used by mac_object
may also be used here.
The following additional arguments are allowed:
The MAC string to verify.
The digested string.
If true, errors will be fatal. The default is false, which means that failures will return undef.
The MAC algorithm to use. Currently only hmac is supported.
This method has no external API but is documented for the sake of its shared options.
It is delegated to by the various encryption and digest method.
Expects a bool.
Expects an algorithm name (symbolic (e.g. uri, alphanumeric), or concrete
(e.g. base64, hex)).
If encode is explicitly supplied it will always determine whether or not the
string will be encoded. Otherwise, if encoding is explicitly supplied then
the string will always be encoded using the specified algorithm. If neither is
supplied default_encode will be checked to determine whether or not to
encode, and default_encoding or fallback_encoding will be used to
determine the algorithm to use (see HANDLING OF DEFAULT VALUES).
The above methods encode based on a fallback list (see HANDLING OF DEFAULT VALUES).
The variations denote types of formats: alphanumerical is letters and
numbers only (case insensitive), uri is safe for inclusions in URIs (without
further escaping), and printable contains no control characters or
whitespace.
Big endian hexadecimal (H* pack format).
the URI::Escape manpage based encoding.
Requires the MIME::Base64 manpage.
The wrapped variant will introduce line breaks as per the the MIME::Base64 manpage
default>.
Requires the MIME::Base64 manpage.
Implements the Base64 for URIs. See http://en.wikipedia.org/wiki/Base64#URL_Applications.
Requires the MIME::Base32 manpage.
(note- unlike the MIME::Base32 manpage this is case insensitive).
disable_fallback()
When true only the first item from the fallback list will be tried, and if it can't be loaded there will be a fatal error.
Enable this to ensure portability.
For every parameter, there are several methods, where PARAMETER is replaced with the parameter name:
default_PARAMETER()
This accessor is available for the user to override the default value.
If set to undef, then fallback_PARAMETER will be consulted instead.
ALL the default values are set to undef unless changed by the user.
fallback_PARAMETER()
Iterates the fallback_PARAMETER_list, choosing the first value that is
usable (it's provider is available).
If disable_fallback is set to a true value, then only the first value in the
fallback list will be tried.
fallback_PARAMETER_list()
An ordered list of values to try and use as fallbacks.
fallback_PARAMETER iterates this list and chooses the first one that works.
Available parameters are as follows:
The fallback list is
Rijndael, Serpent, Twofish, RC6, Blowfish and RC5.
the Crypt::Rijndael manpage is the AES winner, the next three are AES finalists, and the last two are well known and widely used.
The mode in which to use the cipher.
The fallback list is CFB, CBC, Ctr, and OFB.
The fallback list is SHA-1, SHA-256, RIPEMD160,
Whirlpool, MD5, and Haval256.
The fallback list is hex (effectively no fallback).
The fallback list is base32 and hex.
the MIME::Base32 manpage is required for base32 encoding.
The fallback list is uri_base64.
The fallback list is base64
The following parameters have a default_ method, as described in the
previous section, but the fallback_ methods are not applicable.
Whether or not to encode by default (applies to digests and encryptions).
The key to use. Useful for when you are repeatedly encrypting.
Whether or not to not hash the key by default. See process_key.
Whether or not tamper resistent strings are by default unencrypted (just MAC).
You may safely subclass and override default_PARAMETER and
fallback_PARAMETER_list to provide values from configurations.
Overriding the fallback_PARAMETER method is also "allowed" but not
reccomended.
Features which are currently missing but are scheduled for 0.02 or 0.03:
Crypt::SaltedHash support
CMAC, EMAC (maybe, the modules are not OO and require refactoring) message authentication modes.
EAX/OCB encryption mode (with implicit authentication)
Bruce Schneier Fact Database http://geekz.co.uk/lovesraymond/archive/bruce-schneier-facts.
Entropy fetching (get N weak/strong bytes, etc) from e.g. OpenSSL bindings, /dev/*random, and EGD.
Additional data formats (streams/iterators, filehandles, generalized storable data/string handling for all methods, not just tamper_proof).
Streams should also be able to used via a simple push api.
the Digest manpage, the Crypt::CBC manpage, the Crypt::CFB manpage, http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation.
This module is maintained using Darcs. You can get the latest version from
http://nothingmuch.woobling.org/Crypt-Util/, and use darcs send to commit
changes.
Yuval Kogman, <nothingmuch@woobling.org> Ann Barcomb
Copyright 2006 by Yuval Kogman <nothingmuch@woobling.org>, Ann Barcomb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.