| Crypt-Util documentation | view source | Contained in the Crypt-Util distribution. |
Crypt::Util - A lightweight Crypt/Digest convenience API
use Crypt::Util; # also has a Sub::Exporter to return functions wrapping a default instance
my $util = Crypt::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.
The API is designed as a cascade, with rich features built using simpler ones. this means that the option processing is uniform throughout, and the behaviors are generally predictable.
Note that Crypt::Util doesn't do any crypto on its own, but delegates the actual work to the various other crypto modules on the CPAN. Crypt::Util merely wraps these modules, providing uniform parameters, and building on top of their polymorphism with higher level features.
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 Crypt::Util
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.
Crypt::Util also presents an optional exported api using Sub::Exporter.
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 authenticated_decrypt_string 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 authenticated_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 Storable will be used to serialize the data.
See pack_data for details.
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 authenticated variants ensure that an authenticated encryption mode
(such as EAX) is used.
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 cryptographic nonce to use. Only necessary for encryption, will be packed in the string as part of the message if applicable.
Not yet supported.
In the future this will include a header for AEAD (the "associated data" bit of AEAD).
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.
If a nonce is explicitly specified this method returns that, and otherwise uses Data::GUID to generate a unique binary string for use as a nonce/IV.
Uses Storable and pack to create a string out of data.
MooseX::Storage support will be added in the future.
The format itself is versioned in order to facilitate future proofing and backwards compatibility.
Note that it is not safe to call unpack_data on an untrusted string, use
thaw_tamper_proof instead (it will authenticate the data and only then
perform the potentially unsafe routines).
Available parameters are:
The cipher algorithm to use, e.g. Rijndael, Twofish etc.
The mode of operation. This can be real (cbc, cfb, ctr, ofb,
eax) or symbolic (authenticated, block, stream).
See http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation for an explanation of this.
Used by cipher_object but accepts additional parameters:
The nonce is a value that should be unique in order to protect against replay attacks. It also ensures that the same plain text with the same key will produce different ciphertexts.
The nonce is not included in the output ciphertext. See
authenticated_encrypt_string for a convenience method that does include the
nonce.
This is additional data to authenticate but not encrypt.
See Crypt::EAX for more details.
The header will not be included in the output ciphertext.
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 Digest.
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 hmac and cmac are 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).
URI::Escape based encoding.
Requires MIME::Base64.
The wrapped variant will introduce line breaks as per the MIME::Base64
default>.
Requires MIME::Base64.
Implements the Base64 for URIs. See http://en.wikipedia.org/wiki/Base64#URL_Applications.
Requires MIME::Base32.
(note- unlike MIME::Base32 this is case insensitive).
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:
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.
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.
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.
Crypt::Rijndael 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.
MIME::Base32 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.
The nonce/IV to use for cipher modes that require it.
Defaults to the empty string, but note that some methods will generate a nonce
for you (e.g. authenticated_encrypt_string) if none was provided.
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.
darcs send to commit
changes.
| Crypt-Util documentation | view source | Contained in the Crypt-Util distribution. |