2020-09-12 21:37:12 +03:00
|
|
|
/*
|
2021-04-22 23:51:19 +03:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-09-12 21:37:12 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-12 21:37:12 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
2021-11-27 17:10:31 +03:00
|
|
|
#include <LibCore/System.h>
|
2020-09-12 21:37:12 +03:00
|
|
|
#include <LibCrypto/Hash/HashManager.h>
|
2021-11-27 17:10:31 +03:00
|
|
|
#include <LibMain/Main.h>
|
2020-09-12 21:37:12 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-11-27 17:10:31 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-09-12 21:37:12 +03:00
|
|
|
{
|
2021-11-27 17:10:31 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath", nullptr));
|
2020-09-12 21:37:12 +03:00
|
|
|
|
2021-11-27 17:10:31 +03:00
|
|
|
auto program_name = arguments.strings[0];
|
2021-03-25 02:19:37 +03:00
|
|
|
auto hash_kind = Crypto::Hash::HashKind::None;
|
2020-09-12 21:37:12 +03:00
|
|
|
|
|
|
|
if (program_name == "md5sum")
|
|
|
|
hash_kind = Crypto::Hash::HashKind::MD5;
|
|
|
|
else if (program_name == "sha1sum")
|
|
|
|
hash_kind = Crypto::Hash::HashKind::SHA1;
|
|
|
|
else if (program_name == "sha256sum")
|
|
|
|
hash_kind = Crypto::Hash::HashKind::SHA256;
|
|
|
|
else if (program_name == "sha512sum")
|
|
|
|
hash_kind = Crypto::Hash::HashKind::SHA512;
|
|
|
|
|
|
|
|
if (hash_kind == Crypto::Hash::HashKind::None) {
|
2021-11-27 17:10:31 +03:00
|
|
|
warnln("Error: program must be executed as 'md5sum', 'sha1sum', 'sha256sum' or 'sha512sum'; got '{}'", program_name);
|
2020-09-12 21:37:12 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hash_name = program_name.substring_view(0, program_name.length() - 3).to_string().to_uppercase();
|
2021-01-16 14:00:33 +03:00
|
|
|
auto paths_help_string = String::formatted("File(s) to print {} checksum of", hash_name);
|
2020-09-12 21:37:12 +03:00
|
|
|
|
2021-11-27 00:32:37 +03:00
|
|
|
Vector<StringView> paths;
|
2020-09-12 21:37:12 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(paths, paths_help_string.characters(), "path", Core::ArgsParser::Required::No);
|
2021-11-27 17:10:31 +03:00
|
|
|
args_parser.parse(arguments);
|
2020-09-12 21:37:12 +03:00
|
|
|
|
|
|
|
if (paths.is_empty())
|
|
|
|
paths.append("-");
|
|
|
|
|
|
|
|
Crypto::Hash::Manager hash;
|
|
|
|
hash.initialize(hash_kind);
|
|
|
|
|
|
|
|
auto has_error = false;
|
|
|
|
|
2021-04-29 12:28:01 +03:00
|
|
|
for (auto const& path : paths) {
|
2021-09-21 19:18:19 +03:00
|
|
|
NonnullRefPtr<Core::File> file = Core::File::standard_input();
|
|
|
|
if (path != "-"sv) {
|
|
|
|
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
|
|
|
|
if (file_or_error.is_error()) {
|
2021-11-27 17:10:31 +03:00
|
|
|
warnln("{}: {}: {}", program_name, path, file->error_string());
|
2021-09-21 19:18:19 +03:00
|
|
|
has_error = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file = file_or_error.release_value();
|
2020-09-12 21:37:12 +03:00
|
|
|
}
|
2021-03-19 00:58:39 +03:00
|
|
|
|
|
|
|
while (!file->eof() && !file->has_error())
|
|
|
|
hash.update(file->read(PAGE_SIZE));
|
2020-09-12 21:37:12 +03:00
|
|
|
auto digest = hash.digest();
|
|
|
|
auto digest_data = digest.immutable_data();
|
|
|
|
StringBuilder builder;
|
|
|
|
for (size_t i = 0; i < hash.digest_size(); ++i)
|
2021-03-25 02:19:37 +03:00
|
|
|
builder.appendff("{:02x}", digest_data[i]);
|
2020-09-12 21:37:12 +03:00
|
|
|
auto hash_sum_hex = builder.build();
|
2021-03-25 02:19:37 +03:00
|
|
|
outln("{} {}", hash_sum_hex, path);
|
2020-09-12 21:37:12 +03:00
|
|
|
}
|
|
|
|
return has_error ? 1 : 0;
|
|
|
|
}
|