| 1 | package MPMinus::MainTools::MD5; # $Id: MD5.pm 122 2013-05-07 13:05:41Z minus $ | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | MPMinus::MainTools::MD5 - MD5 functions | ||
| 7 | |||
| 8 | =head1 VERSION | ||
| 9 | |||
| 10 | Version 1.01 | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use MPMinus::MainTools::MD5; | ||
| 15 | |||
| 16 | my $md5_apache = get_md5_apache( $password ); | ||
| 17 | my $md5_unix = get_md5_unix( $password ); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | MD5 functions | ||
| 22 | |||
| 23 | =over 8 | ||
| 24 | |||
| 25 | =item B<get_md5_apache> | ||
| 26 | |||
| 27 | my $md5_apache = get_md5_apache( $password ); | ||
| 28 | |||
| 29 | =item B<get_md5_unix> | ||
| 30 | |||
| 31 | my $md5_unix = get_md5_unix( $password ); | ||
| 32 | |||
| 33 | =back | ||
| 34 | |||
| 35 | =head1 AUTHOR | ||
| 36 | |||
| 37 | Serz Minus (Lepenkov Sergey) L<http://serzik.ru> E<lt>minus@mail333.comE<gt> | ||
| 38 | |||
| 39 | =head1 COPYRIGHT | ||
| 40 | |||
| 41 | Copyright (C) 1998-2013 D&D Corporation. All Rights Reserved | ||
| 42 | |||
| 43 | =head1 LICENSE | ||
| 44 | |||
| 45 | This program is free software: you can redistribute it and/or modify | ||
| 46 | it under the terms of the GNU General Public License as published by | ||
| 47 | the Free Software Foundation, either version 3 of the License, or | ||
| 48 | (at your option) any later version. | ||
| 49 | |||
| 50 | This program is distributed in the hope that it will be useful, | ||
| 51 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 52 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 53 | GNU General Public License for more details. | ||
| 54 | |||
| 55 | See C<LICENSE> file | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | use vars qw($VERSION); | ||
| 60 | $VERSION = 1.01; | ||
| 61 | |||
| 62 | use base qw/Exporter/; | ||
| 63 | our @EXPORT = qw( | ||
| 64 | get_md5_apache get_md5_unix | ||
| 65 | ); | ||
| 66 | |||
| 67 | use Digest::MD5; | ||
| 68 | |||
| 69 | my $Magic; | ||
| 70 | my $itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | ||
| 71 | |||
| 72 | sub get_md5_apache { | ||
| 73 | my $password = shift || return 0; | ||
| 74 | return apache_md5_crypt($password,&get_salt); | ||
| 75 | } | ||
| 76 | sub get_md5_unix { | ||
| 77 | my $password = shift || return 0; | ||
| 78 | return unix_md5_crypt($password,&get_salt); | ||
| 79 | } | ||
| 80 | sub get_salt { | ||
| 81 | my($salt,$i, $rand); | ||
| 82 | $rand=0; | ||
| 83 | my (@itoa64) = (0 .. 9,'a' .. 'z','A' .. 'Z'); | ||
| 84 | $salt = ''; | ||
| 85 | for ($i = 0; $i < 8; $i++) { | ||
| 86 | srand(time + $rand + $$); | ||
| 87 | $rand = rand(25*29*17 + $rand); | ||
| 88 | $salt .= $itoa64[$rand & $#itoa64]; | ||
| 89 | } | ||
| 90 | return $salt; # crypt($passwd,$salt); | ||
| 91 | } | ||
| 92 | sub to64 { | ||
| 93 | my ($v, $n) = @_; | ||
| 94 | my $ret = ''; | ||
| 95 | while (--$n >= 0) { | ||
| 96 | $ret .= substr($itoa64, $v & 0x3f, 1); | ||
| 97 | $v >>= 6; | ||
| 98 | } | ||
| 99 | return $ret; | ||
| 100 | } | ||
| 101 | sub apache_md5_crypt { | ||
| 102 | $Magic = q/$apr1$/; | ||
| 103 | return _md5_crypt(@_); | ||
| 104 | } | ||
| 105 | sub unix_md5_crypt { | ||
| 106 | $Magic = q/$1$/; | ||
| 107 | return _md5_crypt(@_); | ||
| 108 | } | ||
| 109 | sub _md5_crypt { | ||
| 110 | my($pw, $salt) = @_; | ||
| 111 | my $passwd; | ||
| 112 | |||
| 113 | if ( defined $salt ) { | ||
| 114 | $salt =~ s/^\Q$Magic//; # Take care of the magic string if | ||
| 115 | # if present. | ||
| 116 | $salt =~ s/^(.*)\$.*$/$1/; # Salt can have up to 8 chars... | ||
| 117 | $salt = substr($salt, 0, 8); | ||
| 118 | } else { | ||
| 119 | $salt = ''; # in case no salt was proffered | ||
| 120 | $salt .= substr($itoa64,int(rand(64)+1),1) | ||
| 121 | while length($salt) < 8; | ||
| 122 | } | ||
| 123 | |||
| 124 | my $ctx = new Digest::MD5; # Here we start the calculation | ||
| 125 | $ctx->add($pw); # Original password... | ||
| 126 | $ctx->add($Magic); # ...our magic string... | ||
| 127 | $ctx->add($salt); # ...the salt... | ||
| 128 | |||
| 129 | my ($final) = new Digest::MD5; | ||
| 130 | $final->add($pw); | ||
| 131 | $final->add($salt); | ||
| 132 | $final->add($pw); | ||
| 133 | $final = $final->digest; | ||
| 134 | |||
| 135 | my $pl; | ||
| 136 | for ($pl = length($pw); $pl > 0; $pl -= 16) { | ||
| 137 | $ctx->add(substr($final, 0, $pl > 16 ? 16 : $pl)); | ||
| 138 | } | ||
| 139 | |||
| 140 | # Now the 'weird' xform | ||
| 141 | my $i; | ||
| 142 | for ($i = length($pw); $i; $i >>= 1) { | ||
| 143 | if ($i & 1) { $ctx->add(pack("C", 0)); } | ||
| 144 | # This comes from the original version, | ||
| 145 | # where a memset() is done to $final | ||
| 146 | # before this loop. | ||
| 147 | else { $ctx->add(substr($pw, 0, 1)); } | ||
| 148 | } | ||
| 149 | $final = $ctx->digest; | ||
| 150 | |||
| 151 | # The following is supposed to make | ||
| 152 | # things run slower. In perl, perhaps | ||
| 153 | # it'll be *really* slow! | ||
| 154 | my $ctx1; | ||
| 155 | for ($i = 0; $i < 1000; $i++) { | ||
| 156 | $ctx1 = new Digest::MD5; | ||
| 157 | if ($i & 1) { $ctx1->add($pw); } | ||
| 158 | else { $ctx1->add(substr($final, 0, 16)); } | ||
| 159 | if ($i % 3) { $ctx1->add($salt); } | ||
| 160 | if ($i % 7) { $ctx1->add($pw); } | ||
| 161 | if ($i & 1) { $ctx1->add(substr($final, 0, 16)); } | ||
| 162 | else { $ctx1->add($pw); } | ||
| 163 | $final = $ctx1->digest; | ||
| 164 | } | ||
| 165 | |||
| 166 | # Final xform | ||
| 167 | $passwd = ''; | ||
| 168 | $passwd .= to64(int(unpack("C", (substr($final, 0, 1))) << 16) | ||
| 169 | | int(unpack("C", (substr($final, 6, 1))) << 8) | ||
| 170 | | int(unpack("C", (substr($final, 12, 1)))), 4); | ||
| 171 | $passwd .= to64(int(unpack("C", (substr($final, 1, 1))) << 16) | ||
| 172 | | int(unpack("C", (substr($final, 7, 1))) << 8) | ||
| 173 | | int(unpack("C", (substr($final, 13, 1)))), 4); | ||
| 174 | $passwd .= to64(int(unpack("C", (substr($final, 2, 1))) << 16) | ||
| 175 | | int(unpack("C", (substr($final, 8, 1))) << 8) | ||
| 176 | | int(unpack("C", (substr($final, 14, 1)))), 4); | ||
| 177 | $passwd .= to64(int(unpack("C", (substr($final, 3, 1))) << 16) | ||
| 178 | | int(unpack("C", (substr($final, 9, 1))) << 8) | ||
| 179 | | int(unpack("C", (substr($final, 15, 1)))), 4); | ||
| 180 | $passwd .= to64(int(unpack("C", (substr($final, 4, 1))) << 16) | ||
| 181 | | int(unpack("C", (substr($final, 10, 1))) << 8) | ||
| 182 | | int(unpack("C", (substr($final, 5, 1)))), 4); | ||
| 183 | $passwd .= to64(int(unpack("C", substr($final, 11, 1))), 2); | ||
| 184 | |||
| 185 | $final = ''; | ||
| 186 | return $Magic . $salt . q/$/ . $passwd; | ||
| 187 | } | ||
| 188 | |||
| 189 | 1; | ||
| 190 | |||
| 191 | __END__ |