使用Qt C++计算类似Git的SHA1哈希
Calculate SHA1 hash like Git with Qt C++
我想以与git hash-object
相同的方式对文件进行哈希,这样我就可以将其与现有的哈希进行比较,但要使用Qt和C++。
这个问题的答案显示了如何获得相同的散列,但没有一个例子使用C++。
到目前为止,这就是我们所尝试的:
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( "