1

I'm writing a little program based off of a Python code that I have found. There is a few lines I need help with. It is about hasing a value using SHA256 encryption.

The python code is as follows:

first = hashlib.sha256((valueOne + valueTwo).encode()).hexdigest()
second = hashlib.sha256(str(timestamp) + value).encode()).hexdigest()

And when I execute it, my values are as follows:

first: 93046e57a3c183186e9e24ebfda7ca04e7eb4d8119060a8a39b48014d4c5172b
second: bde1c946749f6716fde713d46363d90846a841ad56a4cf7eaccbb33aa1eb1b70

My C# code is:

string first = sha256_hash((secret + auth_token));
string second = sha256_hash((timestamp.ToString() + secret));

And when I execute it, my values are:

first: 9346e57a3c183186e9e24ebfda7ca4e7eb4d81196a8a39b48014d4c5172b   
second: bde1c946749f6716fde713d46363d9846a841ad56a4cf7eaccbb33aa1eb1b70

As you can see, the values are slightly different. The python code returns two values BOTH with the length of 64 characters, where as in C# the values are 60 characters and 63 characters respectively.

My sha256_hash method is from here: Obtain SHA-256 string of a string

Any help would be appreciated, thanks.

Community
  • 1
  • 1
user2764359
  • 127
  • 3
  • 18
  • 3
    Your C# output has dropped three `0` characters, that looks like a problem with how you printed that data more than how you produced the hash digests. Apart from those three missing `0` digits, the hashes match. – Martijn Pieters Jan 08 '14 at 12:19
  • @oleksii My first string now went to: fd72a372c9c76ab3aa28f52ba722d1e530011efab6116cee9e49e414c16c638 which is 63 characters long, and the second one went to: e5b76f4165491067188fb49de0db8ac2877fa0b9cbc222d998a1ff891b556a60 which is 64 characters long. – user2764359 Jan 08 '14 at 12:23
  • @MartijnPieters My program then goes on to take characters away from both of these strings, and it relies on them being 64 characters long, however it throws an error because some characters are missing. – user2764359 Jan 08 '14 at 12:28
  • [Duplicate](http://stackoverflow.com/questions/20479980/q-im-getting-slightly-different-hmac-signatures-out-of-clojure-and-python/), though not the same programming language. – ntoskrnl Jan 08 '14 at 12:32
  • @ntoskrnl That didn't work. It gives a massive value for each one with heaps of % signs and 'x's. – user2764359 Jan 08 '14 at 12:34
  • Of course it works. But it's Java, not C#. – ntoskrnl Jan 08 '14 at 12:34
  • @ntoskrnl Well it didn't work for C#. Could you suggest any way to implement that into C#? – user2764359 Jan 08 '14 at 12:36
  • Okay so as @MartijnPieters said it seems that it's something wrong with perhaps the encoding of it, as it just drops the 0 character. – user2764359 Jan 08 '14 at 12:40
  • @user2764359: have you tried my edited answer yet? – Martijn Pieters Jan 08 '14 at 12:41
  • @MartijnPieters About how I print the data? The thing is my program then uses these variables, so it's not how I print them. Unless I am understanding you incorrectly? – user2764359 Jan 08 '14 at 12:43
  • 1
    @user2764359: I already found the problem, see my answer again. My analysis was certainly correct, it was only the exact syntax of the `b.ToString()` call that eluded me. Have you tried the latest edit yet? – Martijn Pieters Jan 08 '14 at 12:51
  • See [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) for many different ways of converting to hex in C#. Pick whichever one you like. – CodesInChaos Jan 13 '14 at 10:08

1 Answers1

5

Your hex digest method is not producing length-2 hex values for bytes < 16. The byte \x08 is being added to your hex output as just '8' instead of '08', leading to an output that is too short.

Adjust the format to produce 0-padded hex characters:

foreach (Byte b in result)
    Sb.Append(b.ToString("x2"));

See Standard Numeric Format Strings for more information on how to format bytes to hexadecimal strings.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • That made my first string go to: 1472x42x1102x872x1632x1932x1312x242x1102x1582x362x2352x2532x1672x2022x42x2312x2352x772x1292x252x62x102x1382x572x1802x1282x202x2122x1972x232x432x and my second string go to: 1892x2252x2012x702x1162x1592x1032x222x2532x2312x192x2122x992x992x2172x82x702x1682x652x1732x862x1642x2072x1262x1722x2032x1792x582x1612x2352x272x1122x – user2764359 Jan 08 '14 at 12:25
  • @user2764359: Sorry, I'm not a C# expert (by a long shot); I guessed at the `ToString()` format before looking it up.. – Martijn Pieters Jan 08 '14 at 12:26