note 56934 deleted from function.md5 by joey

From: Date: Sun, 17 Jul 2011 08:45:22 +0000
Subject: note 56934 deleted from function.md5 by joey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181616@lists.php.net to get a copy of this message
Note Submitter: mina86 at tlen dot pl ---- It seems that the best solution would be to use HMAC-MD5. An implementation of HMAC-SHA1 was posted by mark on 30-Jan-2004 02:28 as a user comment to sha1() function (-> http://php.net/manual/function.sha1.php#39492). Here's how it would look like (some other optimizations/modifications are included as well): <?php // Calculate HMAC according to RFC2104 // http://www.ietf.org/rfc/rfc2104.txt function hmac($key, $data, $hash = 'md5', $blocksize = 64) { if (strlen($key)>$blocksize) { $key = pack('H*', $hash($key)); } $key = str_pad($key, $blocksize, chr(0)); $ipad = str_repeat(chr(0x36), $blocksize); $opad = str_repeat(chr(0x5c), $blocksize); return $hash(($key^$opad) . pack('H*', $hash(($key^$ipad) . $data))); } // Remember to initialize MT (using mt_srand() ) if required function pw_encode($password) { $seed = substr('00' . dechex(mt_rand()), -3) . substr('00' . dechex(mt_rand()), -3) . substr('0' . dechex(mt_rand()), -2); return hmac($seed, $password, 'md5', 64) . $seed; } function pw_check($password, $stored_value) { $seed = substr($stored_value, 32, 8); return hmac($seed, $password, 'md5', 64) . $seed==$stored_value; } // Test $password = 'foobar'; $encoded = pw_encode($password); $result = pw_check ($password, $encoded) ? 'true' : 'false'; echo<<<END password: $password encoded : $encoded rsult : $result END; ?>

« previous php.notes (#181616) next »