Use ring crate to generate StatHashes when saving stateid/statehash

This commit is contained in:
Devin Ragotzy 2020-08-18 14:32:38 -04:00
parent c4f5a0a631
commit cb68bf9e0c
4 changed files with 15 additions and 10 deletions

1
Cargo.lock generated
View file

@ -295,6 +295,7 @@ dependencies = [
"log",
"rand",
"reqwest",
"ring",
"rocket",
"ruma",
"rust-argon2 0.8.2",

View file

@ -33,7 +33,7 @@ image = { version = "0.23.4", default-features = false, features = ["jpeg", "png
base64 = "0.12.3" # Used to encode server public key
# state-res = { path = "../../state-res" }
state-res = { git = "https://github.com/ruma/state-res", version = "0.1.0" }
ring = "0.16.15"
[features]
default = ["conduit_bin"]

View file

@ -4,6 +4,7 @@ pub use edus::RoomEdus;
use crate::{pdu::PduBuilder, utils, Error, PduEvent, Result};
use log::error;
use ring::digest;
use ruma::{
api::client::error::ErrorKind,
events::{
@ -21,9 +22,8 @@ use sled::IVec;
use state_res::{event_auth, Requester, StateEvent, StateMap, StateStore};
use std::{
collections::{hash_map::DefaultHasher, BTreeMap, HashMap},
collections::{BTreeMap, HashMap},
convert::{TryFrom, TryInto},
hash::{Hash, Hasher},
mem,
result::Result as StdResult,
};
@ -285,8 +285,10 @@ impl Rooms {
.next()
.is_none()
{
// TODO use ring crate to hash
return Ok(room_id.as_str().to_owned());
return utils::string_from_bytes(
digest::digest(&digest::SHA256, room_id.as_bytes()).as_ref(),
)
.map_err(|_| Error::bad_database("Empty state generated invalid string from hash."));
}
let pdu_ids_to_hash = self
@ -304,11 +306,13 @@ impl Rooms {
.collect::<Result<Vec<Vec<u8>>>>()
})??;
let mut hasher = DefaultHasher::new();
pdu_ids_to_hash.hash(&mut hasher);
let hash = hasher.finish().to_string();
let hash = digest::digest(
&digest::SHA256,
&pdu_ids_to_hash.into_iter().flatten().collect::<Vec<u8>>(),
);
// TODO not sure how you want to hash this
Ok(hash)
utils::string_from_bytes(hash.as_ref())
.map_err(|_| Error::bad_database("State generated invalid string from hash."))
}
/// Checks if a room exists.

View file

@ -94,5 +94,5 @@ pub fn common_elements(
pub fn deserialize<'de, T: serde::Deserialize<'de>>(val: &'de sled::IVec) -> Result<T, Error> {
serde_json::from_slice::<T>(val.as_ref())
.map_err(|_| Error::bad_database("PDU in db is invalid."))
.map_err(|_| Error::bad_database("Found invalid bytes as PDU in db."))
}