PurePerl/Util.pm

filter: Statementstmt 66.6% 46 / 69 Branchbran 62.5% 10 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 5 totaltotal 63.4% 66 / 104 SCAR 26.4
CC
10
Cov
66%
digest
12.0
digester
4.1
fold_into_octets
4.0
CRAP 14.0
↑ Summary PurePerl.pm »
1 package UUID::Generator::PurePerl::Util;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 *import = \&Exporter::import;
8  
9 11 our @EXPORT = qw( digest_as_octets digest_as_32bit digest_as_16bit );
10  
11 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub fold_into_octets {
15 60 ▶ my ($num_octets, $s) = @_;
Subroutine fold_into_octets: called CC=4 SCAR=13.9
Pod undocumented
16  
17 60 my $x = "\x0" x $num_octets;
18  
19 60 ▶ while (length $s > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $s > 0) 389 60 449
20 389 my $n = q{};
21 389 ▶ while (length $x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $x > 0) 1200 329 1529
22 1200 my $c = ord(substr $x, -1, 1, q{}) ^ ord(substr $s, -1, 1, q{});
23 1200 $n = chr($c) . $n;
24 1200 ▶ last if length $s <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $s <= 0 60 1140 1200
25 }
26 389 $n = $x . $n;
27  
28 389 $x = $n;
29 }
30  
31 60 return $x;
32 }
33  
34 {
35 11 my $digester;
36  
37 sub digester {
38 60 ▶ if (! defined $digester) {
Subroutine digester: called CC=4 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $digester) 5 55 60
39 5 my $d;
40 5 $d = eval { Digest->new('SHA-1') };
41 5 ▶ $d = eval { Digest->new('MD5') } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
42 5 ▶ $d = UUID::Generator::PurePerl::Util::PseudoDigester->new() if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
43 5 $digester = $d;
44 }
45  
46 60 return $digester;
47 }
48 }
49  
50 11 sub digest_as_octets {
51 60 ▶ my $num_octets = shift;
Subroutine digest_as_octets: called CC=2 SCAR=6.9
Pod undocumented
52  
53 60 my $d = digester();
54 60 $d->reset();
55 60 $d->add($_) for @_;
56  
57 60 return fold_into_octets($num_octets, $d->digest);
58 }
59  
60 sub digest_as_32bit {
61 1 ▶ return unpack 'N', digest_as_octets(4, @_);
Subroutine digest_as_32bit: called CC=1 SCAR=0.0
Pod undocumented
62 }
63  
64 sub digest_as_16bit {
65 3 ▶ return unpack 'n', digest_as_octets(2, @_);
Subroutine digest_as_16bit: called CC=1 SCAR=0.0
Pod undocumented
66 }
67  
68 package UUID::Generator::PurePerl::Util::PseudoDigester;
69  
70 sub new {
71 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
72 0 my $entity = q{};
73  
74 0 return bless \$entity, $class;
75 }
76  
77 sub digest {
78 0 ▶ my $self = shift;
Subroutine digest: not called CC=3 SCAR=24.8
79  
80 0 my $entity = $$self;
81  
82 0 my $source = q{};
83 0 ▶ while (length $entity > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $entity > 0) 0 0 0
84 # 4 bytes seems to be enough (8 bytes in ordinal crypt() impl.)
85 0 my $token = substr($entity, 0, 4, q{}) . "\0\0\0\0";
86 0 $source .= crypt $token, $token;
87 }
88  
89 0 my @r = ( 0, 0, 0, 0 ); # 32bits * 4
90 0 my $index = 0;
91 0 ▶ while (length $source > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $source > 0) 0 0 0
92 0 my $token = substr($source, 0, 4, q{}) . "\0\0\0\0";
93 0 $r[$index] ^= unpack 'N', $token;
94  
95 0 $index = ($index + 1) % 4;
96 }
97  
98 0 return pack 'NNNN', @r;
99 }
100  
101 sub reset {
102 0 ▶ my $self = shift;
Subroutine reset: not called CC=1 SCAR=6.9
103 0 $$self = q{};
104 0 return $self;
105 }
106  
107 sub add {
108 0 ▶ my ($self, $data) = @_;
Subroutine add: not called CC=1 SCAR=6.9
109 0 $$self .= $data;
110 0 return $self;
111 }
112  
113 11 1;
114 __END__
↑ Summary PurePerl.pm »