0

I'd like to hash a file in the same way that git hash-object does, so I can compare it to an existing hash, but using Qt and C++.

The answers to this question show how to get the same hash, but none of the examples use C++.

So far this is what we've tried:

QString fileName = entry.toObject().value( "name" ).toString();
QByteArray shaJson = entry.toObject().value( "sha" ).toString().toUtf8();
QByteArray shaFile;

QFile f( QString( "%1/%2" ).arg( QCoreApplication::applicationDirPath() ).arg( fileName ) );
if( f.open(QFile::ReadOnly ) )
{
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData( QString( "blob " ).toUtf8() ); // start with the string "blob "
hash.addData( QString( "%1" ).arg( f.size() ).toUtf8() ); // add size in bytes of the content
hash.addData( QString( "\0" ).toUtf8() ); // null byte
hash.addData( f.readAll() ); // actual file content
shaFile = hash.result().toHex();
if( shaFile != shaJson ){

}
}

How to implement this hashing method with Qt?

Edit:

Here's an example hash output: ccbf4f0a52fd5ac59e18448ebadf2ef37c62f54f Computed with git hash-object from this file: https://raw.githubusercontent.com/ilia3101/MLV-App/master/pixel_maps/80000301_1808x1007.fpm So that's the hash we also like to compute with Qt.

3
  • Does this answer your question? stackoverflow.com/a/28784281/11613622 or How to get the SHA-1/MD5 checksum of a file with Qt?
    – brc-dd
    Commented Jul 12, 2020 at 13:51
  • Can you post examples of files that you tried? The empty file and foobar example from the linked question would be good options. Also, does QString::toUtf8 include a terminating null character? If so, it will change the hash results, as the input will be different.
    – T0xicCode
    Commented Jul 12, 2020 at 13:53
  • Thanks! Computing a hash with QCryptographicHash works. So it's not a question of how to do sha1 hash. The hard part is combining all the parts in the same way git does when hashing... I've added an example file and the desired hash for it.
    – Jip
    Commented Jul 12, 2020 at 14:22

1 Answer 1

2

The problem is that on the one hand, QString ignores \0 as termination string, on the other hand, QByteArray always appends extra \0. From Qt's docs:

Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a \0 terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

https://doc.qt.io/qt-5/qbytearray.html

So, every addData in your case is adding extra \0 to the data that is to be hashed. Some workaround might be the following code:

QFile file(path);
if( file.open(QFile::ReadOnly ) )
{
    QCryptographicHash hash(QCryptographicHash::Sha1);
    QByteArray header = QString("blob %1").arg(file.size()).toUtf8();
    hash.addData(header.data(), header.size() + 1);
    hash.addData(file.readAll());

    shaFile = hash.result().toHex();
    qDebug() << shaFile;
}

The data() of QByteArray is returning a pointer to the data stored in the byte array. The pointer can be used to access and modify the bytes that compose the array. The data is '\0'-terminated, i.e. the number of bytes in the returned character string is size() + 1 for the '\0' terminator. Therefore, we do not need add explicitly \0, QByteArray is doing that for us. We need to add +1 to the size since QByteArray returns size of an array as it would be no \0 character.

The code above generated ccbf4f0a52fd5ac59e18448ebadf2ef37c62f54f for your file, so I guess it is a correct hash.

1
  • Aah great, that works! Juggling bytes isn't my specialty.
    – Jip
    Commented Jul 12, 2020 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.