SHA256 How to get digest value in `unsigned char` format?
#4
the u32 and u8 are primitive types, you can simply cast an array of 4-byte integers to an array with 1-byte chars/bytes.

Code:
u8 * bytes = (u8 *) digest32;

or copy them directly

Code:
u8 bytes[16];

bytes[0] = (digest32[0] >>  0) & 0xff;
bytes[1] = (digest32[0] >>  8) & 0xff;
bytes[2] = (digest32[0] >> 16) & 0xff;
bytes[3] = (digest32[0] >> 24) & 0xff;

bytes[4] = (digest32[1] >>  0) & 0xff;
bytes[5] = (digest32[1] >>  8) & 0xff;
bytes[6] = (digest32[1] >> 16) & 0xff;
bytes[7] = (digest32[1] >> 24) & 0xff;

....

btw: if you need the "reverse order", you just use >> 24, >> 16, >> 8 and finally >> 0 instead of the reverse order.

While it is often easier to think in bytes (or unsigned chars), the compilers/runtimes normally prefer to use the full 4-byte integers... therefore hashcat actually uses u32 wherever possible. There are of course exceptions, because compilers (unless you use volatile) are most of the time clever enough to just optimize the code from the single chars to the more easier to use u32/integer type (it's more native/easier for most OpenCL device architectures to just use u32).

btw: the output of sha256 is 32 bytes, not 16 bytes: I see that you are using 16 bytes output, but sha256 outputs 32 raw bytes. This might be fine, depending on what you need to do with the output. I just want to mention it here, because it's very confusing to use 16 bytes with sha256.
Reply


Messages In This Thread
RE: SHA256 How to get digest value in `unsigned char` format? - by philsmd - 03-30-2019, 11:50 AM