File Coverage

lib/Hash/MD5.pm
Criterion Covered Total %
statement 44 47 93.6
branch 6 14 42.8
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 62 73 84.9


line stmt bran cond sub pod time code
1             package Hash::MD5;
2              
3 3     3   37076 use utf8;
  3         23  
  3         11  
4 3     3   127 use 5.006;
  3         6  
  3         72  
5 3     3   17 use strict;
  3         3  
  3         93  
6 3     3   9 use warnings FATAL => 'all';
  3         4  
  3         107  
7 3     3   10 use Digest::MD5 qw(md5_hex);
  3         3  
  3         149  
8 3     3   15 use vars qw($VERSION @EXPORT_OK);
  3         3  
  3         1522  
9              
10             require Exporter;
11             *import = \&Exporter::import;
12             @EXPORT_OK = qw(sum_hash sum_array);
13              
14             ###############################################################################
15             ############## PRIVATE SUBS ######################
16             ###############################################################################
17             sub _sort_keys {
18 11     11   9 my ($hashref) = @_;
19 11         18 my $serial = scalar( keys %$hashref );
20 11         13 my $ret = '';
21 11         26 foreach ( sort { $a cmp $b } keys %$hashref ) {
  11         23  
22 22 0       51 if ( ref $hashref->{$_} eq 'HASH' ) { $ret = $ret . unpack( 'H*', $_ ) . _sort_keys( ( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); }
  0 50       0  
    50          
23 0 0       0 elsif ( ref $hashref->{$_} eq 'ARRAY' ){ $ret = $ret . unpack( 'H*', $_ ) . _sort_array(( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); }
24             else {
25 22 50       30 my $var = (defined $hashref->{$_}) ? $hashref->{$_} : 'undef';
26 22         24 $serial = $serial . length( $var );
27 22         26 utf8::encode($_);
28 22         22 utf8::encode($var);
29 22         60 $ret = $ret . unpack( 'H*', $_ ) . unpack( 'H*', $var );
30             }
31             }
32 11         36 return $ret . $serial;
33             }
34              
35             sub _sort_array {
36 16     16   13 my ($arrayref) = @_;
37 16         16 my $serial = scalar(@$arrayref);
38 16         14 my $ret = '';
39 16         83 foreach (@$arrayref) {
40 40 100       65 if ( ref $_ eq 'HASH' ) { $ret = $ret . _sort_keys($_); }
  3 50       6  
41 0         0 elsif ( ref $_ eq 'ARRAY') { $ret = $ret . _sort_array($_);}
42             else {
43 37         46 $serial = $serial . length($_);
44 37         41 utf8::encode($_);
45 37         74 $ret = $ret . unpack( 'H*', $_ );
46             }
47             }
48 16         68 return $ret . $serial;
49             }
50              
51             =pod
52              
53             =head1 NAME
54              
55             Hash::MD5 - MD5 checksum for choosen hashref
56              
57             =head1 VERSION
58              
59             Version 0.05
60              
61             =cut
62              
63             our $VERSION = '0.05';
64              
65             =head1 SYNOPSIS
66              
67             use Hash::MD5 qw(sum_hash);
68            
69             ...
70             my $hashref = {some => 'hashref'};
71             print sum_hash( $hashref ), $/;
72             ...
73              
74              
75              
76             use Hash::MD5 qw(sum_array);
77            
78             ...
79             my $arrayref = ['some', 'arrayref'];
80             print sum_array( $arrayref ), $/;
81             ...
82              
83             =head1 SUBROUTINES/METHODS
84              
85             =head2 sum_hash
86              
87             =cut
88              
89             sub sum_hash {
90 8     8 1 764 my ($arg) = @_;
91 8         10 return md5_hex( _sort_keys($arg) );
92             }
93              
94             =head2 sum_array
95              
96             =cut
97              
98             sub sum_array {
99 16     16 1 2642 my ($arg) = @_;
100 16         24 return md5_hex( _sort_array($arg) );
101             }
102              
103             =head1 AUTHOR
104              
105             Mario Zieschang, C<< >>
106              
107             =head1 BUGS
108              
109             Please report any bugs or feature requests to C, or through
110             the web interface at L. I will be notified, and then you'll
111             automatically be notified of progress on your bug as I make changes.
112              
113              
114             =head1 SUPPORT
115              
116             =over 4
117              
118             =item * RT: CPAN's request tracker (report bugs here)
119              
120             L
121              
122             =item * AnnoCPAN: Annotated CPAN documentation
123              
124             L
125              
126             =item * CPAN Ratings
127              
128             L
129              
130             =item * Search CPAN
131              
132             L
133              
134             =back
135              
136              
137             =head1 MOTIVATION
138              
139             I need a package which I can grab the uniqueness of a Hashrefs in a md5 sum.
140             This I will compare to later processing.
141             My first approach was to use encode_json string.
142             Unfortunately, the resort was not very consistent.
143              
144             So I wrote my first cpan packet in order also to learn.
145              
146              
147             =head1 LICENSE AND COPYRIGHT
148              
149             Copyright 2015 Mario Zieschang.
150              
151             This program is free software; you can redistribute it and/or modify it
152             under the terms of the the Artistic License (2.0). You may obtain a
153             copy of the full license at:
154              
155             L
156              
157             Any use, modification, and distribution of the Standard or Modified
158             Versions is governed by this Artistic License. By using, modifying or
159             distributing the Package, you accept this license. Do not use, modify,
160             or distribute the Package, if you do not accept this license.
161              
162             If your Modified Version has been derived from a Modified Version made
163             by someone other than you, you are nevertheless required to ensure that
164             your Modified Version complies with the requirements of this license.
165              
166             This license does not grant you the right to use any trademark, service
167             mark, tradename, or logo of the Copyright Holder.
168              
169             This license includes the non-exclusive, worldwide, free-of-charge
170             patent license to make, have made, use, offer to sell, sell, import and
171             otherwise transfer the Package with respect to any patent claims
172             licensable by the Copyright Holder that are necessarily infringed by the
173             Package. If you institute patent litigation (including a cross-claim or
174             counterclaim) against any party alleging that the Package constitutes
175             direct or contributory patent infringement, then this Artistic License
176             to you shall terminate on the date that such litigation is filed.
177              
178             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
179             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
180             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
181             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
182             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
183             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
184             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
185             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
186              
187              
188             =cut
189              
190             1; # End of Hash::MD5