note 30347 added to ref.mysql

From: Date: Fri, 14 Mar 2003 19:23:44 +0000
Subject: note 30347 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-45317@lists.php.net to get a copy of this message
Let's say that you want to generate a MySQL password hash from a plain text password. Normally, you would just submit the MySQL query "SELECT PASSWORD('password')", but if for some reason you can't access to MySQL database directly, then you can use the following function (translated right out of the MySQL source code): function mysql_password($passStr) { $nr=0x50305735; $nr2=0x12345671; $add=7; $charArr = preg_split("//", $passStr); foreach ($charArr as $char) { if (($char == '') || ($char == ' ') || ($char == '\t')) continue; $charVal = ord($char); $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); $nr2 += ($nr2 << 8) ^ $nr; $add += $charVal; } return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); } example: <? print mysql_password("hello"); ?> outputs: 70de51425df9d787 Which is the same result you get if you do "SELECT PASSWORD('hello')" directly in MySQL. Hopefully you'll never be in a situation where you have to use this, but if you need it (like I did), it's here. -- http://www.php.net/manual/en/ref.mysql.php http://master.php.net/manage/user-notes.php?action=edit+30347 http://master.php.net/manage/user-notes.php?action=delete+30347 http://master.php.net/manage/user-notes.php?action=reject+30347

« previous php.notes (#45317) next »