diff --git a/include/ircd/m/event.h b/include/ircd/m/event.h index a1399bf73..34587f1ff 100644 --- a/include/ircd/m/event.h +++ b/include/ircd/m/event.h @@ -26,6 +26,9 @@ namespace ircd::m std::string pretty(const event &); std::string pretty_oneline(const event &, const bool &content_keys = true); + bool verify_sha256b64(const event &, const string_view &); + bool verify_hash(const event &, const sha256::buf &); + bool verify_hash(const event &); id::event event_id(const event &, id::event::buf &buf, const const_buffer &hash); id::event event_id(const event &, id::event::buf &buf); diff --git a/ircd/m/event.cc b/ircd/m/event.cc index bf024c09d..3d7e50a21 100644 --- a/ircd/m/event.cc +++ b/ircd/m/event.cc @@ -36,6 +36,52 @@ ircd::m::event_id(const event &event) return at<"event_id"_>(event); } +bool +ircd::m::verify_hash(const event &event) +{ + const sha256::buf hash + { + event.hash(event) + }; + + return verify_hash(event, hash); +} + +bool +ircd::m::verify_hash(const event &event, + const sha256::buf &hash) +{ + static const size_t hashb64sz + { + size_t(hash.size() * 1.34) + 1 + }; + + thread_local char b64buf[hashb64sz]; + return verify_sha256b64(event, b64encode_unpadded(b64buf, hash)); +} + +bool +ircd::m::verify_sha256b64(const event &event, + const string_view &b64) +try +{ + const json::object &object + { + at<"hashes"_>(event) + }; + + const string_view &hash + { + unquote(object.at("sha256")) + }; + + return hash == b64; +} +catch(const json::not_found &) +{ + return false; +} + void ircd::m::check_size(const event &event) {