Crypt-RHash

 view release on metacpan or  search on metacpan

Makefile.PL  view on Meta::CPAN

	!$inc_dir || has_librhash($inc_dir) || die "No librhash headers at: '$inc_dir'";
	$inc  = "-I$inc_dir" if $inc_dir;
	$libs = "-L$lib_dir" if $lib_dir;
	$libs .= ' -lrhash'  if $rh_type ne 'builtin';
} else {
	# set custom compile and linking flags
	$inc = $custom_inc;
	$libs = $custom_ld;
}

sub read_librhash_version($) {
	my $path = $_[0] . "/version.h";
	open my $fh, "<", $path or die "could not open $path: $!";
	chomp(my @lines = <$fh>);
	close $fh;
	foreach (@lines) {
		if (/^#define VERSION "(\d+)\.(\d+)\.(\d+)"/) {
			return sprintf("0x%02x%02x%02x%02x", $1, $2, $3, 0);
		}
	}
	return '0';
}

# copy and rename *.c files by prepending underscore '_'
sub copy_c_files($) {
	my $from_dir = $_[0];
	my @result = ();
	(opendir my($dh), $from_dir) or die "Can't open $from_dir: $!";
	my @files = grep { /(?<!\Atest_lib)\.c$/ } readdir $dh;
	closedir $dh;
	for (@files) {
		my ($from, $to) = ("$from_dir/$_", "_$_");
		push @result, $to;

		my ($df, $dt) = ((stat($from))[9], (stat($to))[9]);

Rhash.pm  view on Meta::CPAN

use constant RHASH_SNEFRU128 => 0x8000000;
use constant RHASH_SNEFRU256 => 0x10000000;
use constant RHASH_BLAKE2S   => 0x20000000;
use constant RHASH_BLAKE2B   => 0x40000000;
use constant RHASH_ALL       => 0x7FFFFFFF;

##############################################################################
# Rhash class methods

# Rhash object constructor
sub new($$@)
{
	my ($class, @hash_ids) = @_;

	# validate @hash_ids to be an array of hash identifiers
	scalar(@_) > 0 or die "hash_id not specified\n";
	for my $id (@hash_ids) {
		if (ref($id) || !scalar($id) || (scalar($id) & RHASH_ALL) != $id) {
			die "bad hash_id = " . scalar($id) . " " . ref($id) . "\n";
		}
	}

Rhash.pm  view on Meta::CPAN

		}
	}
	my $context = rhash_init_multi_wrapper(\@hash_ids) or return undef;
	my $self = {
		context => $context,
	};
	return bless $self, $class;
}

# 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;
	}

Rhash.pm  view on Meta::CPAN

	} 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});
}

##############################################################################
# Message digest 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_wrapper($self->{context}, $hash_id, $print_flags);
}

sub hash_base32($;$)
{
	hash($_[0], $_[1], RHPR_BASE32);
}

sub hash_base64($;$)
{
	hash($_[0], $_[1], RHPR_BASE64);
}

sub hash_hex($;$)
{
	hash($_[0], $_[1], RHPR_HEX);
}

sub hash_rhex($;$)
{
	hash($_[0], $_[1], RHPR_HEX | RHPR_REVERSE);
}

sub hash_raw($;$)
{
	hash($_[0], $_[1], RHPR_RAW);
}

sub magnet_link($;$@)
{
	my ($self, $filename, @hash_ids) = @_;
	my $hash_mask = 0;
	if (scalar(@hash_ids)) {
		for my $id (@hash_ids) {
			if (ref($id) || !scalar($id) || (scalar($id) & RHASH_ALL) != $id) {
				die "bad hash_id = " . scalar($id) . " " . ref($id) . "\n";
			}
			$hash_mask = $hash_mask | scalar($id);
		}

Rhash.pm  view on Meta::CPAN

	die "no arguments specified to $field()\n" if !@_;
	die "the $field() argument is undefined\n" if !defined $self;

	($type = ref($self)) && $type eq $pkg || die "the $field() argument is not a $pkg reference\n";
	my $text = (exists $self->{$field} ? "is not accessible" : "does not exist");
	die "the method $field() $text in the class $pkg\n";
}

# static functions

sub msg($$)
{
	my ($hash_id, $msg) = @_;
	my $raw = rhash_msg_wrapper($hash_id, $msg); # get a binary message digest
	return (is_base32($hash_id) ? raw2base32($raw) : raw2hex($raw));
}

1;
__END__
# Below is Rhash module documentation in the standard POD format



( run in 0.263 second using v1.01-cache-2.11-cpan-1f129e94a17 )