| 1 | package Hash::MD5; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 3 | ▶ | use utf8; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 3 | ▶ | use 5.006; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 3 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 3 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 3 | ▶ | use vars qw($VERSION @EXPORT_OK); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 3 | require Exporter; | |||||||||||||||||
| 11 | 3 | *import = \&Exporter::import; | |||||||||||||||||
| 12 | 3 | @EXPORT_OK = qw(sum_hash sum_array); | |||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | ############################################################################### | ||||||||||||||||||
| 15 | ############## PRIVATE SUBS ###################### | ||||||||||||||||||
| 16 | ############################################################################### | ||||||||||||||||||
| 17 | sub _sort_keys { | ||||||||||||||||||
| 18 | 11 | ▶ | my ($hashref) = @_; | ||||||||||||||||
|
Subroutine
_sort_keys: called CC=7 SCAR=22.6
| |||||||||||||||||||
| 19 | 11 | my $serial = scalar( keys %$hashref ); | |||||||||||||||||
| 20 | 11 | my $ret = ''; | |||||||||||||||||
| 21 | 11 | foreach ( sort { $a cmp $b } keys %$hashref ) { | |||||||||||||||||
| 22 | 22 | ▶ | if ( ref $hashref->{$_} eq 'HASH' ) { $ret = $ret . unpack( 'H*', $_ ) . _sort_keys( ( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); } | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 23 | 0 | ▶ | elsif ( ref $hashref->{$_} eq 'ARRAY' ){ $ret = $ret . unpack( 'H*', $_ ) . _sort_array(( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | else { | ||||||||||||||||||
| 25 | 22 | ▶ | my $var = (defined $hashref->{$_}) ? $hashref->{$_} : 'undef'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 26 | 22 | $serial = $serial . length( $var ); | |||||||||||||||||
| 27 | 22 | utf8::encode($_); | |||||||||||||||||
| 28 | 22 | utf8::encode($var); | |||||||||||||||||
| 29 | 22 | $ret = $ret . unpack( 'H*', $_ ) . unpack( 'H*', $var ); | |||||||||||||||||
| 30 | } | ||||||||||||||||||
| 31 | } | ||||||||||||||||||
| 32 | 11 | return $ret . $serial; | |||||||||||||||||
| 33 | } | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | sub _sort_array { | ||||||||||||||||||
| 36 | 16 | ▶ | my ($arrayref) = @_; | ||||||||||||||||
|
Subroutine
_sort_array: called CC=4 SCAR=14.0
| |||||||||||||||||||
| 37 | 16 | my $serial = scalar(@$arrayref); | |||||||||||||||||
| 38 | 16 | my $ret = ''; | |||||||||||||||||
| 39 | 16 | foreach (@$arrayref) { | |||||||||||||||||
| 40 | 40 | ▶ | if ( ref $_ eq 'HASH' ) { $ret = $ret . _sort_keys($_); } | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 41 | 0 | elsif ( ref $_ eq 'ARRAY') { $ret = $ret . _sort_array($_);} | |||||||||||||||||
| 42 | else { | ||||||||||||||||||
| 43 | 37 | $serial = $serial . length($_); | |||||||||||||||||
| 44 | 37 | utf8::encode($_); | |||||||||||||||||
| 45 | 37 | $ret = $ret . unpack( 'H*', $_ ); | |||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | 16 | 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 | 3 | 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 | ▶ | my ($arg) = @_; | ||||||||||||||||
|
Subroutine
sum_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 91 | 8 | return md5_hex( _sort_keys($arg) ); | |||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | =head2 sum_array | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | =cut | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | sub sum_array { | ||||||||||||||||||
| 99 | 16 | ▶ | my ($arg) = @_; | ||||||||||||||||
|
Subroutine
sum_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 100 | 16 | return md5_hex( _sort_array($arg) ); | |||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | =head1 AUTHOR | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | Mario Zieschang, C<< <mario.zieschang at combase.de> >> | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | =head1 BUGS | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | Please report any bugs or feature requests to C<bug-hash-md5 at rt.cpan.org>, or through | ||||||||||||||||||
| 110 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-MD5>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-MD5> | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | L<http://annocpan.org/dist/Hash-MD5> | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | =item * CPAN Ratings | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | L<http://cpanratings.perl.org/d/Hash-MD5> | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | =item * Search CPAN | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | L<http://search.cpan.org/dist/Hash-MD5/> | ||||||||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||
| 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 | 3 | 1; # End of Hash::MD5 | |||||||||||||||||