Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以下.NET代码的PHP等效密码哈希_Php_Cryptography_Passwords_Sha512_Pbkdf2 - Fatal编程技术网

以下.NET代码的PHP等效密码哈希

以下.NET代码的PHP等效密码哈希,php,cryptography,passwords,sha512,pbkdf2,Php,Cryptography,Passwords,Sha512,Pbkdf2,我真的不想转储一些代码并期望得到答案,但这是一个相当长的函数,它对密码进行散列,以便稍后将其与数据库存储值进行比较 我看到过一些帖子,其中人们浪费时间试图重新创建他们在PHP中使用md5()函数可以实现的功能 出于这个原因,我想知道是否有具备加密知识的人知道用PHP实现以下效果的PHP等价物: internal static string GenerateEncryptedPassword(string password, string salt, int iterationCount)

我真的不想转储一些代码并期望得到答案,但这是一个相当长的函数,它对密码进行散列,以便稍后将其与数据库存储值进行比较

我看到过一些帖子,其中人们浪费时间试图重新创建他们在PHP中使用
md5()
函数可以实现的功能

出于这个原因,我想知道是否有具备加密知识的人知道用PHP实现以下效果的PHP等价物:

    internal static string GenerateEncryptedPassword(string password, string salt, int iterationCount)
    {
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
        byte[] iterationCountBytes = BitConverter.GetBytes(iterationCount);
        int derivedLength = passwordBytes.Length + saltBytes.Length;
        byte[] passwordSaltBytes = new byte[derivedLength];
        byte[] pbkdf2Bytes;
        string encryptedString;

        for (int i = 0; i < passwordBytes.Length; i++)
        {
            passwordSaltBytes[i] = passwordBytes[i];
        }

        for (int i = 0; i < saltBytes.Length; i++)
        {
            passwordSaltBytes[passwordBytes.Length + i] = saltBytes[i];
        }

        using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, passwordSaltBytes, iterationCount))
        {
            pbkdf2Bytes = pbkdf2.GetBytes(derivedLength + iterationCountBytes.Length);
        }

        using (SHA512 sha512 = new SHA512Managed())
        {
            byte[] hashBytes = sha512.ComputeHash(pbkdf2Bytes);
            byte[] hashSaltBytes = new byte[hashBytes.Length + saltBytes.Length];

            for (int i = 0; i < hashBytes.Length; i++)
            {
                hashSaltBytes[i] = hashBytes[i];
            }

            for (int i = 0; i < saltBytes.Length; i++)
            {
                hashSaltBytes[hashBytes.Length + i] = saltBytes[i];
            }

            encryptedString = Convert.ToBase64String(hashSaltBytes);
        }

        return encryptedString;
    }

阅读PHP手册了解密码\u散列和密码\u验证。我建议使用BCrypt作为算法

openssl_pbkdf2(password, saltBytes, keyLength, iterationCount, 'sha512')

一点也不难!祝你好运!:-)


另外,SHA-512也不是那么安全。阅读这里:

我已经多年没有使用PHP了,所以不确定是否有新的方法来做事情,但您可以使用OpenSSL生成SHA-512哈希:

要生成salt,强烈建议使用安全(不可预测)随机数。对于PHP,请参见以下内容:

编辑: 您还可以直接使用OpenSSL生成pbkfd2:

只需注意函数签名末尾定义摘要算法的可选参数

openssl_pbkdf2(password, saltBytes, keyLength, iterationCount, 'sha512')

问题是,我必须重新创造我的同事已经做过的,所以我不能使用其他任何东西。它必须输出相同的散列。在这里发布之前,我确实尝试了一些方法,但我会再看一次,谢谢。您也可以使用SHA512尝试密码\u hash(),检查选项!再次,我查看了选项,显然我已经运行了好几十个组合和重载,这就是为什么我在这里粘贴代码,希望有人可能知道一种在PHP中方便快捷地完成此操作的方法。谢谢你的评论。这支持盐类吗?我做了一个编辑来回答你的问题:)一般来说,你可以散列任何你想要的数据。在您的代码片段中,您已经连接了salt+密码,这很好。感谢您的帮助。:)没问题。希望你能把它整理好。谢谢-我的头还在为这个哈哈大笑而爆炸。什么都想不出来,完全被挡住了。
openssl_pbkdf2(password, saltBytes, keyLength, iterationCount, 'sha512')