Crypt::Util(3pm)

πŸ“¦
version 0.11 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Crypt::Util (this is the Crypt::Util(3pm) page, straight outta User Contributed Perl Documentation) β€” a lightweight Crypt/Digest convenience API. That's it, that's the whole vibe. ✨


πŸŽ€ SYNOPSIS πŸŽ€

Bestie, look at how cute this is:

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

πŸ¦‹ DESCRIPTION πŸ¦‹

So this module hands you an easy, intuitive and forgiving API for wielding crypto-fu. πŸ˜ŒπŸ”

The API is literally designed as a cascade, where the rich fancy features are built on top of simpler ones. Which means β€” and this is the slay part β€” the option processing is uniform throughout, and the behaviors are generally predictable. No chaos, just vibes and consistency. πŸ’…

Not me needing you to know this though: Crypt::Util doesn't do any crypto on its own. It delegates the actual work to the various other crypto modules on the CPAN. Crypt::Util merely wraps those modules, giving you uniform parameters and then building on top of their polymorphism with higher level features. She's a wrapper, not a cryptographer. 🫢

πŸ‘‘ Priorities πŸ‘‘

🌸 Ease of use 🌸

This module is designed to have an easy API so you can do easy β€” but responsible β€” use of the more low level Crypt:: and Digest:: modules on CPAN. So if you've got patches that improve ease-of-use? Very welcome, bestie, send them. πŸ’Œ

🌈 Pluggability 🌈

Dependency hell gets avoided using a fallback mechanism that tries to choose an algorithm based on an overridable list. Iconic behavior honestly.

For "simple" use: install Crypt::Util and your favourite digest, cipher and cipher mode (CBC, CFB, etc). Done. ✨

And to ensure predictable behavior, the fallback behavior can be disabled as necessary. You're in charge, queen. πŸ‘‘


πŸ’œ 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. No guessing games. 🎯

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. So if you never leave the house, you're fine either way. 😭


✨ EXPORTED API ✨

NOTE: nothing is exported by default. Zero. None. You gotta ask. πŸ™ˆ

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 β€” instead, every importer gets its own instance. Personal instance, personal vibes. πŸŽ€

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. Two packages, two secrets, no crossover. πŸ’€

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 πŸ”€
  • "params" β€” gives you functions for handling default values πŸŽ›οΈ

more parts coming bestie, don't go anywhere πŸ¦‹

πŸ’– METHODS πŸ’–

okay bestie, this is where it gets juicy β€” every method you can actually call. we're going method by method, nothing skipped, periodt. 🫢


πŸ” tamper_proof( [ $data ], %params ), thaw_tamper_proof( [ $string ], %params ), tamper_proof_string( $string, %params ), thaw_tamper_proof_string( $string, %params ), aead_tamper_proof_string( [ $string ], %params ), mac_tamper_proof_string( [ $string ], %params ) πŸ”

ngl, tamper_proof is in an intermittent state right now β€” like, the data parameter's API is not completely finalized. it's giving "work in progress" and we're all just watching. πŸ™ˆ

you know what's safer? use tamper_proof_string. its API is expected to stay the same in future versions too, so it's the reliable bestie.

peep TODO for more info about the data types that'll be supported in the future.

when thawing, the authenticated_decrypt_string or verify_mac methods get used, and fatal defaults to on unless you explicitly disable it in the parameters. so yes, it will yell at you. 😭

This method accepts the following parameters:

  • encrypt β€” by default this one is true, unless default_tamper_proof_unencrypted(), has been enabled. a true value means all the parameters available to authenticated_encrypt_string() are also available to you. if you pass a negative value, MAC mode is used instead, and then the extra 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. peep pack_data for the details. ✨

if the string is encrypted, then all the parameters of encrypt_string and digest_string are also available to you.

if the string is not encrypted, then all the parameters of mac_digest_string are also available. slay. πŸ’…


🌸 encrypt_string( [ $string ], %params ), decrypt_string( [ $string ], %params ), authenticated_encrypt_string( [ $string ], %params ), authenticated_decrypt_string( [ $string ], %params ) 🌸

all of the parameters you can hand to process_key(), cipher_object and maybe_encode are also available to these methods. one big happy parameter family. 🫢

the authenticated variants make sure an authenticated encryption mode (like EAX) is used β€” that's literally their whole thing.

the following parameters may be used:

  • string β€” the string to be en/decrypted can either be supplied first, which creates an odd number of arguments, or supplied as a named parameter.
  • nonce β€” the cryptographic nonce to use. only necessary for encryption, and it'll be packed into the string as part of the message if applicable.
  • header β€” not yet supported. in the future this will include a header for AEAD (the associated data bit of AEAD).

πŸ—οΈ process_key( [ $key ], %params ) πŸ—οΈ

the following arguments may be specified:

  • literal_key β€” this disables mungung. also peep default_use_literal_key.
  • key_size β€” can be used to force a key size, even if the cipher specifies another size. if not specified, the key size chosen will depend
  • cipher β€” used to determine the key size.

🎲 process_nonce( [ $nonce ], %params ) 🎲

if a nonce is explicitly specified, this method returns that. otherwise it uses Data::GUID to generate a unique binary string to use as a nonce/IV. basically it'll make one for you, no stress. ✨


πŸ“¦ pack_data( [ $data ], %params ), unpack_data( [ $data ], %params ) πŸ“¦

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, so that future proofing and backwards compatibility are actually possible. iconic. πŸ‘‘

⚠️ and now the important bit, bestie: it is not safe to call unpack_data on an untrusted string! use thaw_tamper_proof instead β€” that one authenticates the data and only then performs the potentially unsafe routines. do not skip this. i'm serious. πŸ’€

🧊 cipher_object( %params ) 🧊

available parameters are:


🧚 cipher_object_eax( %params ) 🧚

used by cipher_object but it accepts additional parameters:

  • nonce β€” the nonce is a value that should be unique in order to protect against replay attacks. it also makes sure that the same plain text with the same key will produce different ciphertexts. the nonce is not included in the output ciphertext. peep authenticated_encrypt_string for a convenience method that does include the nonce.
  • header β€” 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.

🧬 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:

  • 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 ) βœ…

delegates to digest_object. all parameters which can be used by digest_object may also be used here.

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 ) πŸ”Ž

  • digest β€” the digest algorithm to use.

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 ) πŸ“

delegates to mac_object. all parameters which can be used by mac_object may also be used here.

  • string

πŸ›‘οΈ 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:

  • hash β€” 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.

πŸ’« mac_object( %params ) πŸ’«

  • mac β€” the MAC algorithm to use. currently hmac and cmac are supported.

πŸŽ€ maybe_encode, maybe_decode πŸŽ€

okay so this method has no external API but it's documented here for the sake of its shared options. it gets delegated to by the various encryption and digest methods. so it's like, backstage main character energy. πŸ’…

  • encode β€” expects a bool.
  • encoding β€” 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 (peep 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 (peep 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. chef's kiss. 😌


πŸ”’ encode_string_hex( $string ), decode_string_hex( $string ) πŸ”’

big endian hexadecimal (H* pack format).


🌐 encode_string_uri_escape( $string ), decode_string_uri_escape( $string ) 🌐

URI::Escape based encoding.


🧁 encode_string_base64( $string ), decode_string_base64( $string ), encode_string_base64_wrapped( $string ) 🧁

requires MIME::Base64.

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


πŸ¦‹ encode_string_uri_base64, decode_string_uri_base64 πŸ¦‹

requires MIME::Base64.

implements the Base64 for URIs. see http://en.wikipedia.org/wiki/Base64#URL_Applications.


⭐ encode_string_base32( $string ), decode_string_base32( $string ) ⭐

requires MIME::Base32.

(note β€” unlike MIME::Base32 this is case insensitive). yes, we're built different. πŸ’œ

πŸ’– HANDLING OF DEFAULT VALUES πŸ’–

Okay bestie, this is the part where the module gets ✨opinionated✨ about what to do when you don't tell it what to do. Buckle up.

  • disable_fallback() β€” when this is true, only the first item from the fallback list gets tried, and if it can't be loaded you get a fatal error. Turn this on if you want to guarantee portability. It's giving "I only trust one thing and I'm not sorry."

For every parameter there are several methods, where PARAMETER gets swapped out for the actual parameter name. Yes, template-style. Slay.

  • default_PARAMETER() β€” this accessor is here so you can override the default value. If you set it to undef, then fallback_PARAMETER gets consulted instead. And listen: ALL the default values are set to undef unless changed by the user. All of them. No cap.
  • fallback_PARAMETER() β€” iterates the fallback_PARAMETER_list, picking the first value that's actually usable (meaning 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. One shot, one opportunity.
  • fallback_PARAMETER_list() β€” an ordered list of values to try and use as fallbacks. fallback_PARAMETER iterates this list and chooses the first one that works. It's basically a lineup and only one of them gets the rose 🌹.

The available parameters are as follows:

  • cipher β€” the fallback list is Rijndael, Serpent, Twofish, RC6, Blowfish and RC5. So here's the lore: Crypt::Rijndael is the AES winner πŸ‘‘, the next three are AES finalists, and the last two are well known and widely used. Iconic lineup fr.
  • mode β€” the mode in which to use the cipher. The fallback list is CFB, CBC, Ctr, and OFB.
  • digest β€” the fallback list is SHA-1, SHA-256, RIPEMD160, Whirlpool, MD5, and Haval256.
  • encoding β€” the fallback list is hex (effectively no fallback). It's giving "the list is one item long, diva."
  • alphanumerical_encoding β€” the fallback list is base32 and hex. Heads up: MIME::Base32 is required for base32 encoding.
  • uri_encoding β€” the fallback list is uri_base64.
  • printable_encoding β€” the fallback list is base64.

🌸 Defaults with no fallbacks 🌸

These following parameters have a default_ method, just like described in the previous section, BUT the fallback_ methods do not apply to them. They're solo acts. No understudies. πŸ’…

  • encode β€” whether or not to encode by default (applies to digests and encryptions).
  • key β€” the key to use. Super useful when you're repeatedly encrypting, so you're not re-typing that thing over and over like a clown.
  • nonce β€” 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. So thoughtful of them 🫢.
  • use_literal_key β€” whether or not to not hash the key by default. See process_key. Yes, the double negative is real, we don't make the rules.
  • tamper_proof_unencrypted β€” whether or not tamper resistent strings are by default unencrypted (just MAC).

πŸ¦‹ Subclassing πŸ¦‹

You may safely subclass and override default_PARAMETER and fallback_PARAMETER_list to provide values from configurations. Go off, build your own defaults, we love a customizable queen ✨


πŸ”₯ TODO πŸ”₯

The wishlist. The vision board. The "one day, bestie" list:

  • Crypt::SaltedHash support
  • EMAC (maybe, the modules are not OO and require refactoring) message authentication mode
  • Bruce Schneier Fact Database <http://geekz.co.uk/lovesraymond/archive/bruce-schneier-facts>.
    • WWW::SchneierFacts
  • Entropy fetching (get N weak/strong bytes, etc) from e.g. OpenSSL bindings, /dev/*random, and EGD.
  • Additional data formats (streams/iterators, filehandles, generalized storable data/string handling for all methods, not just tamper_proof).
    • Streams should also be able to used via a simple push api. (that's their phrasing, not mine, don't come for me)
  • IV/nonce/salt support for the various cipher modes, not just EAX (CBC, CCM, GCM, etc)
  • Crypt::Rijndael can do its own cipher modes

πŸ‘‘ SEE ALSO πŸ‘‘

Digest, Crypt::CBC, Crypt::CFB, <http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation>.

Go read those too, the more you know the more you glow πŸ’–


πŸŽ€ 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 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.


πŸ’€ POD ERRORS πŸ’€

Hey! The above document had some coding errors, which are explained below:

  • Around line 1163:
You can't have =items (as at line 1167) unless the first thing after
the =over is an =item
  • Around line 1520:
You forgot a '=back' before '=head1'
  • Around line 1580:
Expected '=item *'
  • Around line 1589:
Expected '=item *'
  • Around line 1642:
=back without =over
  • Around line 1684:
You forgot a '=back' before '=head1'

Like, the doc itself has some typos and honestly? Relatable. We've all forgotten a =back before a =head1. It happens. 😭


And that's a wrap on Crypt::Util(3pm), babes πŸ’– This page came from perl v5.40.1 and it's dated 2025-08-14 β€” so if you're reading this, hi, you're in the right timeline. Go forth, encrypt responsibly, and never forget your =back. Periodt. πŸŒΈβœ¨πŸ¦‹πŸ’…

πŸ“¦ where this page comes from