diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index f9959839b..060658659 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -4,3 +4,4 @@ add_subdirectory(normalize) add_subdirectory(plugintest) add_subdirectory(qlalr) add_subdirectory(imgconv) +add_subdirectory(filehash) diff --git a/util/filehash/CMakeLists.txt b/util/filehash/CMakeLists.txt new file mode 100644 index 000000000..d2fb91854 --- /dev/null +++ b/util/filehash/CMakeLists.txt @@ -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}) diff --git a/util/filehash/README b/util/filehash/README new file mode 100644 index 000000000..e55da27a1 --- /dev/null +++ b/util/filehash/README @@ -0,0 +1 @@ +This utility will print hash sum of file in chosen algorithm. diff --git a/util/filehash/main.cpp b/util/filehash/main.cpp new file mode 100644 index 000000000..22681122b --- /dev/null +++ b/util/filehash/main.cpp @@ -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 +#include +#include +#include +#include + +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 "; + 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; +} +