| 1 | package Hashids; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use POSIX 'ceil'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Hashids::Util ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | our $VERSION = "1.001012"; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | has salt => ( is => 'ro', default => '' ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | has minHashLength => ( | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | isa => sub { | ||||||||||||||||||||||||||||
| 16 | 26 | ▶ | croak "$_[0] must be a positive number" unless $_[0] =~ /^[0-9]+$/; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | }, | ||||||||||||||||||||||||||||
| 18 | 5 | default => 0 | |||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | has alphabet => ( | ||||||||||||||||||||||||||||
| 22 | is => 'rwp', | ||||||||||||||||||||||||||||
| 23 | isa => sub { | ||||||||||||||||||||||||||||
| 24 | 29 | ▶ | local $_ = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 25 | 29 | ▶ | croak "$_ must not have spaces" if /\s/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 28 | ▶ | croak "$_ must contain at least 16 characters" if 16 > length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 27 | my %u; | |||||||||||||||||||||||||||
| 28 | croak "$_ must contain unique characters" | ||||||||||||||||||||||||||||
| 29 | 27 | ▶ | if any { $u{$_}++ } split //; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||
| 31 | 18 | ▶ | default => sub { join '' => 'a' .. 'z', 'A' .. 'Z', 1 .. 9, 0 } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 5 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 26 | ▶ | has chars => ( is => 'rwp', init_arg => undef, default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has seps => ( | ||||||||||||||||||||||||||||
| 37 | is => 'rwp', | ||||||||||||||||||||||||||||
| 38 | init_arg => undef, | ||||||||||||||||||||||||||||
| 39 | default => sub { | ||||||||||||||||||||||||||||
| 40 | 24 | ▶ | my @seps = qw(c f h i s t u); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 24 | [ @seps, map {uc} @seps ]; | |||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | 5 | ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 26 | ▶ | has guards => ( is => 'rwp', init_arg => undef, default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | around BUILDARGS => sub { | ||||||||||||||||||||||||||||
| 48 | 29 | ▶ | my ( $orig, $class, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 29 | ▶ | unshift @args, 'salt' if @args % 2 == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 29 | $class->$orig(@args); | |||||||||||||||||||||||||||
| 52 | 5 | }; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub BUILD { | ||||||||||||||||||||||||||||
| 55 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 24 | ▶ | croak "salt must be shorter than or of equal length to alphabet" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | if length $self->salt > length $self->alphabet; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 23 | my @alphabet = split // => $self->alphabet; | |||||||||||||||||||||||||||
| 61 | 23 | my ( @seps, @guards ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 23 | my $sepDiv = 3.5; | |||||||||||||||||||||||||||
| 64 | 23 | my $guardDiv = 12; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # seps should contain only chars present in alphabet; | ||||||||||||||||||||||||||||
| 67 | # alphabet should not contain seps | ||||||||||||||||||||||||||||
| 68 | 23 | for my $sep ( @{ $self->seps } ) { | |||||||||||||||||||||||||||
| 69 | 322 | ▶ | push @seps, $sep if any {/$sep/} @alphabet; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 322 | @alphabet = grep { !/$sep/ } @alphabet; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 23 | @seps = consistent_shuffle( \@seps, $self->salt ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 23 | ▶ | if ( !@seps || ( @alphabet / @seps ) > $sepDiv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not @seps or @alphabet / @seps > $sepDiv
MC/DC50%1/2
not @seps or @alphabet / @seps > $sepDiv
not @seps
@alphabet / @seps > $sepDiv
Truth table66%2/3
not(A) or B
A= not @sepsB= @alphabet / @seps > $sepDiv
| |||||||||||||||||||||||||||||
| 76 | 1 | my $sepsLength = ceil( @alphabet / $sepDiv ); | |||||||||||||||||||||||||||
| 77 | 1 | ▶ | $sepsLength++ if $sepsLength == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if ( $sepsLength > @seps ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | push @seps => splice @alphabet, 0, $sepsLength - @seps; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 23 | @alphabet = consistent_shuffle( \@alphabet, $self->salt ); | |||||||||||||||||||||||||||
| 84 | 23 | my $guardCount = ceil( @alphabet / $guardDiv ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | @guards | ||||||||||||||||||||||||||||
| 87 | 23 | ▶ | = @alphabet < 3 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | ? splice @seps, 0, $guardCount | ||||||||||||||||||||||||||||
| 89 | : splice @alphabet, 0, $guardCount; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 23 | $self->_set_chars( \@alphabet ); | |||||||||||||||||||||||||||
| 92 | 23 | $self->_set_seps( \@seps ); | |||||||||||||||||||||||||||
| 93 | 23 | $self->_set_guards( \@guards ); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub encode_hex { | ||||||||||||||||||||||||||||
| 97 | 2 | ▶ | my ( $self, $str ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encode_hex: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | return '' unless $str =~ /^[0-9a-fA-F]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | my @num; | |||||||||||||||||||||||||||
| 102 | 1 | ▶ | push @num, '1' . substr $str, 0, 11, '' while $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | @num = map { bignum(0)->from_hex($_) } @num; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | $self->encode(@num); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub decode_hex { | ||||||||||||||||||||||||||||
| 110 | 2 | ▶ | my ( $self, $hash ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
decode_hex: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | my @res = $self->decode($hash); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2 | ▶ | @res ? join '' => map { substr( bignum($_)->to_hex, 1 ) } @res : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub encrypt { | ||||||||||||||||||||||||||||
| 118 | 4 | ▶ | shift->encode(@_); | ||||||||||||||||||||||||||
|
Subroutine
encrypt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub decrypt { | ||||||||||||||||||||||||||||
| 122 | 2 | ▶ | shift->decode(shift); | ||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub encode { | ||||||||||||||||||||||||||||
| 126 | 2081 | ▶ | my ( $self, @num ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2081 | ▶ | return '' unless @num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 2079 | ▶ | map { return '' unless defined and /^[0-9]+$/ } @num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_ and /^[0-9]+$/
MC/DC100%2/2
defined $_ and /^[0-9]+$/
defined $_
/^[0-9]+$/
Truth table100%3/3
A and B
A= defined $_B= /^[0-9]+$/
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2070 | my $num = [ map { bignum($_) } @num ]; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2070 | my @alphabet = @{ $self->chars }; | |||||||||||||||||||||||||||
| 134 | 2070 | my @res; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2070 | my $numHashInt = bignum(0); | |||||||||||||||||||||||||||
| 137 | 2070 | for my $i ( 0 .. $#$num ) { | |||||||||||||||||||||||||||
| 138 | 2110 | $numHashInt += $num->[$i] % ( $i + 100 ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 2070 | my $lottery = $res[0] = $alphabet[ $numHashInt % @alphabet ]; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2070 | for my $i ( 0 .. $#$num ) { | |||||||||||||||||||||||||||
| 144 | 2110 | my $n = bignum( $num->[$i] ); | |||||||||||||||||||||||||||
| 145 | 2110 | my @s = ( $lottery, split( // => $self->salt ), @alphabet ) | |||||||||||||||||||||||||||
| 146 | [ 0 .. @alphabet ]; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2110 | @alphabet = consistent_shuffle( \@alphabet, \@s ); | |||||||||||||||||||||||||||
| 149 | 2110 | my $last = to_alphabet( $n, \@alphabet ); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2110 | push @res => split // => $last; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2110 | ▶ | if ( $i + 1 < @$num ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 40 | $n %= ord($last) + $i; | |||||||||||||||||||||||||||
| 155 | 40 | my $sepsIndex = $n % @{ $self->seps }; | |||||||||||||||||||||||||||
| 156 | 40 | push @res, $self->seps->[$sepsIndex]; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 2070 | ▶ | if ( @res < $self->minHashLength ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 18 | my $guards = $self->guards; | |||||||||||||||||||||||||||
| 162 | 18 | my $guardIndex = ( $numHashInt + ord $res[0] ) % @$guards; | |||||||||||||||||||||||||||
| 163 | 18 | my $guard = $guards->[$guardIndex]; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 18 | unshift @res, $guard; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 18 | ▶ | if ( @res < $self->minHashLength ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 18 | $guardIndex = ( $numHashInt + ord $res[2] ) % @$guards; | |||||||||||||||||||||||||||
| 169 | 18 | $guard = $guards->[$guardIndex]; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 18 | push @res, $guard; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2070 | my $halfLength = int @alphabet / 2; | |||||||||||||||||||||||||||
| 176 | 2070 | ▶ | while ( @res < $self->minHashLength ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 18 | @alphabet = consistent_shuffle( \@alphabet, \@alphabet ); | |||||||||||||||||||||||||||
| 178 | 18 | @res = ( | |||||||||||||||||||||||||||
| 179 | @alphabet[ $halfLength .. $#alphabet ], | ||||||||||||||||||||||||||||
| 180 | @res, @alphabet[ 0 .. $halfLength - 1 ] | ||||||||||||||||||||||||||||
| 181 | ); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 18 | ▶ | if ( ( my $excess = @res - $self->minHashLength ) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 18 | @res = splice @res, int $excess / 2, $self->minHashLength; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 2070 | join '' => @res; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub decode { | ||||||||||||||||||||||||||||
| 192 | 1031 | ▶ | my ( $self, $hash ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 1031 | ▶ | return unless $hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 1030 | ▶ | return unless defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 1030 | my $res = []; | |||||||||||||||||||||||||||
| 198 | 1030 | my $orig = $hash; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 1030 | my $guard = join '|', map {quotemeta} @{ $self->guards }; | |||||||||||||||||||||||||||
| 201 | 1030 | my @hash = grep { $_ ne '' } split /$guard/ => $hash; | |||||||||||||||||||||||||||
| 202 | 1030 | ▶ | my $i = ( @hash == 3 || @hash == 2 ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@hash == 3 or @hash == 2
MC/DC100%2/2
@hash == 3 or @hash == 2
@hash == 3
@hash == 2
Truth table100%3/3
A or B
A= @hash == 3B= @hash == 2
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1030 | ▶ | return unless defined( $hash = $hash[$i] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 1030 | my $lottery = substr $hash, 0, 1; | |||||||||||||||||||||||||||
| 206 | 1030 | $hash = substr $hash, 1; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1030 | my $sep = join '|', @{ $self->seps }; | |||||||||||||||||||||||||||
| 209 | 1030 | @hash = grep { $_ ne '' } split /$sep/ => $hash; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 1030 | my @alphabet = @{ $self->chars }; | |||||||||||||||||||||||||||
| 212 | 1030 | for my $part (@hash) { | |||||||||||||||||||||||||||
| 213 | 1051 | my @s = ( $lottery, split( // => $self->salt ), @alphabet ) | |||||||||||||||||||||||||||
| 214 | [ 0 .. @alphabet ]; | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1051 | @alphabet = consistent_shuffle( \@alphabet, \@s ); | |||||||||||||||||||||||||||
| 217 | 1051 | push @$res => from_alphabet( $part, \@alphabet ); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 1030 | ▶ | return unless $self->Hashids::encode(@$res) eq $orig; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1029 | ▶ | wantarray ? @$res : @$res == 1 ? $res->[0] : $res; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 5 | 1; | |||||||||||||||||||||||||||
| 226 | __END__ | ||||||||||||||||||||||||||||