0

我正在开发一个游戏网站。帐户是通过基于 php 的网站创建的,游戏登录服务器正在 Python 中进行原型设计,并将在 C 中完成。我遇到的问题是,当我在 PHP 中散列某些内容时,我无法重现相同的结果在 Python 中使用相同的起始数据和盐。我在这里查看了 PHP 中的算法http://www.akkadia.org/drepper/SHA-crypt.txt并将其与我的操作方式进行了比较,并注意到 PHP 在最后切断了空字节。虽然,除此之外,我没有太多的运气。

Python:

def HashPassword(Salt,Password,Rounds=5000):
    passhash=hashlib.sha512(Password+Salt).digest()[:-1]
    for i in xrange(2,Rounds):
        passhash=hashlib.sha512(passhash+Salt).digest()[:-1]
    return passhash

PHP:

function HashPW($password, $salt = "")
{
    if(strlen($salt) == 0) $salt = RandChar(16);
    return crypt($password, '$6$rounds=5000$' . $salt . '$');
}
4

2 回答 2

1

这是一些更简单的 Python 代码。

import crypt
def hashPassword(salt, password, rounds=5000):
    return crypt.crypt(password, '$6$rounds={:d}${}$'.format(rounds, salt))
于 2011-05-17T04:52:07.773 回答
0

python 中的字符串末尾不包含空字节。

并且range(2,5000)只会循环 4998 次。

于 2011-05-17T05:07:35.880 回答