note 73619 added to function.crypt

From: Date: Sun, 04 Mar 2007 15:47:35 +0000
Subject: note 73619 added to function.crypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-124065@lists.php.net to get a copy of this message
Are you using Apache2 on f.i. WinXP and want to create .htpasswd files via php? Then you need to use the APR1-MD5 encryption method. Here is a function for that: <?php function crypt_apr1_md5($plainpasswd) { $salt = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8); $len = strlen($plainpasswd); $text = $plainpasswd.'$apr1$'.$salt; $bin = pack("H32", md5($plainpasswd.$salt.$plainpasswd)); for($i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, min(16, $i)); } for($i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : $plainpasswd{0}; } $bin = pack("H32", md5($text)); for($i = 0; $i < 1000; $i++) { $new = ($i & 1) ? $plainpasswd : $bin; if ($i % 3) $new .= $salt; if ($i % 7) $new .= $plainpasswd; $new .= ($i & 1) ? $bin : $plainpasswd; $bin = pack("H32", md5($new)); } for ($i = 0; $i < 5; $i++) { $k = $i + 6; $j = $i + 12; if ($j == 16) $j = 5; $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp; } $tmp = chr(0).chr(0).$bin[11].$tmp; $tmp = strtr(strrev(substr(base64_encode($tmp), 2)), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); return "$"."apr1"."$".$salt."$".$tmp; } ?> ---- Server IP: 129.16.214.50 Probable Submitter: 83.252.43.77 ---- Manual Page -- http://www.php.net/manual/en/function.crypt.php Edit -- https://master.php.net/note/edit/73619 Del: integrated -- https://master.php.net/note/delete/73619/integrated Del: useless -- https://master.php.net/note/delete/73619/useless Del: bad code -- https://master.php.net/note/delete/73619/bad+code Del: spam -- https://master.php.net/note/delete/73619/spam Del: non-english -- https://master.php.net/note/delete/73619/non-english Del: in docs -- https://master.php.net/note/delete/73619/in+docs Del: other reasons-- https://master.php.net/note/delete/73619 Reject -- https://master.php.net/note/reject/73619 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124065) next »