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_protected( $data ); my $verified = $util->thaw_tamper_protected( $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 Crypt::Util deployments on other machines (where different Crypt/Digest modules are available, etc) you should use disable_fallback.
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.
# FIXME # missing: # process_key, default_mode, fallback_mode, fallback_mode_list =over 4
# DESCRIBE
This method accepts the following parameters:
data
The data to encrypt. If this is a reference Storable will be used to serialize the data.
encrypt
Not yet implemented.
By default this parameter is true, unless default_tamper_protect_unencrypted(), has been enabled.
default_tamper_protect_unencrypted()
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_string() may also be specified to this method.,
encrypt_string()
mac_string()
All of the parameters which may be supplied to process_key() are also available to this method.
process_key()
The following parameters may be used:
encode
Boolean. The default value is false.
encoding
alg.
key
The default value is server_key.
mode
# Describe
string
The string to be decrypt can either be supplied first, creating an odd number of arguments, or as a named parameter.
The following arguments may be specified:
literal_key
This disables mungung.
key_size
Can be used to force a key size, even if the cipher specifies another size.
cipher
Used to determine the key size.
Available parameters are:
# Description.
Return an object using Crypt::CBC.
The following arguments are available:
digest
The string to be digested can either be supplied first, creating an odd number of arguments, or as a named parameter.
The following parameters are accepted:
hash
A string containing the hash to verify.
The digested string.
fatal
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.
digest_string()
params: digest
Returns an object using Digest.
The encoding may be a symbolic type (uri, printable) or a concrete type (none, hex, base64, base32).
XXX emac, hmac, etc wrapper?
The following arguments are allowed:
mac
The MAC string to verify.
type
One of 'digest' or 'cipher' (hmac or cmac).
In addition, all the parameters which can be supplied to hmac_digest_string() are also available to this method.
hmac_digest_string()
cmac, emac
with odd args the firstr is treated as the string
A fairly entropic random string, suitable for digesting.
The result is the concatenation of several pseudorandom numbers.
This is a good enough value for e.g. session IDs.
The following parameters are available:
Expects bool or algorithm. Unless disabled, the string will be digested with the default algorithm.
Expects bool or alg.
Available arguments are:
Expects bool or alg. The default is false.
bytes
Expects a number; the default is 32.
might not be supported (tries /dev/random and/or the OpenSSL bindings)
encoding into a URI safe string
# "default" is there to be overridden by configs, if it returns nothing fallback will be called # "fallback" is for when nothing is configured -- the class's default
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.
find the first from fallback_cipher_list
qw/Rijndael Serpent Twofish Blowfish RC6 RC5/
qw/SHA-1 SHA-256 RIPEMD160 Whirlpool MD5 Haval256/
"hex"
"base32", "hex"
"uri_base64" # XXX make this uri_escape?
"base64"
Digest, Crypt::CBC, Crypt::CFB, Lhttp://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.
darcs send
Yuval Kogman, <nothingmuch@woobling.org>
Copyright 2006 by Yuval Kogman <nothingmuch@woobling.org>
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.
1 POD Error
The following errors were encountered while parsing the POD:
'=item' outside of any '=over'
To install Crypt::Util, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Crypt::Util
CPAN shell
perl -MCPAN -e shell install Crypt::Util
For more information on module installation, please visit the detailed CPAN module installation guide.