note 30347 modified in ref.mysql by aidan

From: Date: Sun, 15 Aug 2004 11:05:09 +0000
Subject: note 30347 modified in ref.mysql by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74688@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): <?php 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. --was-- 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://php.net/manual/en/ref.mysql.php

« previous php.notes (#74688) next »