简体   繁体   中英

Hashing Results From C# and Java are Different

I'm trying to hash data "text" to be transferred from Java Service to C# Service. I'm using SHA256 as a Hashing algorithm, but despite the values and the salt being the same the result doesn't.

Here is my C# snippet

public string Sign(string textToHash, string salt){
    byte[] convertedHash = new byte[salt.Length / 2];
    for (int i = 0; i < salt.Length / 2; i++)
        convertedHash[i] = (byte)int.Parse(salt.Substring(i * 2, 2), NumberStyles.HexNumber);
        
    HMAC hasher = new HMACSHA256(convertedHash);
       
    string hexHash = "";
    using (hasher)
    {
        byte[] hashValue = hasher.ComputeHash(Encoding.UTF8.GetBytes(textToHash));
        foreach (byte b in hashValue)
        {
            hexHash += b.ToString("X2");
        }
    }
    return hexHash;
}

And, here is the Java snippet

public static String sign(String textToHash, String salt){
    
    byte[] convertedHash = new byte[salt.length() / 2];

    for (int i = 0; i < salt.length() / 2; i++)
    {
        convertedHash[i] = (byte)Integer.parseInt(salt.substring(i * 2, i * 2 + 2),16);
    }
    String hashedText = null;
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(convertedHash);
        byte[] bytes = md.digest(textToHash.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
        }
        hashedText = sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return hashedText;
}

In Java, I also tried

convertedHash = salt.getBytes();

But I got different results also.

Tests:

salt = ABCDEFG
text = hashme

Result in C#

70B38047C28FFEDCF7275C428E65310671CADB65F11A5C9A8CFBB3CF52112BA3

Result in Java

a8bc36606aade01591a1d12c8b3c87aca1fe55def79740def03a90b49f2c6b7c

So, any help about why the results aren't the same.

Thanks in advance.

To mimic the Java hashing, I used SHA256Managed rather than HMACSHA256 in C#

public static string Sign(string data, string salt)
{
    UTF8Encoding encoder = new UTF8Encoding();
    SHA256Managed sha256hasher = new SHA256Managed();
    byte[] convertedHash = new byte[salt.Length / 2];

    for (int i = 0; i < salt.Length / 2; i++)
        convertedHash[i] = (byte)int.Parse(salt.Substring(i * 2, 2), NumberStyles.HexNumber);

    byte[] dataBytes = encoder.GetBytes(data);
    byte[] bytes = new byte[convertedHash.Length + dataBytes.Length];
    
    Array.Copy(convertedHash, bytes, convertedHash.Length);
    Array.Copy(dataBytes, 0, bytes, convertedHash.Length, dataBytes.Length);
    byte[] hashedBytes = sha256hasher.ComputeHash(bytes);

    return hashedBytes.Aggregate("", (current, t) => current + t.ToString("X2"));
}

HMACSHA256 is not a pure SHA-256 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM