3.141.152.173 |    

Navigation

Google Advertisement



crypt_apr1_md5
  1. # This is a porting from PHP of the APR1-MD5 encryption method (windows compatible).
  2. # This function requires the packages List::Util, Digest::MD5 and MIME::Base64.
  3. # See: http://stackoverflow.com/questions/1038791/how-to-programmaticaly-build-an-apr1-md5-using-php
  4. sub crypt_apr1_md5 {
  5.  
  6. my ($plainpasswd, $salt) = @_;
  7. $salt = "" if (!$salt);
  8.  
  9. # Subroutine like the PHP min() function to get
  10. # the min value of an array.
  11. my $min = sub {
  12. 	return $_[$_[0] > $_[1]];
  13. };
  14.  
  15. # Subroutine like the PHP str_shuffle() function to
  16. # shuffles a string randomly. List::Util is needed.
  17. my $str_shuffle = sub {
  18. 	return join("", shuffle(split //, shift));
  19. };
  20.  
  21. # Subroutine like the PHP strtr() function to
  22. # replaced characters from strings.
  23. my $strtr = sub {
  24. 	my ($str, $a, $b) = @_;
  25. 	$a =~ s!/!\\/!g;
  26. 	$b =~ s!/!\\/!g;
  27. 	eval "\$str =~ tr/$a/$b/, 1";
  28. 	return $str;
  29. };
  30.  
  31. my $tmp = "";
  32. my $translateTo = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  33. $salt = substr($str_shuffle->("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8) if (!length($salt)>0);
  34. my $len = length($plainpasswd);
  35. my $text = $plainpasswd.'$apr1$'.$salt;
  36. my $bin = pack("H32", md5_hex($plainpasswd.$salt.$plainpasswd));
  37.  
  38. for(my $i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, $min->(16, $i)); }
  39. for(my $i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : substr($plainpasswd, 0, 1); }
  40.  
  41. $bin = pack("H32", md5_hex($text));
  42.  
  43. for(my $i = 0; $i < 1000; $i++) {
  44. 	my $new = ($i & 1) ? $plainpasswd : $bin;
  45. 	$new .= $salt if ($i % 3);
  46. 	$new .= $plainpasswd if ($i % 7);
  47. 	$new .= ($i & 1) ? $bin : $plainpasswd;
  48. 	$bin = pack("H32", md5_hex($new));
  49. }
  50. for (my $i = 0; $i < 5; $i++) {
  51. 	my $k = $i + 6;
  52. 	my $j = $i + 12;
  53. 	$j = 5 if ($j == 16);
  54. 	$tmp = substr($bin, $i, 1).substr($bin, $k, 1).substr($bin, $j, 1).$tmp;
  55. }
  56. $tmp = chr(0).chr(0).substr($bin, 11, 1).$tmp;
  57. $tmp = reverse(substr(encode_base64($tmp), 2));
  58. $tmp =~ s!\s+!!g;
  59.  
  60. $tmp = $strtr->($tmp, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", $translateTo);
  61. return "\$apr1\$".$salt."\$".$tmp;
  62.  
  63. }
Parsed in 0.002 seconds at 1016.06 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 3 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)