Same string, different SHA1 hash values obtained from VB.net and PHP

前端 未结 1 1986
独厮守ぢ
独厮守ぢ 2020-12-20 23:40

I have some problems with the SHA1 hash value of a string. I\'m trying to send a file from a client written in VB.net to a server written in PHP. My problem is that when I p

相关标签:
1条回答
  • 2020-12-21 00:07

    Well, the problem is that you're double hashing in .NET and only single hashing in PHP. Here's what you're doing in .NET translated into PHP:

    $cInput = "the quick brown fox jumps over the lazy dog";
    $cBase64 = base64_encode($cInput);
    
    $sha = sha1($cBase64, true); // The true param returns the raw bytes instead of hex
    $chash = sha1($sha);
    

    So you're double hashing it. To fix it, you just need to change your algorithm to:

    cInput = "the quick brown fox jumps over the lazy dog"
    cBase64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(cInput))
    
    abBytesToHash = System.Text.Encoding.ASCII.GetBytes(cBase64)
    
    cHash = BitConverter.ToString(objSHA1.ComputeHash(abBytesToHash))
    cHash = Replace(cHash, "-", "")
    

    Note that all I did was remove the abBytesToHash = objSHA1.ComputeHash(abBytesToHash) line...

    Alternatively, you could change the PHP to do this:

    $cInput = "the quick brown fox jumps over the lazy dog";
    $cBase64 = base64_encode($cInput);
    
    echo "BASE64: " . $cBase64 . "<br />";
    echo "SHA1: " . strtoupper(sha1(sha1($cBase64, true)));
    
    0 讨论(0)
提交回复
热议问题