SHA-1 implementation
-
@jsulm because I want to learn how the alg. works, therefore I'm writing the alg. myself. I have tested the code but it gives me the wrong hash for the message, and I don't know where the problem is. When I enter the first if ( if (i>=0 && i<=20)) it gives me the rights result, but after the result is incorrect I checked everything and can't find the error.
@mandruk1331 Your question isn't related to Qt, but maybe we have cryptographic experts here.
-
Hi, code looks ok, except for the 64-bit uints used in the calculations, according to Wikipedia:
...
Note 1: All variables are unsigned 32-bit quantities ...
...Try changing the var declarations, like this:
void main(){ uint32_t h0 = 0x67452301; uint32_t h1 = 0xEFCDAB89; uint32_t h2 = 0x98BADCFE; uint32_t h3 = 0x10325476; uint32_t h4 = 0xC3D2E1F0; uint32_t A = h0; uint32_t B = h1; uint32_t C = h2; uint32_t D = h3; uint32_t E = h4;
and
... // till this moment i checked everything works ok uint32_t f = 0; uint32_t k = 0; uint32_t temp__ = 0; ...
-
@jsulm because I want to learn how the alg. works, therefore I'm writing the alg. myself. I have tested the code but it gives me the wrong hash for the message, and I don't know where the problem is. When I enter the first if ( if (i>=0 && i<=20)) it gives me the rights result, but after the result is incorrect I checked everything and can't find the error.
@mandruk1331 I just tested your code (with the 64-bit to 32 bit changes I wrote about above) and it works fine on the Ubuntu GCC and MacOS Clang compilers, but fails in Visual Studio, I'm guessing because of the rotl rand rotr function still being 64-bit flavored.
-
@mandruk1331 I just tested your code (with the 64-bit to 32 bit changes I wrote about above) and it works fine on the Ubuntu GCC and MacOS Clang compilers, but fails in Visual Studio, I'm guessing because of the rotl rand rotr function still being 64-bit flavored.
@hskoglund thanks for the tips. I will change the code.
-
@hskoglund thanks for the tips. I will change the code.
@mandruk1331 Forgot to mention, correct hash_result is 8f0c0855915633e4a7de19468b3874c8901df043
(so you know when it's working :-) -
@mandruk1331 Forgot to mention, correct hash_result is 8f0c0855915633e4a7de19468b3874c8901df043
(so you know when it's working :-)@hskoglund I have checked the result using the online SHA-1. But thanks :-). I changed the types but it didn't do the trick, I also changed the strtol to stoi, and now the program crashes, in this case it's a good thing, because I now have a place to investigate.
-
@hskoglund I have checked the result using the online SHA-1. But thanks :-). I changed the types but it didn't do the trick, I also changed the strtol to stoi, and now the program crashes, in this case it's a good thing, because I now have a place to investigate.
@mandruk1331 If you're using Visual Studio that could explain why you get the wrong hash_result. Try on a Mac or on Ubuntu as I did, and you'll get the correct result.
-
@mandruk1331 If you're using Visual Studio that could explain why you get the wrong hash_result. Try on a Mac or on Ubuntu as I did, and you'll get the correct result.
@hskoglund I will try that, but It would be also great if the program gave the correct result in VS
-
@hskoglund I will try that, but It would be also great if the program gave the correct result in VS
@mandruk1331 Couldn't resist testing in Visual Studio and I put in trace output of temp__ after the
temp__ = rotl(A, 5)+ f + E + k + strtol(Chunks.at(i)->Chunk_words.at(j)->c_str(), &pEnd, 2);
line,
and it agrees with Ubuntu on the first 16 iterations (of the 80) then it goes south in Visual Studio. Interesting... -
@mandruk1331 Couldn't resist testing in Visual Studio and I put in trace output of temp__ after the
temp__ = rotl(A, 5)+ f + E + k + strtol(Chunks.at(i)->Chunk_words.at(j)->c_str(), &pEnd, 2);
line,
and it agrees with Ubuntu on the first 16 iterations (of the 80) then it goes south in Visual Studio. Interesting...@hskoglund interesting... I checked the whole table of chunk_words and the numbers are correct. I followed the instruction on wikipedia and also found this site which describes how SHA-1 works in a good way: http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software
-
@hskoglund interesting... I checked the whole table of chunk_words and the numbers are correct. I followed the instruction on wikipedia and also found this site which describes how SHA-1 works in a good way: http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software
@mandruk1331 Found it! It's the strtol() function that behaves differently, in clang and g++ it returns more than 32 bits, but not in Visual Studio. The solution is to use strtoll()(it also works in Ubuntu).
So, change your call from strtol() to strtoll() and you should be good to go, like this:
temp__ = rotl(A, 5)+ f + E + k + strtoll(Chunks.at(i)->Chunk_words.at(j)->c_str(), &pEnd, 2);
-
@mandruk1331 Found it! It's the strtol() function that behaves differently, in clang and g++ it returns more than 32 bits, but not in Visual Studio. The solution is to use strtoll()(it also works in Ubuntu).
So, change your call from strtol() to strtoll() and you should be good to go, like this:
temp__ = rotl(A, 5)+ f + E + k + strtoll(Chunks.at(i)->Chunk_words.at(j)->c_str(), &pEnd, 2);
@hskoglund Wow! It works I am so thankful. I'd be looking for this issue probably a week.