I have a website that connects to another external website and passes ID numbers, the page I am working on and that generates the links is written in c#, whereas the target website is written in Java and I am experiences some mismatches
I am trying to use the same approach in C# as the code in Java to encrypt the ID's, the code looks like this:
public static String shaHash(String input)
throws NoSuchAlgorithmException {
if (input == null) {
return null;
}
MessageDigest sha = MessageDigest.getInstance("SHA");
try {
sha.update(input.getBytes("UTF8"));
BigInteger hash = new BigInteger(1, sha.digest());
return hash.toString(16);
} catch (UnsupportedEncodingException e) {}
return null;
}
And the C# code looks like
StringBuilder convertedId = new StringBuilder();
var idToByte = Encoding.ASCII.GetBytes(id);
using (SHA1 sha1 = SHA1.Create())
{
var bytes = sha1.ComputeHash(idToByte);
for (int i = 0; i < bytes.Length; i++)
{
convertedId.Append(bytes[i].ToString("x2"));
}
}
return convertedId.ToString();
But the return values are different as the C# code seems like it is adding an extra 0 at the beginning:
Java: b83caf2f739209c42a8d02df0f6d4794f288703, and C#: 0b83caf2f739209c42a8d02df0f6d4794f288703
What would it be the problem? I have tried to change the encoding in the C# code for UTF8 but still doesn't work.
Any ideas guys?
Many thanks in advance