2013-06-12 17 views
1

我正在導出,通過與http請求刮,因爲主機不會給我數據庫訪問,論壇和導入到一個MySQL數據庫vbulletin。Python是從PHP生成的鹽,並存儲在Mysql

在vBulletin用戶擁有唯一的密碼鹽,並生成使用該算法的密碼哈希值:

$hash = md5(md5($plaintext) . $salt); 

我使用python腳本從用戶信息我的SQLite數據庫讀取存儲數據,以產生新密碼和salt,然後將這些數據存儲到mysql數據庫中,以便vbulletin可以使用它。

我的問題是,python以意想不到的方式改變字符串的值。我正在測試我的腳本,使用已知的密碼和鹽來比較哈希值。但是,我使用的鹽是這樣的:

D(A\3*w/lo6Coo\Mc,[email protected]&R 

當我試圖存儲在蟒蛇,當我檢索字符串我得到這個:

D(A\x03*w/lo6Coo\\Mc,[email protected]&R 

這是Python代碼我使用模仿vBulletin哈希算法「M:

salt = 'D(A\3*w/lo6Coo\Mc,[email protected]&R' 

password = hashlib.md5() 
password.update('*snipped password*') 
password = password.hexdigest() 

password_hash = hashlib.md5() 
password_hash.update(password + salt) 
password_hash = password_hash.hexdigest() 

爲了比較,這個PHP匹配什麼vBulletin存儲在數據庫中的密碼哈希:

$plaintext = '*snipped password*'; 
$salt = 'D(A\3*w/lo6Coo\Mc,[email protected]&R'; 

$hash = md5(md5($plaintext) . $salt); 

$ hash與vbulletin存儲和password_hash不匹配。我做錯了什麼導致差異?

回答

2

你定義的方式salt是問題所在。你必須要麼使之成爲原始字符串:

salt = r'D(A\3*w/lo6Coo\Mc,[email protected]&R' 

或者逃避反斜槓:

salt = 'D(A\\3*w/lo6Coo\\Mc,[email protected]&R' 

否則,\3被解釋爲與3字符代碼字符轉義序列。

此外,爲便於閱讀,你可能想使一個md5功能:

def md5(text): 
    return hashlib.md5(text).hexdigest() 

hash = md5(md5(plaintext) + salt) 
+0

我發誓,我已經嘗試過爲原料和Unicode都在這裏發帖之前,並沒有工作,但現在它的工作原理。謝謝您的幫助。 –