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. Am I misunderstanding or misusing QCryptographicHash::hash()?
Forum Updated to NodeBB v4.3 + New Features

Am I misunderstanding or misusing QCryptographicHash::hash()?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.3k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    In Fo
    wrote on last edited by In Fo
    #1

    I have an array with original data

    char data[32];
    

    in which data gradually increments. I also have

    QByteArray hash = Q_NULLPTR;
    

    Every time I call

    hash = QCryptographicHash::hash(data, QCryptographicHash::Sha256);
    

    it returns the same hash, dumped as follows:

    0000000000000000000000000000000000000000000000000000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000010000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000020000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000030000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000040000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000050000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000060000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000070000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000080000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    0000000000000000000000000000000000000000000000090000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    00000000000000000000000000000000000000000000000a0000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    

    Why does not the hash reflect changes to the data?

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Maybe its the same where data converted to a QByteArray is considered empty if if starts with null.
      You call hash(const QByteArray &data, QCryptographicHash::Algorithm method)
      and data is converted to a QByteArray so I think its the same as here
      https://forum.qt.io/topic/120008/am-i-misunderstanding-or-misusing-qbytearray-tohex

      1 Reply Last reply
      0
      • I Offline
        I Offline
        In Fo
        wrote on last edited by
        #3

        Looks like a bug to me.

        Christian EhrlicherC 1 Reply Last reply
        0
        • I In Fo

          Looks like a bug to me.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @In-Fo Why? Please provide some code - we don't see what you're really doing.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • I Offline
            I Offline
            In Fo
            wrote on last edited by
            #5

            Never mind. I am forgetting that QByteArray has no way of knowing how many bytes are in the array.

            Christian EhrlicherC 1 Reply Last reply
            0
            • I In Fo

              Never mind. I am forgetting that QByteArray has no way of knowing how many bytes are in the array.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @In-Fo said in Am I misunderstanding or misusing QCryptographicHash::hash()?:

              I am forgetting that QByteArray has no way of knowing how many bytes are in the array.

              ?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • I Offline
                I Offline
                In Fo
                wrote on last edited by In Fo
                #7
                hash = QCryptographicHash::hash(QByteArray(data, 32), QCryptographicHash::Sha256);
                

                This just means that I should not use Qt for hashing. This is too slow, considering all the required plumbing.

                JonBJ Christian EhrlicherC 2 Replies Last reply
                0
                • I In Fo
                  hash = QCryptographicHash::hash(QByteArray(data, 32), QCryptographicHash::Sha256);
                  

                  This just means that I should not use Qt for hashing. This is too slow, considering all the required plumbing.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @In-Fo
                  Getting lost in what you are saying. QCryptographicHash::hash is no better or worse than similar in other libraries.

                  (At one point you seemed to have an expectation that as you add/update in your original data buffer the hash will notice this and auto-recalculate? This is not going to happen.... Maybe I misunderstood that bit of what you were saying.)

                  1 Reply Last reply
                  2
                  • I In Fo
                    hash = QCryptographicHash::hash(QByteArray(data, 32), QCryptographicHash::Sha256);
                    

                    This just means that I should not use Qt for hashing. This is too slow, considering all the required plumbing.

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #9

                    @In-Fo said in Am I misunderstanding or misusing QCryptographicHash::hash()?:

                    This just means that I should not use Qt for hashing. This is too slow, considering all the required plumbing.

                    To avoid copying use

                    QCryptographicHash::hash(QByteArray::fromRawData(data, 32), QCryptographicHash::Sha256);

                    or simply QCryptographicHash::addData(const char*, int)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    3
                    • I In Fo

                      I have an array with original data

                      char data[32];
                      

                      in which data gradually increments. I also have

                      QByteArray hash = Q_NULLPTR;
                      

                      Every time I call

                      hash = QCryptographicHash::hash(data, QCryptographicHash::Sha256);
                      

                      it returns the same hash, dumped as follows:

                      0000000000000000000000000000000000000000000000000000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000010000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000020000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000030000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000040000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000050000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000060000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000070000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000080000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      0000000000000000000000000000000000000000000000090000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      00000000000000000000000000000000000000000000000a0000000000000000	e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
                      

                      Why does not the hash reflect changes to the data?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @In-Fo
                      It's also nicer when you post two similar questions that you tell us about the other one: https://forum.qt.io/topic/120008/am-i-misunderstanding-or-misusing-qbytearray-tohex. And is the answer you are looking for here affected by the answer you have received there?

                      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