Reputation: 1779
int main()
{
SHA256_CTX context;
unsigned char md[SHA256_DIGEST_LENGTH];
char *input = NULL;
printf("What to hash: ");
scanf("%m[^\n]%*c", &input);
size_t length = strlen((const char*)input);
int i, n=0;
SHA256_Init(&context);
SHA256_Update(&context, (unsigned char*)input, length);
SHA256_Final(md, &context);
goto NORMAL;
MORE:
n++;
SHA256_Update(&context, md, sizeof(md));
SHA256_Final(md, &context);
NORMAL:
for(i=0; i<SHA256_DIGEST_LENGTH; i++){
printf("%02x", md[i]);
}
printf("\n");
if(n==5){free(input);exit(0);}
goto MORE;
return 0;
}
Why this spaghetti code doesn't correctly generate multiple hash (sha256)?
My output:
What to hash: paolo
254835d73cc88095a30fc74133beabe9d8463b2954493227b205ea326c8a9c86
f8c9fd41c9aea21015b375af8e30c55df62a74e750f6381c704579b326bff2cd
dbbae564faab81da846c904659e993542fbb20643f7e2f57cc62934dade3ed87
a07424dbf030c30b76eb6db0f32a4ed4af084b4d1523cc2cacefbd19a3d0525d
33041103d21220c8906a3cc0b6369ab38428a440aceb1c5cac963ec471ec1aac
d529745ec47c77aea87d6a5f63ea509223cbd756d77326f232b0cd1d14681e24
expected output (thanks to an online sha256 calculator):
string: paolo
254835d73cc88095a30fc74133beabe9d8463b2954493227b205ea326c8a9c86
afbdc26d1e74bf1e9f68f154883e0bf5a1b9878c5c631e78126b07fa21b79ecd
739efedfc55930ef8cb0d9df487a50ab5b06b7f66594e82e437723df1e6184a1
008f3e3942bfb4289ad8fbbd081e981c4e32ec6508d43d8c3ae10d1b42e792d0
5ec489fc8b5345f43d821b8bac6851d61dc5f4f1fb825a5b74cc18e7bbc3e1b3
Upvotes: 1
Views: 1482
Reputation:
The problem is that you're re-hashing the binary (byte) representation of the resulting hash, and not its hexadecimal representation. You have to convert it to hexadecimal (probably using sprintf()
or alike) after every iteration.
As David Schwartz correctly pointed it out, you also have to re-Init()
the hash context in order to clear it and not leave other erroneous data in it.
Upvotes: 3
Reputation: 182761
When you start the second and subsequent hashes, you leave the SHA256_CTX
full of leftover junk from previous loops. You need to call SHA256_Init
again to reset it.
Upvotes: 3