File Coverage

lib/Hash/MD5.pm
Criterion Covered Total %
statement 45 46 97.8
branch 9 14 64.2
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 66 72 91.6


line stmt bran cond sub pod time code
1             package Hash::MD5;
2              
3 3     3   55092 use utf8;
  3         28  
  3         14  
4 3     3   95 use 5.10.0;
  3         9  
5 3     3   23 use strict;
  3         4  
  3         61  
6 3     3   14 use warnings FATAL => 'all';
  3         4  
  3         123  
7 3     3   14 use Digest::MD5 qw(md5_hex);
  3         5  
  3         165  
8 3     3   25 use vars qw($VERSION @EXPORT_OK);
  3         5  
  3         1723  
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 13     13   17 my ($hashref) = @_;
19 13         22 my $serial = scalar( keys %$hashref );
20 13         15 my $ret = '';
21 13         41 foreach ( sort { $a cmp $b } keys %$hashref ) {
  17         37  
22 28 0       77 if ( ref $hashref->{$_} eq 'HASH' ) { $ret = $ret . unpack( 'H*', $_ ) . _sort_keys( ( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); }
  0 50       0  
    100          
23 2 50       9 elsif ( ref $hashref->{$_} eq 'ARRAY' ){ $ret = $ret . unpack( 'H*', $_ ) . _sort_array(( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); }
24             else {
25 26 50       54 my $var = (defined $hashref->{$_}) ? $hashref->{$_} : 'undef';
26 26         31 $serial = $serial . length( $var );
27 26         51 utf8::encode($_);
28 26         37 utf8::encode($var);
29 26         94 $ret = $ret . unpack( 'H*', $_ ) . unpack( 'H*', $var );
30             }
31             }
32 13         61 return $ret . $serial;
33             }
34              
35             sub _sort_array {
36 22     22   24 my ($arrayref) = @_;
37 22         27 my $serial = scalar(@$arrayref);
38 22         24 my $ret = '';
39 22         39 foreach (@$arrayref) {
40 58 100       124 if ( ref $_ eq 'HASH' ) { $ret = $ret . _sort_keys($_); }
  3 100       7  
41 2         5 elsif ( ref $_ eq 'ARRAY') { $ret = $ret . _sort_array($_);}
42             else {
43 53         72 $serial = $serial . length($_);
44 53         88 utf8::encode($_);
45 53         140 $ret = $ret . unpack( 'H*', $_ );
46             }
47             }
48 22         100 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.06
60              
61             =cut
62              
63             our $VERSION = '0.06';
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 10     10 1 1094 my ($arg) = @_;
91 10         18 return md5_hex( _sort_keys($arg) );
92             }
93              
94             =head2 sum_array
95              
96             =cut
97              
98             sub sum_array {
99 18     18 1 3170 my ($arg) = @_;
100 18         34 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             =head1 LICENSE AND COPYRIGHT
147              
148             Copyright 2015 Mario Zieschang.
149              
150             This program is free software; you can redistribute it and/or modify it
151             under the terms of the the Artistic License (2.0). You may obtain a
152             copy of the full license at:
153              
154             L
155              
156             Any use, modification, and distribution of the Standard or Modified
157             Versions is governed by this Artistic License. By using, modifying or
158             distributing the Package, you accept this license. Do not use, modify,
159             or distribute the Package, if you do not accept this license.
160              
161             If your Modified Version has been derived from a Modified Version made
162             by someone other than you, you are nevertheless required to ensure that
163             your Modified Version complies with the requirements of this license.
164              
165             This license does not grant you the right to use any trademark, service
166             mark, tradename, or logo of the Copyright Holder.
167              
168             This license includes the non-exclusive, worldwide, free-of-charge
169             patent license to make, have made, use, offer to sell, sell, import and
170             otherwise transfer the Package with respect to any patent claims
171             licensable by the Copyright Holder that are necessarily infringed by the
172             Package. If you institute patent litigation (including a cross-claim or
173             counterclaim) against any party alleging that the Package constitutes
174             direct or contributory patent infringement, then this Artistic License
175             to you shall terminate on the date that such litigation is filed.
176              
177             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
178             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
179             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
180             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
181             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
182             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
183             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
184             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
185              
186              
187             =cut
188              
189             1; # End of Hash::MD5