/usr/share/perl5/Digest/MultiHash.pm is in libcrypt-util-perl 0.11-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | #!/usr/bin/perl
package Digest::MultiHash;
use Moose;
extends our @ISA, qw(Digest::base);
use Carp qw/croak/;
use Digest;
use Digest::MoreFallbacks;
use Scalar::Util qw/blessed/;
use namespace::clean -except => [qw(meta)];
has width => (
isa => "Int",
is => "rw",
);
has hashes => (
isa => "ArrayRef",
is => "ro",
required => 1,
default => sub { [qw(SHA-1)] },
);
has _digest_objects => (
isa => "ArrayRef",
is => "ro",
lazy_build => 1,
);
sub BUILD {
shift->_digest_objects; # force building
}
sub _call {
my ( $self, $method, @args ) = @_;
map { $_->$method( @args ) } @{ $self->_digest_objects };
}
sub _build__digest_objects {
my $self = shift;
my @digests = map {
blessed($_)
? $_
: Digest->new(
((ref($_)||'') eq "ARRAY")
? @$_
: $_
)
} @{ $self->hashes };
die "No digest module specified" unless @digests;
return \@digests;
}
# MooseX::Clone
sub clone {
my $self = shift;
$self->new(
width => $self->width,
hashes => $self->hashes,
_digest_objects => [ $self->_call("clone") ],
);
}
sub add {
my ( $self, @args ) = @_;
$self->_call("add", @args);
}
sub digest {
my $self = shift;
my @digests = $self->_call("digest");
my $width = $self->width || length($digests[0]);
my $concat = join "", @digests;
die "Chosen hashes are insufficient for desired width" if length($concat) < $width;
my ( $buf, @pieces ) = unpack "(a$width)*", $concat;
$buf ^= $_ for @pieces;
return $buf;
}
__PACKAGE__;
__END__
=pod
=head1 NAME
Digest::MultiHash - XOR based, variable width multiplexing of hashes (a
generalized Digest::SV1).
=head1 SYNOPSIS
use Digest::MultiHash;
my $d = Digest::Multihash->new(
width => 16, # bytes
hashs => ["SHA-512", "Whirlpool"], # see below for arbitrary arguments
);
$d->add($data);
print $d->hexdigest;
=head1 DESCRIPTION
This class inherits from L<Digest::base>, and provides generalized digest
multiplexing.
It will multiplex all calls to C<add> to all of it's sub digest objects.
Likewise, when the final digest is extracted the digests will be extracted and
then XOR'd over eachother according to C<width>.
C<width> will default to the width of the first hash if unspecified.
C<hashes> defaults to C<SHA-1> for compatibility reasons.
This module is useful for generating keys from passphrases, by supplying the
desired width and simply making sure there is enough data from the combined
hashes.
=head1 METHODS
See L<Digest> for the complete API. This module inherits from L<Digest::base>.
=over 4
=item new
This methods accepts a hash reference or an even sized list of parameters named
according to the methods.
=item add
=item digest
Compute the hash by calling C<digest> on all of the subhashes, splitting the
result up into C<width> sized chunk, and then XORing these together.
If the result is not aligned on C<width> the result will not be truncated. The
shorter string will still be XOR'd with the hash, even if this only affects
part of the result.
If there are not at least C<width> bytes of data in the output of the combined
hashes an error is thrown.
=item clone
Clones the hash.
=item hashes
Get the array of hashes to use. Array values in this will be dereferenced
before the call to L<Digest/new> to allow passing of arbitrary arguments.
Blessed objects (of any class) will be used verbatim.
The list of hashes cannot be changed after construction.
=item width
Get/set the byte-width to use.
=back
=head1 SEE ALSO
L<Digest>, L<Digest::SV1>, L<Digest::SHA1>
=cut
|