Luna/apps/sha256sum.cpp

31 lines
791 B
C++
Raw Normal View History

#include <luna/SHA.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
Result<int> luna_main(int argc, char** argv)
{
Vector<StringView> files;
os::ArgumentParser parser;
parser.add_description("Calculate SHA256 hashes for a series of files."_sv);
parser.add_system_program_info("sha256sum"_sv);
parser.set_vector_argument(files, "files"_sv, "-"_sv);
parser.parse(argc, argv);
for (auto path : files)
{
auto file = TRY(os::File::open_input_file(path));
auto data = TRY(file->read_all());
SHA256 sha;
TRY(sha.append(data.data(), data.size()));
auto hash = TRY(sha.digest());
auto string = TRY(sha.hash_to_string(hash));
os::println("%s %s", string.chars(), path.chars());
}
return 0;
}