嘗試使用PHP對現(xiàn)有ASP.NET成員資格數(shù)據(jù)庫進(jìn)行身份驗(yàn)證時,我遇到了一些問題.我在網(wǎng)上搜索過,我發(fā)現(xiàn)的現(xiàn)有答案似乎對我不起作用.即:
public static function Hash($password, $salt){ $decodedSalt = base64_decode($salt); $utf = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8'); return base64_encode(sha1($decodedSalt.$utf, true));}
我認(rèn)為問題的一部分是密碼哈希實(shí)際上不是用SHA-1計(jì)算的,因?yàn)閿?shù)據(jù)庫中的值是44個字符長,base64編碼的字符串(這意味著輸入可能是256位長).我曾嘗試使用SHA-256代替SHA-1,但無濟(jì)于事.我在web.config中找不到一個機(jī)器密鑰,它可以進(jìn)一步腌制哈希值,當(dāng)我在本地運(yùn)行或在生產(chǎn)服務(wù)器上運(yùn)行時,ASP.NET站點(diǎn)生成相同的哈希值,所以我不知道為什么它們不是不匹配.
Web.config成員資格提供程序:
<add connectionStringName="MySqlMembershipConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" autogenerateschema="true" name="MySqlMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, mysql.web" />
應(yīng)該起作用的示例密碼:
$salt = 'Mu1tp8XzfKl8dSTVAZm44A=='; // Straight from the DB$password = 'testing';$expectedHash = 'TQN7m8OWIyBOKVwzegWSUBVq7o7 KWFBc46J B77mLw=' // Straight from the DB// When using the above Hash function with SHA-256 instead of SHA-1$generatedHash = 'rpmTmtBfWoJz71ooQGQUIIyQJKd99qhYxMUI1yda0qE='
思考?知道為什么我的哈希值與數(shù)據(jù)庫中存儲的內(nèi)容不匹配(以及通過ASP.NET站點(diǎn)登錄時原因和方式如何)?我已經(jīng)嘗試更換哈希函數(shù),反轉(zhuǎn)密碼/鹽連接,并在敲擊我的計(jì)算機(jī)時大聲喊叫,但這些似乎都沒有幫助.
解決方法:
我看了一下你鏈接到的身份驗(yàn)證問題頁面,一個特別的答案引起了我的注意:https://stackoverflow.com/a/4227642/633098.
因?yàn)槟阏f使用的算法不再是SHA1,但更可能是SHA256我開始嘗試使用HMAC哈希,而不是SHA256.它起初沒有用,但后來我嘗試使用由密碼和鹽組成的連接字符串和salt(= key)本身,并且它起作用.
這是我做的簡單功能:
function _hash($password, $salt) { return base64_encode(hash_hmac('sha256', base64_decode($salt) . iconv('UTF-8', 'UTF-16LE', $password), base64_decode($salt), true));}$salt = 'Mu1tp8XzfKl8dSTVAZm44A=='; // Straight from the DB$password = 'testing';var_dump(_hash($password, $salt));
來源:https://www.icode9.com/content-1-266001.htmlResulting hash: TQN7m8OWIyBOKVwzegWSUBVq7o7 KWFBc46J B77mLw=