我正在开发一个服务器控制面板,但我没有能够在mysql数据库中正确设置用户密码。
用于存储密码的mysql查询如下:
ENCRYPT('firstpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))
然而,我还不能在php中复制结果(当然,使用静态盐):它们就是不匹配。
这是我一直在使用的php代码:
$salt = '1234567890123456';
$password = 'hello';
$hexhash = hash('sha512', '$6$'. $salt);
// bda6a11cc3aa00b8fe46f0559e86b3e4be3a6646f7dca2df29da1db86cfd7fc0ceb9ca076d16f0296b82e120170f08e049f607cc6e2d7328976f8cb4e8cacf98
$binhash = hex2bin($hexhash);
$hash = base64_encode($binhash);
// vaahHMOqALj+RvBVnoaz5L46Zkb33KLfKdoduGz9f8DOucoHbRbwKWuC4SAXDwjgSfYHzG4tcyiXb4y06MrPmA==
密码应该改为12NKz5XM5JeKI
。mysql和php的实现有什么不同吗?
发现,与我在谷歌上发现的相反(有些人试图使用sha512哈希),MySQL ENCRYPT
函数只使用普通的unix crypt
库,所以在PHP中实现它真的很微不足道。
代码如下:
$password = 'firstpassword';
$salt = '$6$' . '6bfd195afba021a2';
// or, if you want the random version,
$password = 'firstpassword';
$salt = '$6$' . substr(md5(microtime()),16);
// And finally the generation
echo crypt($password, $salt);
很遗憾,在互联网上没有任何地方有这个代码,这就是为什么我和你分享它:)