The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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

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

METHODS

# FIXME # missing: # process_key, default_mode, fallback_mode, fallback_mode_list =over 4

tamper_protected( [ $data ], %params )

# DESCRIBE

thaw_tamper_protected( [ $string ], %params )

# DESCRIBE

tamper_unprotected( [ $string ], %params )

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.

    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( [ $string ], %params )

All of the parameters which may be supplied to process_key() are also available to this method.

decrypt_string( [ $string ], %params )

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.

process_key( $key, %params )

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.

cipher_object( %params )

Available parameters are:

  • cipher

    # Description.

  • mode

    # Description.

Return an object using Crypt::CBC.

digest_string( [ $string ], %params )

The following arguments are available:

  • digest

    alg.

  • string

    The string to be digested can either be supplied first, creating an odd number of arguments, or as a named parameter.

verify_digest( %params )

The following parameters are accepted:

  • hash

    A string containing the hash to verify.

  • string

    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_object( %params )

params: digest

Returns an object using Digest.

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

The following parameters are accepted:

  • encoding

    The encoding may be a symbolic type (uri, printable) or a concrete type (none, hex, base64, base32).

mac_digest_string( [ $string ], %param )
verify_mac( %params )

XXX emac, hmac, etc wrapper?

The following arguments are allowed:

  • mac

    The MAC string to verify.

  • string

    The digested string.

  • type

    One of 'digest' or 'cipher' (hmac or cmac).

  • fatal

    If true, errors will be fatal. The default is false, which means that failures will return undef.

hmac_digest_string( %params )
verify_hmac( %params )

The following arguments are allowed:

  • mac

    The MAC string to verify.

  • string

    The digested string.

  • fatal

    If true, errors will be fatal. The default is false, which means that failures will return undef.

In addition, all the parameters which can be supplied to hmac_digest_string() are also available to this method.

cmac_digest_string( %params )
verify_cmac()

cmac, emac

with odd args the firstr is treated as the string

weak_random_string( %params )

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:

  • digest

    Expects bool or algorithm. Unless disabled, the string will be digested with the default algorithm.

  • encode

    Expects bool or alg.

strong_random_string( %params )

Available arguments are:

  • digest

    Expects bool or alg. The default is false.

  • encode

    Expects bool or alg.

  • bytes

    Expects a number; the default is 32.

might not be supported (tries /dev/random and/or the OpenSSL bindings)

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

encoding into a URI safe string

encode_string_printable( $string )
decode_string_printable( $string )
encode_string_hex( $string )
decode_string_hex( $string )
encode_string_uri_escape( $string )
decode_string_uri_escape( $string )
encode_string_base64( $string )
decode_string_base64( $string )
encode_string_base32( $string )
decode_string_base32( $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

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.

default_key()
default_cipher()
fallback_cipher()

find the first from fallback_cipher_list

fallback_cipher_list()

qw/Rijndael Serpent Twofish Blowfish RC6 RC5/

default_digest()
fallback_digest()
fallback_digest_list()

qw/SHA-1 SHA-256 RIPEMD160 Whirlpool MD5 Haval256/

default_encoding()
fallback_encoding()
fallback_encoding_list()

"hex"

default_alphanumerical_encoding()
fallback_alphanumerical_encoding()
fallback_alphanumerical_encoding_list()

"base32", "hex"

default_uri_encoding()
fallback_uri_encoding()
fallback_uri_encoding_list()

"uri_base64" # XXX make this uri_escape?

default_printable_encoding()
fallback_printable_encoding()

"base64"

SEE ALSO

Digest, Crypt::CBC, Crypt::CFB, Lhttp://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>

COPYRIGHT & LICENSE

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:

Around line 957:

'=item' outside of any '=over'