utility to compute hash sums for files

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-03-11 22:05:44 +02:00
parent 4e5220dede
commit 255508bf4b
4 changed files with 92 additions and 0 deletions

View file

@ -4,3 +4,4 @@ add_subdirectory(normalize)
add_subdirectory(plugintest)
add_subdirectory(qlalr)
add_subdirectory(imgconv)
add_subdirectory(filehash)

View file

@ -0,0 +1,19 @@
# add_definitions()
set(EXTRA_FILEHASH_LIBS KtCore KtNetwork)
include_directories(
${CMAKE_BINARY_DIR}/include
${CMAKE_BINARY_DIR}/privateinclude
${CMAKE_BINARY_DIR}/include/QtCore
${CMAKE_BINARY_DIR}/include/QtNetwork
${CMAKE_CURRENT_SOURCE_DIR}
)
set(FILEHASH_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
katie_setup_target(filehash ${FILEHASH_SOURCES})
add_executable(filehash ${filehash_SOURCES})
target_link_libraries(filehash ${EXTRA_FILEHASH_LIBS})

1
util/filehash/README Normal file
View file

@ -0,0 +1 @@
This utility will print hash sum of file in chosen algorithm.

71
util/filehash/main.cpp Normal file
View file

@ -0,0 +1,71 @@
/****************************************************************************
**
** Copyright (C) 2022 Ivailo Monev
**
** This file is part of the utils of the Katie Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtCore/qcoreapplication.h>
#include <QtCore/qdebug.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qfile.h>
#include <QtNetwork/qcryptographichash.h>
QT_USE_NAMESPACE
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
const QStringList args = app.arguments().mid(1);
if (args.size() != 2) {
qWarning() << "Usage: imgconv <md5|sha1|sha256|sha512|blake3> <filepath>";
return 1;
}
QCryptographicHash::Algorithm algorithm = QCryptographicHash::Md5;
if (args.at(0) == "md5") {
algorithm = QCryptographicHash::Md5;
} else if (args.at(0) == "sha1") {
algorithm = QCryptographicHash::Sha1;
} else if (args.at(0) == "sha256") {
algorithm = QCryptographicHash::Sha256;
} else if (args.at(0) == "sha512") {
algorithm = QCryptographicHash::Sha512;
} else if (args.at(0) == "blake3") {
algorithm = QCryptographicHash::BLAKE3;
} else {
qWarning() << "Invalid algorithm" << args.at(0);
return 2;
}
QFile file(args.at(1));
if (file.open(QFile::ReadOnly) == false) {
qWarning() << "Could not open" << args.at(1);
return 3;
}
const QByteArray hash = QCryptographicHash::hash(file.readAll(), algorithm).toHex();
if (hash.isEmpty()) {
qWarning() << "Could not hash" << args.at(1);
return 4;
}
qDebug() << hash;
return 0;
}