0

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.

1
  • 3
    They are the same here for me. Show exact values for which you are testing it. Maybe character encoding differences? Commented Feb 21, 2014 at 22:29

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

You saved me. I was converting back to string incorrectly, and your snippet showed me the way. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.