加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

为什么我的PHP SHA256哈希值不等于C#SHA256管理哈希

发布时间:2020-12-13 16:38:08 所属栏目:PHP教程 来源:网络整理
导读:为什么这些不一样? PHP: $hash = hash('sha256',$userData['salt'] . hash('sha256',$password) ); C# public static string ComputeHash(string plainText,string salt) { // Convert plain text into a byte array. byte[] plainTextBytes = Encoding.UT
为什么这些不一样?

PHP:

$hash = hash('sha256',$userData['salt'] . hash('sha256',$password) );

C#

public static string ComputeHash(string plainText,string salt)
    {
        // Convert plain text into a byte array.
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        byte[] saltBytes = Encoding.UTF8.GetBytes(salt);

        SHA256Managed hash = new SHA256Managed();

        // Compute hash value of salt.
        byte[] plainHash = hash.ComputeHash(plainTextBytes);

        byte[] concat = new byte[plainHash.Length + saltBytes.Length];

        System.Buffer.BlockCopy(saltBytes,concat,saltBytes.Length);
        System.Buffer.BlockCopy(plainHash,saltBytes.Length,plainHash.Length);

        byte[] tHashBytes = hash.ComputeHash(concat);

        // Convert result into a base64-encoded string.
        string hashValue = Convert.ToBase64String(tHashBytes);

        // Return the result.
        return hashValue;
    }
C#正在输出一个base64生态编码的字符串,PHP正在输出十六进制数.更好的比较可能是将参数true传递给PHP和base64的hash函数的结尾:
$hash = base64_encode(
           hash('sha256',$password),true )
         );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读