NAME

Crypt::Rhash - Compute hash sums and magnet links

SYNOPSIS

use Crypt::Rhash;

my $msg = "a message text";
print "MD5 = " . Crypt::Rhash::msg(RHASH_MD5, $msg) . "\n";

# Calculate two hash functions simultaneously
my $r = Crypt::Rhash->new(RHASH_MD5 | RHASH_SHA1);
$r->update("a message text")->update(" another message");
print  "MD5  = " . $r->hash(RHASH_MD5) . "\n";
print  "SHA1 = " . $r->hash(RHASH_SHA1) . "\n";

DESCRIPTION

Crypt::Rhash module is an object-oriented interface to the LibRHash library, allowing simultaneous calculation of several hash functions for a file or a text message.

Resulting hash digest can be obtained in hexadecimal, BASE32, BASE64, raw binary format or as a magnet link.

SUPPORTED ALGORITHMS

The module supports the following hashing algorithms: CRC32, MD4, MD5, SHA1, SHA256, SHA512, SHA3, AICH, ED2K, Tiger, DC++ TTH, BitTorrent BTIH, GOST R 34.11-94, RIPEMD-160, HAS-160, EDON-R 256/512, Whirlpool and Snefru-128/256.

CONSTRUCTOR

Creates and returns new Crypt::Rhash object.

my $r = Crypt::Rhash->new($hash_id);
my $p = new Crypt::Rhash($hash_id); # alternative way to call the constructor

The $hash_id parameter can be union (via bitwise OR) of any of the following bit-flags:

RHASH_CRC32,
RHASH_MD4,
RHASH_MD5,
RHASH_SHA1,
RHASH_TIGER,
RHASH_TTH,
RHASH_BTIH,
RHASH_ED2K,
RHASH_AICH,
RHASH_WHIRLPOOL,
RHASH_RIPEMD160,
RHASH_GOST,
RHASH_GOST_CRYPTOPRO,
RHASH_HAS160,
RHASH_SNEFRU128,
RHASH_SNEFRU256,
RHASH_SHA224,
RHASH_SHA256,
RHASH_SHA384,
RHASH_SHA512,
RHASH_SHA3_224,
RHASH_SHA3_256,
RHASH_SHA3_384,
RHASH_SHA3_512,
RHASH_EDONR256,
RHASH_EDONR512

Also the RHASH_ALL bit mask is the union of all listed bit-flags. So the object created via Crypt::Rhash->new(RHASH_ALL) calculates all supported hash functions for the same data.

COMPUTING HASHES

$rhash->update( $msg )

Calculates hashes of the $msg string. The method can be called repeatedly with chunks of the message to be hashed. It returns the $rhash object itself allowing the following construct:

$rhash = Crypt::Rhash->new(RHASH_MD5)->update( $chunk1 )->update( $chunk2 );
$rhash->update_file( $file_path, $start, $size )
$rhash->update_fd( $fd, $start, $size )

Calculate a hash of the file (or its part) specified by $file_path or a file descriptor $fd. The update_fd method doesn't close the $fd, leaving the file position after the hashed block. The optional $start and $size specify the block of the file to hash. No error is reported if the $size is greater than the number of the unread bytes left in the file.

Returns the number of characters actually read, 0 at end of file, or undef if there was an error (in the latter case $! is also set).

use Crypt::Rhash;
my $r = new Crypt::Rhash(RHASH_SHA1);
open(my $fd, "<", "input.txt") or die "cannot open < input.txt: $!";
while ((my $n = $r->update_fd($fd, undef, 1024) != 0) {
    print "$n bytes hashed. The SHA1 hash is " . $r->final()->hash() . "\n";
    $r->reset();
}
defined($n) or die "read error for input.txt: $!";
close($fd);
$rhash->final()

Finishes calculation for all data buffered by updating methods and stops hash calculation. The function is called automatically by any of the $rhash->hash*() methods if the final() call was skipped.

$rhash->reset()

Resets the $rhash object to the initial state.

$rhash->hashed_length()

Returns the total length of the hashed message.

$rhash->hash_id()

Returns the hash mask, the $rhash object was constructed with.

FORMATING HASH VALUE

Computed hash can be formatted as a hexadecimal string (in the forward or reverse byte order), a base32/base64-encoded string or as raw binary data.

$rhash->hash( $hash_id )

Returns the hash string in the default format, which can be hexadecimal or base32. Actually the method is equivalent of

(Crypt::Rhash::is_base32($hash_id) ? $rhash->hash_base32($hash_id) :
  $rhash->hash_hex($hash_id))

If the optional $hash_id parameter is omitted or zero, then the method returns the hash for the algorithm contained in $rhash with the lowest identifier.

$rhash->hash_hex( $hash_id )

Returns the specified hash in the hexadecimal format.

use Crypt::Rhash;
my $msg = "abc";
print "MD5 = " . Crypt::Rhash->new(RHASH_MD5)->update($msg)->hash_hex() . "\n";
$rhash->hash_rhex( $hash_id )

Returns the specified hash in the hexadecimal format with reversed order of bytes. Some programs prefer to output the GOST R 34.11-94 hash in this format.

$rhash->hash_base32( $hash_id )

Returns the specified hash in the base32 format.

$rhash->hash_base64( $hash_id )

Returns the specified hash in the base64 format.

$rhash->magnet_link( $filename, $hash_mask )

Returns the magnet link containing the computed hashes, filesize, and, optionaly, $filename. The $filename (if specified) is URL-encoded, by converting special characters into the %<hexadecimal-code> form. The optional parameter $hash_mask can limit which hash values to put into the link.

STATIC METHODS

Crypt::Rhash::count()

Returns the number of supported hash algorithms

Crypt::Rhash::is_base32($hash_id)

Returns nonzero if default output format is Base32 for the hash function specified by $hash_id. Returns zero if default format is hexadecimal.

Crypt::Rhash::get_digest_size($hash_id)

Returns the size in bytes of raw binary hash of the specified hash algorithm.

Crypt::Rhash::get_hash_length($hash_id)

Returns the length of a hash string in default output format for the specified hash algorithm.

Crypt::Rhash::get_name($hash_id)

Returns the name of the specified hash algorithm.

ALTERNATIVE WAY TO COMPUTE HASH

Crypt::Rhash::msg($hash_id, $message)

Computes and returns a single hash (in its default format) of the $message by the selected hash algorithm.

use Crypt::Rhash;
print "SHA1( 'abc' ) = " . Crypt::Rhash::msg(RHASH_SHA1, "abc") . "\n";

LICENSE

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.

The Software  is distributed in the hope that it will be useful,  but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  Use  this  program  at  your  own  risk!