m/market labResearch, with a record.
Source on GitHub ↗

digest.cpp

1#include "itch/common/digest.hpp"2#include <array>3#include <fstream>4#include <iomanip>5#include <sstream>6#include <stdexcept>78namespace itch {9namespace {10// SHA-256, with explicit big-endian words and modulo-2^32 arithmetic.11class Sha256 {12    std::array<uint32_t, 8> h_{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,13                               0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};14    std::array<unsigned char, 64> block_{};15    uint64_t size_ = 0;16    size_t used_ = 0;1718    static uint32_t rotate(uint32_t x, unsigned n) {19        return (x >> n) | (x << (32 - n));20    }2122    void compress() {23        static constexpr uint32_t k[] = {24            0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,25            0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,26            0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,27            0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,28            0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,29            0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,30            0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,31            0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,32            0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,33            0xc67178f2};34        uint32_t w[64];35        for (size_t i = 0; i < 16; ++i) {36            w[i] = 0;37            for (size_t j = 0; j < 4; ++j) {38                w[i] = (w[i] << 8) | block_[4 * i + j];39            }40        }41        for (size_t i = 16; i < 64; ++i) {42            const uint32_t a = w[i - 15], b = w[i - 2];43            w[i] = w[i - 16] + (rotate(a, 7) ^ rotate(a, 18) ^ (a >> 3)) + w[i - 7] +44                   (rotate(b, 17) ^ rotate(b, 19) ^ (b >> 10));45        }46        auto v = h_;47        for (size_t i = 0; i < 64; ++i) {48            const uint32_t t1 = v[7] + (rotate(v[4], 6) ^ rotate(v[4], 11) ^ rotate(v[4], 25)) +49                                ((v[4] & v[5]) ^ (~v[4] & v[6])) + k[i] + w[i];50            const uint32_t t2 = (rotate(v[0], 2) ^ rotate(v[0], 13) ^ rotate(v[0], 22)) +51                                ((v[0] & v[1]) ^ (v[0] & v[2]) ^ (v[1] & v[2]));52            for (size_t j = 7; j > 0; --j) {53                v[j] = v[j - 1];54            }55            v[4] += t1;56            v[0] = t1 + t2;57        }58        for (size_t i = 0; i < 8; ++i) {59            h_[i] += v[i];60        }61    }6263  public:64    void update(const char* data, size_t length) {65        size_ += length;66        for (size_t i = 0; i < length; ++i) {67            block_[used_++] = static_cast<unsigned char>(data[i]);68            if (used_ == 64) {69                compress();70                used_ = 0;71            }72        }73    }7475    std::string finish() {76        const uint64_t bits = size_ * 8;77        const char marker = static_cast<char>(0x80), zero = 0;78        update(&marker, 1);79        while (used_ != 56) {80            update(&zero, 1);81        }82        for (int i = 7; i >= 0; --i) {83            const char b = static_cast<char>(bits >> (i * 8));84            update(&b, 1);85        }86        std::ostringstream out;87        out << std::hex << std::setfill('0');88        for (uint32_t word : h_) {89            out << std::setw(8) << word;90        }91        return out.str();92    }93};94} // namespace9596uint64_t fnv1a_bytes(uint64_t hash, std::string_view bytes) {97    for (unsigned char byte : bytes) {98        hash ^= byte;99        hash *= 1099511628211ULL;100    }101    return hash;102}103104uint64_t hash_csv_line(uint64_t hash, std::string_view line) {105    return fnv1a_bytes(fnv1a_bytes(hash, line), "\n");106}107108std::string sha256(const std::string& bytes) {109    Sha256 hash;110    hash.update(bytes.data(), bytes.size());111    return hash.finish();112}113114Fingerprint fingerprint(const std::string& path) {115    std::ifstream in(path, std::ios::binary);116    if (!in) {117        throw std::runtime_error("cannot open input: " + path);118    }119    Sha256 hash;120    uint64_t size = 0;121    std::array<char, 65536> buffer;122    while (in.read(buffer.data(), buffer.size()) || in.gcount()) {123        const size_t count = static_cast<size_t>(in.gcount());124        hash.update(buffer.data(), count);125        size += count;126    }127    if (!in.eof() || in.bad()) {128        throw std::runtime_error("input digest read failed: " + path);129    }130    return {size, hash.finish()};131}132} // namespace itch