使用Rfc2898DeriveBytes将C# PBKDF2转换为PHP

mwg9r5ms  于 2022-12-21  发布在  PHP
关注(0)|答案(3)|浏览(152)

长话短说,有一个内置在.NET的会员系统,我们正在移植到WordPress的,需要复制PBKDF2加密,使用户不需要重置他们的密码。
使用已知的散列密码,我已经能够在.NET中轻松地复制此操作,代码如下:

static void Main(string[] args)
{
  var isValid = CheckPassword("#0zEZcD7uNmv", "5SyOX+Rbclzvvit3MEM2nBRaPVo2M7ZTs7n3znXTfyW4OhwTlJLvpcUlCryblgkQ");
}

public static int PBKDF2IterCount = 10000;
public static int PBKDF2SubkeyLength = 256 / 8; // 256 bits
public static int SaltSize = 128 / 8; // 128 bits

private static bool CheckPassword(string Password, string ExistingHashedPassword)
{
  byte[] saltAndPassword = Convert.FromBase64String(ExistingHashedPassword);
  byte[] salt = new byte[SaltSize];

  Array.Copy(saltAndPassword, 0, salt, 0, SaltSize);

  Console.WriteLine("--Salt--");
  Console.WriteLine(Convert.ToBase64String(salt));

  string hashedPassword = HashPassword(Password, salt);

  Console.WriteLine("--HashedPassword--");
  Console.WriteLine(hashedPassword);

  return hashedPassword == ExistingHashedPassword;
}

private static string HashPassword(string Password, byte[] salt)
{
  byte[] hash = new byte[PBKDF2SubkeyLength];
  using (var pbkdf2 = new Rfc2898DeriveBytes(Password, salt, PBKDF2IterCount))
  {
    hash = pbkdf2.GetBytes(PBKDF2SubkeyLength);
  }

  byte[] hashBytes = new byte[PBKDF2SubkeyLength + SaltSize];
  Array.Copy(salt, 0, hashBytes, 0, SaltSize);
  Array.Copy(hash, 0, hashBytes, SaltSize, PBKDF2SubkeyLength);

  string hashedPassword = Convert.ToBase64String(hashBytes);
  return hashedPassword;
}

控制台应用程序将生成以下内容:

--Salt--
5SyOX+Rbclzvvit3MEM2nA==
--HashedPassword--
5SyOX+Rbclzvvit3MEM2nBRaPVo2M7ZTs7n3znXTfyW4OhwTlJLvpcUlCryblgkQ
--IsValid--
True

然而在PHP方面我不能得到相同的结果。我的代码到目前为止如下。

$mySalt = base64_decode('5SyOX+Rbclzvvit3MEM2nA==');
$dev = pbkdf2('sha1', '#0zEZcD7uNmv', $mySalt, 10000, 48, true);
$key = substr($dev, 0, 32); //Keylength: 32
$iv = substr($dev, 32, 16); // IV-length: 16

echo 'PHP<br/>';
echo 'PASS: '.base64_encode($dev).'<br/>';
echo 'SALT: '.base64_encode($iv).'<br/><br/>'; 

echo '.NET<br/>';
echo 'PASS: 5SyOX+Rbclzvvit3MEM2nBRaPVo2M7ZTs7n3znXTfyW4OhwTlJLvpcUlCryblgkQ<br/>';
echo 'SALT: 5SyOX+Rbclzvvit3MEM2nA==<br/><br/>'; 

function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
{
    $algorithm = strtolower($algorithm);
    if(!in_array($algorithm, hash_algos(), true))
        die('PBKDF2 ERROR: Invalid hash algorithm.');
    if($count <= 0 || $key_length <= 0)
        die('PBKDF2 ERROR: Invalid parameters.');

    $hash_length = strlen(hash($algorithm, "", true));
    $block_count = ceil($key_length / $hash_length);

    $output = "";
    for($i = 1; $i <= $block_count; $i++) {
        // $i encoded as 4 bytes, big endian.
        $last = $salt . pack("N", $i);
        // first iteration
        $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
        // perform the other $count - 1 iterations
        for ($j = 1; $j < $count; $j++) {
            $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
        }
        $output .= $xorsum;
    }
    return substr($output, 0, $key_length);
}

结果是:

PHP
PASS: FFo9WjYztlOzuffOddN/Jbg6HBOUku+lxSUKvJuWCRCsYe+1Tgbb8Ob4FtxumMal
SALT: rGHvtU4G2/Dm+BbcbpjGpQ==

.NET
PASS: 5SyOX+Rbclzvvit3MEM2nBRaPVo2M7ZTs7n3znXTfyW4OhwTlJLvpcUlCryblgkQ
SALT: 5SyOX+Rbclzvvit3MEM2nA==

任何帮助都将不胜感激。

yuvru6vn

yuvru6vn1#

最后使用https://github.com/defuse/password-hashing库使其工作,做了一些小的修改,以匹配我正在导入的数据库的哈希格式。
但我的主要问题是这些行,我试图从散列中得到一个键。

$dev = pbkdf2('sha1', '#0zEZcD7uNmv', $mySalt, 10000, 48, true);
$key = substr($dev, 0, 32); //Keylength: 32
$iv = substr($dev, 32, 16); // IV-length: 16

将其更改为以下内容,以便创建32位长的散列散列,并将返回的散列加入salt,从而解决了这个问题。

$dev = pbkdf2('sha1', '#0zEZcD7uNmv', $mySalt, 10000, 32, true);
echo 'PASS: '.base64_encode($mySalt.$dev).'<br />';

下面的输出现在与.NET匹配:

PASS: 5SyOX+Rbclzvvit3MEM2nBRaPVo2M7ZTs7n3znXTfyW4OhwTlJLvpcUlCryblgkQ
h79rfbju

h79rfbju2#

我在寻找将密码从遗留的Asp.NetMVC应用程序迁移到Laravel的方法时遇到了这篇文章。
对于那些只对比较生成的哈希值感兴趣的人(例如,出于身份验证目的),请考虑以下几点:

function legacyHashCheck($hash, $password)
{
    $raw     = base64_decode($hash);
    $salt    = substr($raw, 1, 16);
    $payload = substr($raw, 17, 32);

    //new Rfc2898DeriveBytes(password, salt, 1000).GetBytes(32)
    $check   = hash_pbkdf2('sha1', $password, $salt, 1000, 32, true);

    return $payload === $check;
}
7lrncoxx

7lrncoxx3#

看来. NET核心现在实现了2种格式(2022年)。
https://github.com/dotnet/AspNetCore/blob/main/src/Identity/Extensions.Core/src/PasswordHasher.cs
我需要为Laravel实现这两个功能,因此以下是我的贡献:

private function dotNetVerifyHash($hash, $password) {
    $version = ord($hash[0]);
    if ($version !== 0 && $version !== 1) {
        throw new \Exception('wrong version header: ' . $version);
    }
    if ($version === 0) {
        // Format: { 0x00, salt, subkey }
        $iterations = 1000;
        $subKeyLength = 32;
        $saltSize = 16;
        $salt = substr($hash, 1, $saltSize);
        $derived = hash_pbkdf2('sha1', $password, $salt, $iterations, $subKeyLength, true);
        $newHash = chr(0x00) . $salt . $derived;
    } else if ($version === 1) {
        // Format: { 0x01, prf (UInt32), iter count (UInt32), salt length (UInt32), salt, subkey }
        $unp = unpack('N3', substr($hash, 1, 12));
        $prf = $unp[1];
        $algorithm = '';
        switch ($prf) {
            case 0: $algorithm = 'sha1'; break;
            case 1: $algorithm = 'sha256'; break;
            case 2: $algorithm = 'sha512'; break;
            default: throw new \Exception('invalid prf: ' . $prf);
        }
        $iterations = $unp[2];
        $saltLength = $unp[3];
        $subKeyLength = 32;
        $salt = substr($hash, 13, $saltLength);
        $derived = hash_pbkdf2($algorithm, $password, $salt, $iterations, $subKeyLength, true);
        $newHash = chr(0x01) . pack('N3', $prf, $iterations, $saltLength) . $salt . $derived;

    }
    return $hash === $newHash;
}
function dotNetCreateHash($password, $version = 1) {
    if ($version !== 0 && $version !== 1) {
        throw new \Exception('invalid version: ' . ord($hash[0]));
    }

    $salt = Str::random(16);
    if ($version === 0) {
        // Format: { 0x00, salt, subkey }
        $dev = hash_pbkdf2('sha1', $password, $salt, 1000, 32, true);
        return base64_encode(chr(0x00) . $salt . $dev);
    } else if ($version === 1) {
        // Format: { 0x01, prf (UInt32), iter count (UInt32), salt length (UInt32), salt, subkey }
        $algorithm = 'sha256';
        $prf = 1;
        $iterations = 10000;
        $saltLength = strlen($salt);
        $subKeyLength = 32;
        $derived = hash_pbkdf2($algorithm, $password, $salt, $iterations, $subKeyLength, true);
        return base64_encode(chr(0x01) . pack('N3', $prf, $iterations, $saltLength) . $salt . $derived);
    }
}

您还可以使用自定义hasher扩展Laravel:https://gist.github.com/tonila/5719aea8ad57df6821d7acdd1ed4ef1a

相关问题