Skip to content

Commit

Permalink
Switch libraries for sha3 256, move implementation to source file
Browse files Browse the repository at this point in the history
The previous library we used (picosha3) returned a wrong hash for one
particular file. Though this was quite rare (we only found 1 such file),
it was a bug with the library regardless.
The current sha3 library now comes from
https://github.com/stbrumme/hash-library
  • Loading branch information
matcool committed Aug 3, 2023
1 parent 2233b94 commit bbf2608
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 762 deletions.
1 change: 1 addition & 0 deletions loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ file(GLOB SOURCES CONFIGURE_DEPENDS
src/ui/internal/info/*.cpp
src/ui/internal/list/*.cpp
src/ui/internal/settings/*.cpp
hash/hash.cpp
)

# Obj-c sources
Expand Down
2 changes: 1 addition & 1 deletion loader/hash/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(GeodeChecksum VERSION 1.0)

add_executable(${PROJECT_NAME} hash.cpp)
add_executable(${PROJECT_NAME} main.cpp hash.cpp)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)

target_link_libraries(${PROJECT_NAME} PUBLIC ghc_filesystem)
Expand Down
47 changes: 40 additions & 7 deletions loader/hash/hash.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
#include <iostream>
#include "hash.hpp"

int main(int argc, char** argv) {
if (argc < 2 || !ghc::filesystem::exists(argv[1])) {
std::cout << "Usage: \"checksum <file>\"\n";
return 1;
// shh, its fine :-)
#include "sha3.cpp"

#include <string>
#include <fstream>
#include <ciso646>
#include "picosha2.h"
#include <vector>

template <class Func>
void readBuffered(std::ifstream& stream, Func func) {
constexpr size_t BUF_SIZE = 4096;
stream.exceptions(std::ios_base::badbit);

std::vector<uint8_t> buffer(BUF_SIZE);
while (true) {
stream.read(reinterpret_cast<char*>(buffer.data()), BUF_SIZE);
size_t amt = stream ? BUF_SIZE : stream.gcount();
func(buffer.data(), amt);
if (!stream) break;
}
std::cout << calculateHash(argv[1]) << std::endl;
return 0;
}

std::string calculateSHA3_256(ghc::filesystem::path const& path) {
std::ifstream file(path, std::ios::binary);
SHA3 sha;
readBuffered(file, [&](const void* data, size_t amt) {
sha.add(data, amt);
});
return sha.getHash();
}

std::string calculateSHA256(ghc::filesystem::path const& path) {
std::vector<uint8_t> hash(picosha2::k_digest_size);
std::ifstream file(path, std::ios::binary);
picosha2::hash256(file, hash.begin(), hash.end());
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}

std::string calculateHash(ghc::filesystem::path const& path) {
return calculateSHA3_256(path);
}
23 changes: 3 additions & 20 deletions loader/hash/hash.hpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
#pragma once

#include <string>
#include <fstream>
#include <ciso646>
#include "picosha3.h"
#include "picosha2.h"
#include <vector>
#include <ghc/filesystem.hpp>

static std::string calculateSHA3_256(ghc::filesystem::path const& path) {
std::vector<uint8_t> s(picosha3::bits_to_bytes(256));
auto sha3_256 = picosha3::get_sha3_generator<256>();
std::ifstream file(path, std::ios::binary);
return sha3_256.get_hex_string(file);
}
std::string calculateSHA3_256(ghc::filesystem::path const& path);

static std::string calculateSHA256(ghc::filesystem::path const& path) {
std::vector<uint8_t> hash(picosha2::k_digest_size);
std::ifstream file(path, std::ios::binary);
picosha2::hash256(file, hash.begin(), hash.end());
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}
std::string calculateSHA256(ghc::filesystem::path const& path);

static std::string calculateHash(ghc::filesystem::path const& path) {
return calculateSHA3_256(path);
}
std::string calculateHash(ghc::filesystem::path const& path);
11 changes: 11 additions & 0 deletions loader/hash/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
#include "hash.hpp"

int main(int argc, char** argv) {
if (argc < 2 || !ghc::filesystem::exists(argv[1])) {
std::cout << "Usage: \"checksum <file>\"\n";
return 1;
}
std::cout << calculateHash(argv[1]) << std::endl;
return 0;
}
Loading

0 comments on commit bbf2608

Please sign in to comment.