0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-29 20:28:52 +02:00

ircd:Ⓜ️ Event hash checking stack.

This commit is contained in:
Jason Volk 2018-03-22 23:50:41 -07:00
parent 5d41b4e00b
commit 50b21edb1c
2 changed files with 49 additions and 0 deletions

View file

@ -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);

View file

@ -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)
{