Calculate and print SHA256 hash of a file using OpenSSL

℡╲_俬逩灬. 提交于 2019-11-29 09:56:04

Looks like there are a lot of '0xff' blocks in your output, and the corresponding blocks in the good string have the high bit set ... maybe a sign extension problem somewhere.

Does making:

char hash[SHA256_DIGEST_LENGTH];

unsigned, like:

unsigned char hash[SHA256_DIGEST_LENGTH];

help? (Especially in the signature of sha256_hash_string.)

You're printing out a signed char as an integer. If the byte is negative, it get's converted to a signed int (the default argument promotions in the call to sprintf), and then that gets converted to an unsigned int (via the %x format specifier) and printed out.

So, the byte A0 is -96 as a signed byte, which gets converted to -96 as a signed int, which is 0xFFFFFFA0 in hex, so it gets printed out as FFFFFFA0.

To fix this, case each byte to an unsigned char before printing:

sprintf(..., (unsigned char)hash[i]);

You're getting the warning about stack smashing because there's a signed byte near the end of the hash, so you're writing the 8 bytes FFFFFFB7 at offset 58 when you intended to only write 2 bytes. This results in a buffer overflow, which happens to be detected here because the compiler likely inserted a guard area or security cookie in the stack before the return value, and it detected that that guard area was inadvertently modified.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!