From db0661dbb51e9082e47926c31e93bdc97b491bf9 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 22 Nov 2023 16:47:21 -0800 Subject: Add support for hash chaining to detect modifications in postings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The following details of a posting contribute to its hash: fullname of account string representation of amount Each posting hashes contributes to the transaction hash, which is compromised of: previous transaction’s hash (as encountered in parsing order) actual date optional auxiliary date optional code payee hashes of all postings Note that this means that changes in the “code” or any of the comments --- src/post.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/post.cc') diff --git a/src/post.cc b/src/post.cc index e2063a36..9d799a9e 100644 --- a/src/post.cc +++ b/src/post.cc @@ -671,6 +671,30 @@ void post_t::set_reported_account(account_t * acct) acct->xdata().reported_posts.push_back(this); } +extern "C" unsigned char *SHA512( + void *data, unsigned int data_len, unsigned char *digest); + +namespace { + std::string bufferToHex(const unsigned char* buffer, std::size_t size) { + std::ostringstream oss; + oss << std::hex << std::setfill('0'); + for(std::size_t i = 0; i < size; ++i) + oss << std::setw(2) << static_cast(buffer[i]); + return oss.str(); + } +} + +string post_t::hash(string nonce) const { + unsigned char data[128]; + std::ostringstream repr; + repr << nonce; + repr << account->fullname(); + repr << amount.to_string(); + string repr_str(repr.str()); + SHA512((void *)repr_str.c_str(), repr_str.length(), data); + return bufferToHex(data, 64 /*SHA512_DIGEST_LENGTH*/); +} + void extend_post(post_t& post, journal_t& journal) { commodity_t& comm(post.amount.commodity()); -- cgit v1.2.3