Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SHA-1 implementation

SHA-1 implementation

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mandruk1331M mandruk1331

    @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.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #7

    @mandruk1331 Your question isn't related to Qt, but maybe we have cryptographic experts here.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by
      #8

      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;
      ...
      
      1 Reply Last reply
      2
      • mandruk1331M mandruk1331

        @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.

        hskoglundH Online
        hskoglundH Online
        hskoglund
        wrote on last edited by
        #9

        @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.

        mandruk1331M 1 Reply Last reply
        3
        • hskoglundH hskoglund

          @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.

          mandruk1331M Offline
          mandruk1331M Offline
          mandruk1331
          wrote on last edited by
          #10

          @hskoglund thanks for the tips. I will change the code.

          Mandruk1331

          hskoglundH 1 Reply Last reply
          0
          • mandruk1331M mandruk1331

            @hskoglund thanks for the tips. I will change the code.

            hskoglundH Online
            hskoglundH Online
            hskoglund
            wrote on last edited by
            #11

            @mandruk1331 Forgot to mention, correct hash_result is 8f0c0855915633e4a7de19468b3874c8901df043
            (so you know when it's working :-)

            mandruk1331M 1 Reply Last reply
            1
            • hskoglundH hskoglund

              @mandruk1331 Forgot to mention, correct hash_result is 8f0c0855915633e4a7de19468b3874c8901df043
              (so you know when it's working :-)

              mandruk1331M Offline
              mandruk1331M Offline
              mandruk1331
              wrote on last edited by mandruk1331
              #12

              @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

              hskoglundH 1 Reply Last reply
              0
              • mandruk1331M mandruk1331

                @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.

                hskoglundH Online
                hskoglundH Online
                hskoglund
                wrote on last edited by
                #13

                @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.

                mandruk1331M 1 Reply Last reply
                1
                • hskoglundH hskoglund

                  @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.

                  mandruk1331M Offline
                  mandruk1331M Offline
                  mandruk1331
                  wrote on last edited by mandruk1331
                  #14

                  @hskoglund I will try that, but It would be also great if the program gave the correct result in VS

                  Mandruk1331

                  hskoglundH 1 Reply Last reply
                  0
                  • mandruk1331M mandruk1331

                    @hskoglund I will try that, but It would be also great if the program gave the correct result in VS

                    hskoglundH Online
                    hskoglundH Online
                    hskoglund
                    wrote on last edited by
                    #15

                    @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...

                    mandruk1331M 1 Reply Last reply
                    1
                    • hskoglundH hskoglund

                      @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...

                      mandruk1331M Offline
                      mandruk1331M Offline
                      mandruk1331
                      wrote on last edited by mandruk1331
                      #16

                      @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

                      hskoglundH 1 Reply Last reply
                      0
                      • mandruk1331M mandruk1331

                        @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

                        hskoglundH Online
                        hskoglundH Online
                        hskoglund
                        wrote on last edited by
                        #17

                        @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);
                        
                        mandruk1331M 1 Reply Last reply
                        2
                        • hskoglundH hskoglund

                          @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);
                          
                          mandruk1331M Offline
                          mandruk1331M Offline
                          mandruk1331
                          wrote on last edited by
                          #18

                          @hskoglund Wow! It works I am so thankful. I'd be looking for this issue probably a week.

                          Mandruk1331

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved