| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | #made by: KorG | ||||||||||||||||||||||||||||
| 3 | # vim: sw=4 ts=4 et cc=79 : | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Math::Revhash; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 6 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 15 | 6 | $VERSION =~ tr/_//d; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | our @EXPORT_OK = qw( revhash revunhash ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Fast hashing is based on reverse modulo operations. | ||||||||||||||||||||||||||||
| 20 | # Given HASH = NUMBER * A % C, then NUMBER = HASH * B % C in case of B is | ||||||||||||||||||||||||||||
| 21 | # a modular inverse of A. | ||||||||||||||||||||||||||||
| 22 | # To avoid hash collisions A should be a primary number. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Pre-defined $A and $B for certain lengths | ||||||||||||||||||||||||||||
| 25 | 6 | my $AB = { | |||||||||||||||||||||||||||
| 26 | 1 => [ 103, 7 ], | ||||||||||||||||||||||||||||
| 27 | 2 => [ 929, 69 ], | ||||||||||||||||||||||||||||
| 28 | 3 => [ 1619, 979 ], | ||||||||||||||||||||||||||||
| 29 | 4 => [ 17027, 9963 ], | ||||||||||||||||||||||||||||
| 30 | 5 => [ 88651, 58851 ], | ||||||||||||||||||||||||||||
| 31 | 6 => [ 894407, 991543 ], | ||||||||||||||||||||||||||||
| 32 | 7 => [ 16006519, 8315079 ], | ||||||||||||||||||||||||||||
| 33 | 8 => [ 130067887, 91167823 ], | ||||||||||||||||||||||||||||
| 34 | 9 => [ 2700655597, 882024933 ], | ||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # See UNSAFE MODE | ||||||||||||||||||||||||||||
| 38 | 6 | our $UNSAFE = 0; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Parse and prepare arguments | ||||||||||||||||||||||||||||
| 41 | # This function is used by revhash, revunhash and new subroutines | ||||||||||||||||||||||||||||
| 42 | 224450 | ▶ | sub _argsparse { | ||||||||||||||||||||||||||
|
Subroutine
_argsparse: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||
| 43 | 224450 | my ($data, $len, $A, $B, $C) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 224450 | ▶ | croak "data not defined" unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 224449 | ▶ | croak "Invalid length specified" unless defined $len and $len > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $len and $len > 0
MC/DC100%2/2
defined $len and $len > 0
defined $len
$len > 0
Truth table100%3/3
A and B
A= defined $lenB= $len > 0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 224446 | ▶ | if (defined $C) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 2215 | ▶ | croak "Hash C value is invalid" unless $C > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 222231 | $C = 10 ** $len; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 224445 | ▶ | croak "data ($data) is out of range" unless $data > 0 and $data < $C; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data > 0 and $data < $C
MC/DC100%2/2
$data > 0 and $data < $C
$data > 0
$data < $C
Truth table100%3/3
A and B
A= $data > 0B= $data < $C
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 224442 | ▶ | if (defined $A) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 2217 | ▶ | croak "Hash A value is invalid" unless $A > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||||
| 59 | 222225 | $A = $AB->{$len}->[0]; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 224441 | ▶ | croak "Hash A value is undefined" unless defined $A; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 224439 | ▶ | if (defined $B) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 2215 | ▶ | croak "Hash B value is invalid" unless $B > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | 222224 | $B = $AB->{$len}->[1]; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 224438 | ▶ | $B = Math::BigInt->new($A)->bmodinv($C) unless defined $B; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 224438 | ▶ | croak "Invalid B value for such length and A" if Math::BigInt->is_nan($B); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 224437 | return ($data, $len, $A, $B, $C); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Calculate hash of number | ||||||||||||||||||||||||||||
| 75 | # args: $number, $length, $A, $B, $C | ||||||||||||||||||||||||||||
| 76 | 122223 | ▶ | sub revhash { | ||||||||||||||||||||||||||
|
Subroutine
revhash: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 122223 | ▶ | @_ = _argsparse @_ unless $UNSAFE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 122211 | sprintf "%0$_[1]d", $_[0] * $_[2] % $_[4]; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Calculate original number of hash | ||||||||||||||||||||||||||||
| 83 | # args: $hash, $length, $A, $B, $C | ||||||||||||||||||||||||||||
| 84 | 122211 | ▶ | sub revunhash { | ||||||||||||||||||||||||||
|
Subroutine
revunhash: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 122211 | ▶ | @_ = _argsparse @_ unless $UNSAFE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 122211 | $_[0] * $_[3] % $_[4]; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # OO alias | ||||||||||||||||||||||||||||
| 91 | 11106 | ▶ | sub hash { @_ = ($_[1], @{$_[0]}); goto &revhash } | ||||||||||||||||||||||||||
|
Subroutine
hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # OO alias | ||||||||||||||||||||||||||||
| 94 | 11106 | ▶ | sub unhash { @_ = ($_[1], @{$_[0]}); goto &revunhash } | ||||||||||||||||||||||||||
|
Subroutine
unhash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # OO ctor | ||||||||||||||||||||||||||||
| 97 | # args: $class, $length, $A, $B, $C | ||||||||||||||||||||||||||||
| 98 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 5 | my $obj; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 5 | (undef, @{$obj}) = _argsparse(1, @_[1..4]); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | bless $obj, $_[0]; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | 1; # End of Math::Revhash | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||