0

I am getting different values when using shA256.ComputeHash(hexByte) from C# and byte[] hash = digest.digest(); in Java. I get the same values in C# and Java when converting stringToHexByte(). Please see how I do it below.

C#

protected static string ComputeHash(string str)
{
  if (str.Length % 2 == 1)
    str += "0";
  SHA256 shA256 = (SHA256) new SHA256CryptoServiceProvider();
  str += "110983".ToString();
  byte[] hexByte = StringToHexByte(str); // hexByte same as JAVA hexByte
  byte[] hash = shA256.ComputeHash(hexByte);
  return string.Empty();
}

protected static byte[] StringToHexByte(string str)
{
  int length1 = str.Length / 2;
  byte[] numArray = new byte[length1];
  int length2 = str.Length;
  for (int index = 0; index <= length1 - 1; ++index)
    numArray[index] = length2 - index * 2 <= 1 ? 
        byte.Parse(str.Substring(index * 2, 2)) :
        byte.Parse(str.Substring(index * 2, 2));
  return numArray;
}

Java

private static String computeHash(String str) throws NoSuchAlgorithmException
{
    if (str.length() % 2 == 1)
        str += "0";
    str += "110983".toString();
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();
    
    byte[] hexByte = stringToHexByteLcs(str); // hexByte same as C# hexByte
    digest.update(hexByte);
    byte[] hash = digest.digest();
    String str1 = convertToHexString(hash, 0, hash.length)
}

private static byte[] stringToHexByteLcs(String str) {
    int length1 = str.length()/2;
    byte[] numArray = new byte[length1];
    int length2 = str.length();
    for (int index = 0; index < length1; ++index) {
        numArray[index] = Byte.parseByte(length2 - index * 2 <= 1 ?
                new String(str.getBytes(), index * 2, 2) :
                new String(str.getBytes(), index * 2, 2));
    }
    return numArray;
}

I get the same number of arrays, but the values are different. In Java I get negative values while in C# all are positive values.

C#

enter image description here

Java

enter image description here

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ibanez1408
  • 4,550
  • 10
  • 59
  • 110
  • 1
    Code is not correctly formatted. – Sıddık Açıl Apr 25 '22 at 06:54
  • @SıddıkAçıl So sorry about that. It's formatted now. Thank you. – Ibanez1408 Apr 25 '22 at 06:58
  • 3
    "In java I get negative values while in C# all are positive values." Yes, because `byte` is signed in Java and unsigned in C#. You've got the same result, basically. – Jon Skeet Apr 25 '22 at 07:08
  • 1
    c# byte is unsigned so 0 to 255, java byte is signed so: ~-128 to ~128 – Patrick Beynio Apr 25 '22 at 07:08
  • @JonSkeet so would this mean that on hash[1] in Java I have -62 it equal to hash[1] in C# with value of 194? – Ibanez1408 Apr 25 '22 at 07:11
  • @PatrickBeynio So for both languages I have the same language? Because this does not stop there. Somewhere below that, I still have to do some encryption. So I want to know that I have the same value so I would get the same encryption. – Ibanez1408 Apr 25 '22 at 07:13
  • 2
    Yes - an unsigned byte with a value 194 is the same bit pattern as a signed byte with a value of -62. (Convert them both into hex strings and you should see the same result...) – Jon Skeet Apr 25 '22 at 07:15
  • 1
    @Ibanez1408 yes, if you want to make sure could just cast it to a `sbyte`, the C# equivalent signed byte to check. but yes both will have the same binary representation – Patrick Beynio Apr 25 '22 at 07:15

0 Answers0