Crypt::Util(3pm) | User Contributed Perl Documentation | Crypt::Util(3pm) |
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.
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.
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.
* encrypt
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.
* data
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.
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).
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.
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).
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.
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.
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.
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 encoding may be a symbolic type (uri, printable) or a concrete type (none, hex, base64, base32).
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.
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 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.
The "wrapped" variant will introduce line breaks as per the MIME::Base64 default>.
Implements the Base64 for URIs. See <http://en.wikipedia.org/wiki/Base64#URL_Applications>.
(note- unlike MIME::Base32 this is case insensitive).
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.
WWW::SchneierFacts
Streams should also be able to used via a simple push api.
Digest, Crypt::CBC, Crypt::CFB, <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-2008 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.
Hey! The above document had some coding errors, which are explained below:
2022-06-12 | perl v5.34.0 |