Crypt::NaCl::Sodium::generichash - Generic hashing (Blake2b)
version 1.0.8.0
use Crypt::NaCl::Sodium qw( :utils ); my $crypto_generichash = Crypt::NaCl::Sodium->generichash(); # generate secret key my $key = $crypto_generichash->keygen(); # list of files for which we are computing the checksums my @files = ...; for my $file ( @files ) { # file name checksum my $filename_hash = $crypto_generichash->mac($file, key => $key, bytes => 32 ); # using multi-part API my $stream = $crypto_generichash->init( key => $key, bytes => 64 ); open(my $fh, $file) or die; while ( sysread($fh, my $buf, 4096) ) { # add the chunk of data $stream->update( $buf ); } close($fh); # calculate the final checksum my $checksum = $stream->final(); }
Compute a fixed-length fingerprint for an arbitrary long message. crypto_generichash supports multi-part API and the use of keys of variable length.
crypto_generichash
A message will always have the same fingerprint for a given key, but different keys used to hash the same message are very likely to produce distinct fingerprints.
If the key is used it should remain secret.
The crypto_generichash provides better and faster alternative to other hashing functions like MD5 and SHA-1/2/3.
MD5
SHA-1/2/3
my $key = $crypto_generichash->keygen( $keybytes );
Helper method to generate a random key to be used by $crypto_generichash.
$crypto_generichash
The value of the $keybytes argument can by any value between "KEYBYTES_MIN" (included) and "KEYBYTES_MAX" (included).
$keybytes
The default value of $keybytes is "KEYBYTES".
The length of the $key equals the value of $keybytes.
$key
NOTE: keep the key confidential.
Returns Data::BytesLocker object.
my $mac = $crypto_generichash->mac( $msg, key => $key, bytes => $bytes );
Computes the MAC of the $msg using (optional) $key. The value of optional $bytes argument specifies the length of the computed $mac.
$msg
$bytes
$mac
The value of the $bytes argument can by any value between "BYTES_MIN" (included) and "BYTES_MAX" (included).
The default value of $bytes is "BYTES".
The length of the $mac equals the value of $bytes.
Multi-part computation is also supported.
my $ctx = $crypto_generichash->init( key => $key, bytes => $bytes ); $ctx->update( $msgX ); $ctx->update( $msgY )->update( $msgZ, ... ); my $mac = $ctx->final( bytes => $bytes );
my $ctx = $crypto_generichash->mac( key => $key, bytes => $bytes );
Creates a context for multi-part computation using (optional) $key. The value of optional $bytes argument specifies the length of the final hash.
Returns Crypt::NaCl::Sodium::generichash::stream object which encapsulates the computation state of the algorithm.
Crypt::NaCl::Sodium::generichash::stream
while ( <> ) { $ctx->update( $_ ); print "Line: $.: ", $ctx->clone->final->to_hex, "\n"; }
Returns a copy of $ctx object, that contains the current computation state.
$ctx
$ctx->update( $msg, ... );
Appends its arguments to the message for which the MAC is being calculated.
Returns the $ctx object itself.
my $mac = $ctx->final( bytes => $bytes );
Computes the final MAC of the input data. The value of optional $bytes argument specifies the output length of the final output.
The default value of $bytes equals to the value of $bytes of the "init" parameter.
my $key_length = $crypto_generichash->KEYBYTES;
Returns the default length of key.
my $key_min_length = $crypto_generichash->KEYBYTES_MIN;
Returns the minimum length of key.
my $key_max_length = $crypto_generichash->KEYBYTES_MAX;
Returns the maximum length of key.
my $mac_length = $crypto_generichash->BYTES;
Returns the default length of MAC.
my $mac_min_length = $crypto_generichash->BYTES_MIN;
Returns the minimum length of MAC.
my $mac_max_length = $crypto_generichash->BYTES_MAX;
Returns the maximum length of MAC.
crypto_generichash uses Blake2b hash function which is an improved version of SHA-3 finalist BLAKE. Like SHA-3, Blake2b offers highest security, yet is fast as MD5 on 64-bit platforms and requires 33% less RAM then SHA-2 or SHA-3 on low-end systems. The core algorithm of Blake2b is derived from ChaCha stream cipher.
Data::BytesLocker - guarded data storage
BLAKE2 - fast secure hashing
Alex J. G. Burzyński <ajgb@cpan.org>
This software is copyright (c) 2015 by Alex J. G. Burzyński <ajgb@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Crypt::NaCl::Sodium, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Crypt::NaCl::Sodium
CPAN shell
perl -MCPAN -e shell install Crypt::NaCl::Sodium
For more information on module installation, please visit the detailed CPAN module installation guide.