Crypt-Rhash
view release on metacpan or search on metacpan
use constant RHASH_MD5 => 0x04;
use constant RHASH_SHA1 => 0x08;
use constant RHASH_TIGER => 0x10;
use constant RHASH_TTH => 0x20;
use constant RHASH_BTIH => 0x40;
use constant RHASH_ED2K => 0x80;
use constant RHASH_AICH => 0x100;
use constant RHASH_WHIRLPOOL => 0x200;
use constant RHASH_RIPEMD160 => 0x400;
use constant RHASH_GOST94 => 0x800;
use constant RHASH_GOST94_CRYPTOPRO => 0x1000;
use constant RHASH_HAS160 => 0x2000;
use constant RHASH_GOST12_256 => 0x4000;
use constant RHASH_GOST12_512 => 0x8000;
use constant RHASH_SHA224 => 0x10000;
use constant RHASH_SHA256 => 0x20000;
use constant RHASH_SHA384 => 0x40000;
use constant RHASH_SHA512 => 0x80000;
use constant RHASH_EDONR256 => 0x100000;
use constant RHASH_EDONR512 => 0x200000;
use constant RHASH_SHA3_224 => 0x0400000;
use constant RHASH_SHA3_256 => 0x0800000;
use constant RHASH_SHA3_384 => 0x1000000;
use constant RHASH_SHA3_512 => 0x2000000;
use constant RHASH_CRC32C => 0x4000000;
use constant RHASH_SNEFRU128 => 0x8000000;
use constant RHASH_SNEFRU256 => 0x10000000;
use constant RHASH_ALL => 0x1FFFFFFF;
##############################################################################
# Rhash class methods
# Rhash object constructor
sub new($$@)
{
my $hash_id = 0;
shift;
scalar(@_) > 0 or die "hash_id not specified";
for my $id (@_) {
$hash_id |= scalar($id);
if(!scalar($id) || (scalar($id) & RHASH_ALL) != $id) {
die "bad hash_id = " . scalar($id);
}
}
my $context = rhash_init($hash_id) or return undef;
my $self = {
context => $context,
};
return bless $self;
}
# destructor
sub DESTROY($)
{
my $self = shift;
# the 'if' added as workaround for perl 'global destruction' bug
# ($self->{context} can disappear on global destruction)
rhash_free($self->{context}) if $self->{context};
}
sub update($$)
{
my $self = shift;
my $message = shift;
rhash_update($self->{context}, $message);
return $self;
}
sub update_fd($$;$$)
{
my ($self, $fd, $start, $size) = @_;
my $res = 0;
my $num = 0;
binmode($fd);
if(defined($start)) {
seek($fd, scalar($start), 0) or return undef;
}
my $data;
if(defined($size)) {
for(my $left = scalar($size); $left > 0; $left -= 8192) {
($res = read($fd, $data,
($left < 8192 ? $left : 8192))) || last;
rhash_update($self->{context}, $data);
$num += $res;
}
} else {
while( ($res = read($fd, $data, 8192)) ) {
rhash_update($self->{context}, $data);
$num += $res;
}
}
return (defined($res) ? $num : undef); # return undef on read error
}
sub update_file($$;$$)
{
my ($self, $file, $start, $size) = @_;
open(my $fd, "<", $file) or return undef;
my $res = $self->update_fd($fd, $start, $size);
close($fd);
return $res;
}
sub final($)
{
my $self = shift;
rhash_final($self->{context});
return $self;
}
sub reset($)
{
my $self = shift;
rhash_reset($self->{context});
return $self;
}
sub hashed_length($)
{
my $self = shift;
return rhash_get_hashed_length($self->{context});
}
sub hash_id($)
{
my $self = shift;
return rhash_get_hash_id($self->{context});
}
##############################################################################
# Hash formatting functions
# printing constants
use constant RHPR_DEFAULT => 0x0;
use constant RHPR_RAW => 0x1;
use constant RHPR_HEX => 0x2;
use constant RHPR_BASE32 => 0x3;
use constant RHPR_BASE64 => 0x4;
use constant RHPR_UPPERCASE => 0x8;
use constant RHPR_REVERSE => 0x10;
sub hash($;$$)
{
my $self = shift;
my $hash_id = scalar(shift) || 0;
my $print_flags = scalar(shift) || RHPR_DEFAULT;
return rhash_print($self->{context}, $hash_id, $print_flags);
}
sub hash_base32($;$)
{
hash($_[0], $_[1], RHPR_BASE32);
}
sub hash_base64($;$)
( run in 0.560 second using v1.01-cache-2.11-cpan-13bb782fe5a )