user3339142
user3339142

Reputation: 9

PHP SHA-256 not the same as this c# code?

Got a problem - maybe... It seems that this piece of PHP:

$mydigestb = hash("sha256",$digeststring);

does not generate the same result as this C# code:

private string GenerateDigest(long currentTime)
{
    SHA256Managed hashString = new SHA256Managed();
    StringBuilder hex = new StringBuilder();
    byte[] hashValue = hashString.ComputeHash(Encoding.UTF8.GetBytes(String.Format("{0}{1}", currentTime, txtApiKey.Text)));
    foreach (byte x in hashValue)
    {
        hex.AppendFormat("{0:x2}", x);
    }
    return hex.ToString();
}

The input values are the same, but it appears that what comes out is different.

Upvotes: 0

Views: 1376

Answers (1)

Abhinav
Abhinav

Reputation: 2085

It is the same for me:

PHP code: http://codepad.org/gcGC5Omp

<?php
$mydigestb = hash("sha256" , "abhinav" );
echo $mydigestb;
?>

C# code: https://ideone.com/jrz05O

using System;

public class Test
{
    public static void Main()
    {
        System.Security.Cryptography.SHA256Managed hm = new System.Security.Cryptography.SHA256Managed();
        byte[] hashValue = hm.ComputeHash(System.Text.Encoding.ASCII.GetBytes("abhinav"));
        Console.WriteLine(System.BitConverter.ToString(hashValue).Replace("-", "").ToLower());
    }
}

Are you using the same sample data?

Upvotes: 1

Related Questions