« blib/lib/DBIx/Class/EncodedColumn.pm
↑ Summary
blib/lib/DBIx/Class/EncodedColumn/Crypt/OpenPGP.pm »
| 1 | package DBIx::Class::EncodedColumn::Digest; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Digest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Encode qw( str2bytes ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.00001'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | my %digest_lengths = | |||||||||||||||||||||||||||
| 11 | ( | ||||||||||||||||||||||||||||
| 12 | 'MD2' => { base64 => 22, binary => 16, hex => 32 }, | ||||||||||||||||||||||||||||
| 13 | 'MD4' => { base64 => 22, binary => 16, hex => 32 }, | ||||||||||||||||||||||||||||
| 14 | 'MD5' => { base64 => 22, binary => 16, hex => 32 }, | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 'SHA-1' => { base64 => 27, binary => 20, hex => 40 }, | ||||||||||||||||||||||||||||
| 17 | 'SHA-256' => { base64 => 43, binary => 32, hex => 64 }, | ||||||||||||||||||||||||||||
| 18 | 'SHA-384' => { base64 => 64, binary => 48, hex => 96 }, | ||||||||||||||||||||||||||||
| 19 | 'SHA-512' => { base64 => 86, binary => 64, hex => 128 }, | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 'CRC-CCITT' => { base64 => 2, binary => 3, hex => 3 }, | ||||||||||||||||||||||||||||
| 22 | 'CRC-16' => { base64 => 6, binary => 5, hex => 4 }, | ||||||||||||||||||||||||||||
| 23 | 'CRC-32' => { base64 => 14, binary => 10, hex => 8 }, | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 'Adler-32' => { base64 => 6, binary => 4, hex => 8 }, | ||||||||||||||||||||||||||||
| 26 | 'Whirlpool' => { base64 => 86, binary => 64, hex => 128 }, | ||||||||||||||||||||||||||||
| 27 | 'Haval-256' => { base64 => 44, binary => 32, hex => 64 }, | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | 1 | my @salt_pool = ('A' .. 'Z', 'a' .. 'z', 0 .. 9, '+','/','='); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub make_encode_sub { | ||||||||||||||||||||||||||||
| 32 | 7 | ▶ | my($class, $col, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make_encode_sub: called CC=11 SCAR=24.7
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 7 | ▶ | my $for = $args->{format} ||= 'base64'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"format"} ||= "base64"
MC/DC100%1/1
$args->{"format"} ||= "base64"
$args->{"format"}
Truth table100%2/2
A
A= $args->{"format"}
| |||||||||||||||||||||||||||||
| 34 | 7 | ▶ | my $alg = $args->{algorithm} ||= 'SHA-256'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"algorithm"} ||= "SHA-256"
MC/DC100%1/1
$args->{"algorithm"} ||= "SHA-256"
$args->{"algorithm"}
Truth table100%2/2
A
A= $args->{"algorithm"}
| |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | my $slen = $args->{salt_length} ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"salt_length"} ||= 0
MC/DC100%1/1
$args->{"salt_length"} ||= 0
$args->{"salt_length"}
Truth table100%2/2
A
A= $args->{"salt_length"}
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7 | my $encode = $args->{charset}; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 7 | ▶ | die("Valid Digest formats are 'binary', 'hex' or 'base64'. You used '$for'.") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | unless $for =~ /^(?:hex|base64|binary)$/; | ||||||||||||||||||||||||||||
| 41 | 7 | ▶ | defined(my $object = eval{ Digest->new($alg) }) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | die("Can't use Digest algorithm ${alg}: $@"); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 7 | ▶ | my $format_method = $for eq 'binary' ? 'digest' : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | ($for eq 'hex' ? 'hexdigest' : 'b64digest'); | ||||||||||||||||||||||||||||
| 46 | #thanks Haval for breaking the standard. thanks! | ||||||||||||||||||||||||||||
| 47 | 7 | ▶ | $format_method = 'base64digest 'if ($alg eq 'Haval-256' && $for eq 'base64'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$alg eq "Haval-256" and $for eq "base64"
MC/DC0%0/2
$alg eq "Haval-256" and $for eq "base64"
$alg eq "Haval-256"
$for eq "base64"
Truth table33%1/3
A and B
A= $alg eq "Haval-256"B= $for eq "base64"
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | my $encoder = sub { | ||||||||||||||||||||||||||||
| 50 | 28 | ▶ | my ($plain_text, $salt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 51 | 28 | ▶ | $plain_text = str2bytes($encode, $plain_text, Encode::FB_PERLQQ | Encode::LEAVE_SRC) if $encode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 28 | ▶ | $salt ||= join('', map { $salt_pool[int(rand(65))] } 1 .. $slen); | ||||||||||||||||||||||||||
|
Condition100%3/3
$salt ||= join("", map({$salt_pool[int rand 65];} 1 .. $slen))
MC/DC100%2/2
$salt ||= join("", map({$salt_pool[int rand 65];} 1 .. $slen))
$salt
join("", map({$salt_pool[int rand 65];} 1 .. $slen))
Truth table100%3/3
A ||= B
A= $saltB= join("", map({$salt_pool[int rand 65];} 1 .. $slen))
| |||||||||||||||||||||||||||||
| 53 | 28 | $object->reset()->add($plain_text.$salt); | |||||||||||||||||||||||||||
| 54 | 28 | my $digest = $object->$format_method; | |||||||||||||||||||||||||||
| 55 | #print "${plain_text}\t ${salt}:\t${digest}${salt}\n" if $salt; | ||||||||||||||||||||||||||||
| 56 | 28 | return $digest.$salt; | |||||||||||||||||||||||||||
| 57 | 7 | }; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | #in case i didn't prepopulate it | ||||||||||||||||||||||||||||
| 60 | 7 | ▶ | $digest_lengths{$alg}{$for} ||= length($encoder->('test1')); | ||||||||||||||||||||||||||
|
Condition33%1/3
$digest_lengths{$alg}{$for} ||= length &$encoder("test1")
MC/DC0%0/2
$digest_lengths{$alg}{$for} ||= length &$encoder("test1")
$digest_lengths{$alg}{$for}
length &$encoder("test1")
Truth table33%1/3
A ||= B
A= $digest_lengths{$alg}{$for}B= length &$encoder("test1")
| |||||||||||||||||||||||||||||
| 61 | 7 | return $encoder; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub make_check_sub { | ||||||||||||||||||||||||||||
| 65 | 5 | ▶ | my($class, $col, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make_check_sub: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #this is the digest length | ||||||||||||||||||||||||||||
| 68 | 5 | my $len = $digest_lengths{$args->{algorithm}}{$args->{format}}; | |||||||||||||||||||||||||||
| 69 | 5 | ▶ | die("Unable to find digest length") unless defined $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | my $encode = $args->{charset} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'charset'} || ''
MC/DC100%1/1
$args->{'charset'} || ''
$args->{'charset'}
Truth table100%2/2
A
A= $args->{'charset'}
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | #fast fast fast | ||||||||||||||||||||||||||||
| 73 | 5 | ▶ | return eval qq^ sub { | ||||||||||||||||||||||||||
|
Subroutine
check_sha1_b64: called CC=1 SCAR=0.0
Subroutine
check_sha1_hex: called CC=1 SCAR=0.0
Subroutine
check_sha256_b64_salted: called CC=1 SCAR=0.0
Condition50%1/2
eval " sub {\n my \$col_v = \$_[0]->get_column('${col}');\n \$col_v = str2bytes('${encode}', \$col_v, Encode::FB_PERLQQ | Encode::LEAVE_SRC) if '${encode}';\n my \$salt = substr(\$col_v, $len);\n \$_[0]->_column_encoders->{$col}->(\$_[1], \$salt) eq \$col_v;\n } " || die($@)
MC/DC0%0/1
eval " sub {\n my \$col_v = \$_[0]->get_column('${col}');\n \$col_v = str2bytes('${encode}', \$col_v, Encode::FB_PERLQQ | Encode::LEAVE_SRC) if '${encode}';\n my \$salt = substr(\$col_v, $len);\n \$_[0]->_column_encoders->{$col}->(\$_[1], \$salt) eq \$col_v;\n } " || die($@)
eval " sub {\n my \$col_v = \$_[0]->get_column('${col}');\n \$col_v = str2bytes('${encode}', \$col_v, Encode::FB_PERLQQ | Encode::LEAVE_SRC) if '${encode}';\n my \$salt = substr(\$col_v, $len);\n \$_[0]->_column_encoders->{$col}->(\$_[1], \$salt) eq \$col_v;\n } "
Truth table50%1/2
A
A= eval " sub {\n my \$col_v = \$_[0]->get_column('${col}');\n \$col_v = str2bytes('${encode}', \$col_v, Encode::FB_PERLQQ | Encode::LEAVE_SRC) if '${encode}';\n my \$salt = substr(\$col_v, $len);\n \$_[0]->_column_encoders->{$col}->(\$_[1], \$salt) eq \$col_v;\n } "
| |||||||||||||||||||||||||||||
| 74 | my \$col_v = \$_[0]->get_column('${col}'); | ||||||||||||||||||||||||||||
| 75 | \$col_v = str2bytes('${encode}', \$col_v, Encode::FB_PERLQQ | Encode::LEAVE_SRC) if '${encode}'; | ||||||||||||||||||||||||||||
| 76 | my \$salt = substr(\$col_v, ${len}); | ||||||||||||||||||||||||||||
| 77 | \$_[0]->_column_encoders->{${col}}->(\$_[1], \$salt) eq \$col_v; | ||||||||||||||||||||||||||||
| 78 | } ^ || die($@); | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | __END__; | ||||||||||||||||||||||||||||