我需要根据 PHP 中的 ASP.NET 成员表进行身份验证。成员(member) API 配置为使用散列密码。
有人可以给我 php 来散列来自登录表单的密码并将其与 sql 字段进行比较吗?
我知道我传递的密码是正确的,但它的哈希值不同。
private function Auth( $username, $password )
{
// Hashed password in db
$hash = $this->parent->_memberData['conv_password'];
// password passed from form
$bytes = mb_convert_encoding($password, 'UTF-7');
// Salt from db
$salt = base64_decode($this->parent->_memberData['misc']);
// hash password from form with salt
$hashedpassword = base64_encode(sha1($salt . $bytes, true));
// Test em out
if ($hashedpassword == $hash)
{
$this->return_code = "SUCCESS";
return true;
}
else
{
$this->return_code = "WRONG_AUTH";
return false;
}
}
更新:
我尝试了不同的编码但结果相同。 UTF-7、UTF-8 和 UTF-16。
更新: 我已经为此奋斗了一个星期了。赏金即将到来......
这是单元测试形式的 .net 代码。单元测试有效,值直接来自数据库。这段代码到 php 的正确翻译是什么?
public void EncodePassword()
{
string expected = "aP/mqBu3VkX+rIna42ramuosS3s=";
string salt = "urIaGX0zd/oBRMDZjc1CKw==";
string pass = "Comeonman";
byte[] bytes = Encoding.Unicode.GetBytes(pass);
byte[] numArray = Convert.FromBase64String(salt);
byte[] numArray1 = new byte[(int)numArray.Length + (int)bytes.Length];
byte[] numArray2 = null;
Buffer.BlockCopy(numArray, 0, numArray1, 0, (int)numArray.Length);
Buffer.BlockCopy(bytes, 0, numArray1, (int)numArray.Length, (int)bytes.Length);
HashAlgorithm hashAlgorithm = HashAlgorithm.Create("SHA1");
if (hashAlgorithm != null)
{
numArray2 = hashAlgorithm.ComputeHash(numArray1);
}
Assert.AreEqual(Convert.ToBase64String(numArray2), expected);
}
最佳答案
$password = 'Comeonman';
$salt = base64_decode('urIaGX0zd/oBRMDZjc1CKw==');
$utf16Password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
echo base64_encode(sha1($salt.$utf16Password, true));
关于php - 在 PHP 中使用 ASP.NET 成员资格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407494/