Unsolved Am I misunderstanding or misusing QCryptographicHash::hash()?
-
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?
-
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 -
Looks like a bug to me.
-
@In-Fo Why? Please provide some code - we don't see what you're really doing.
-
Never mind. I am forgetting that QByteArray has no way of knowing how many bytes are in the array.
-
@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.
?
-
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.
-
@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.)
-
@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);
-
@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?