Crypt::Util - A lightweight Crypt/Digest convenience API



NAME

Crypt::Util - A lightweight Crypt/Digest convenience API


SYNOPSIS

        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",
        );


DESCRIPTION

This module provides an easy, intuitive and forgiving API for wielding crypto-fu.

Priorities

Ease of use

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.

Pluggability

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.

Interoperability

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.


EXPORTED API

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.


METHODS

tamper_proof( [ $data ], %params )
thaw_tamper_proof( [ $string ], %params )
tamper_proof_string $string, %params
thaw_tamper_proof_string $string, %params

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:

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.

encrypt_string( [ $string ], %params )
decrypt_string( [ $string ], %params )

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:

process_key( $key, %params )

The following arguments may be specified:

cipher_object( %params )

Available parameters are:

digest_string( [ $string ], %params )

Delegates to digest_object. All parameters which can be used by digest_object may also be used here.

The following arguments are available:

verify_digest( %params )

Delegates to digest_object. All parameters which can be used by digest_object may also be used here.

The following parameters are accepted:

In addition, the parameters which can be supplied to digest_string() may also be supplied to this method.

digest_object( %params )

Returns an object using the Digest manpage.

encode_string( [ $string ], %params )
decode_string( [ $string ], %params )

The following parameters are accepted:

mac_digest_string( [ $string ], %param )

Delegates to mac_object. All parameters which can be used by mac_object may also be used here.

verify_mac( %params )

Delegates to mac_object. All parameters which can be used by mac_object may also be used here.

The following additional arguments are allowed:

mac_object
maybe_encode
maybe_decode

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.

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).

encode_string_alphanumerical( $string )
decode_string_alphanumerical( $string )
encode_string_uri( $string )
decode_string_uri( $string )
encode_string_printable( $string )
decode_string_printable( $string )

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.

encode_string_hex( $string )
decode_string_hex( $string )

Big endian hexadecimal (H* pack format).

encode_string_uri_escape( $string )
decode_string_uri_escape( $string )

the URI::Escape manpage based encoding.

encode_string_base64( $string )
decode_string_base64( $string )
encode_string_base64_wrapped( $string )

Requires the MIME::Base64 manpage.

The wrapped variant will introduce line breaks as per the the MIME::Base64 manpage default>.

encode_string_uri_base64
decode_string_uri_base64

Requires the MIME::Base64 manpage.

Implements the Base64 for URIs. See http://en.wikipedia.org/wiki/Base64#URL_Applications.

encode_string_base32( $string )
decode_string_base32( $string )

Requires the MIME::Base32 manpage.

(note- unlike the MIME::Base32 manpage this is case insensitive).


HANDLING OF DEFAULT VALUES

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:

Available parameters are as follows:

Defaults with no fallbacks

The following parameters have a default_ method, as described in the previous section, but the fallback_ methods are not applicable.

Subclassing

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.


TODO

Features which are currently missing but are scheduled for 0.02 or 0.03:


SEE ALSO

the Digest manpage, the Crypt::CBC manpage, the Crypt::CFB manpage, http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation.


VERSION CONTROL

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.


AUTHORS

Yuval Kogman, <nothingmuch@woobling.org> Ann Barcomb


COPYRIGHT & LICENSE

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.