| 1 | package Hash::MD5; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.006; | ||
| 5 | use strict; | ||
| 6 | use warnings FATAL => 'all'; | ||
| 7 | use Digest::MD5 qw(md5_hex); | ||
| 8 | use vars qw($VERSION @EXPORT_OK); | ||
| 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 | my ($hashref) = @_; | ||
| 19 | my $serial = scalar( keys %$hashref ); | ||
| 20 | my $ret = ''; | ||
| 21 | foreach ( sort { $a cmp $b } keys %$hashref ) { | ||
| 22 | if ( ref $hashref->{$_} eq 'HASH' ) { $ret = $ret . unpack( 'H*', $_ ) . _sort_keys( ( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); } | ||
| 23 | elsif ( ref $hashref->{$_} eq 'ARRAY' ){ $ret = $ret . unpack( 'H*', $_ ) . _sort_array(( defined $hashref->{$_} ? $hashref->{$_} : 'undef' ) ); } | ||
| 24 | else { | ||
| 25 | my $var = (defined $hashref->{$_}) ? $hashref->{$_} : 'undef'; | ||
| 26 | $serial = $serial . length( $var ); | ||
| 27 | utf8::encode($_); | ||
| 28 | utf8::encode($var); | ||
| 29 | $ret = $ret . unpack( 'H*', $_ ) . unpack( 'H*', $var ); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | return $ret . $serial; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _sort_array { | ||
| 36 | my ($arrayref) = @_; | ||
| 37 | my $serial = scalar(@$arrayref); | ||
| 38 | my $ret = ''; | ||
| 39 | foreach (@$arrayref) { | ||
| 40 | if ( ref $_ eq 'HASH' ) { $ret = $ret . _sort_keys($_); } | ||
| 41 | elsif ( ref $_ eq 'ARRAY') { $ret = $ret . _sort_array($_);} | ||
| 42 | else { | ||
| 43 | $serial = $serial . length($_); | ||
| 44 | utf8::encode($_); | ||
| 45 | $ret = $ret . unpack( 'H*', $_ ); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | 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 | my ($arg) = @_; | ||
| 91 | return md5_hex( _sort_keys($arg) ); | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 sum_array | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub sum_array { | ||
| 99 | my ($arg) = @_; | ||
| 100 | 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 | 1; # End of Hash::MD5 |