Use ring crate to generate StatHashes when saving stateid/statehash

next
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", "log",
"rand", "rand",
"reqwest", "reqwest",
"ring",
"rocket", "rocket",
"ruma", "ruma",
"rust-argon2 0.8.2", "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 base64 = "0.12.3" # Used to encode server public key
# state-res = { path = "../../state-res" } # state-res = { path = "../../state-res" }
state-res = { git = "https://github.com/ruma/state-res", version = "0.1.0" } state-res = { git = "https://github.com/ruma/state-res", version = "0.1.0" }
ring = "0.16.15"
[features] [features]
default = ["conduit_bin"] default = ["conduit_bin"]

View File

@ -4,6 +4,7 @@ pub use edus::RoomEdus;
use crate::{pdu::PduBuilder, utils, Error, PduEvent, Result}; use crate::{pdu::PduBuilder, utils, Error, PduEvent, Result};
use log::error; use log::error;
use ring::digest;
use ruma::{ use ruma::{
api::client::error::ErrorKind, api::client::error::ErrorKind,
events::{ events::{
@ -21,9 +22,8 @@ use sled::IVec;
use state_res::{event_auth, Requester, StateEvent, StateMap, StateStore}; use state_res::{event_auth, Requester, StateEvent, StateMap, StateStore};
use std::{ use std::{
collections::{hash_map::DefaultHasher, BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
hash::{Hash, Hasher},
mem, mem,
result::Result as StdResult, result::Result as StdResult,
}; };
@ -285,8 +285,10 @@ impl Rooms {
.next() .next()
.is_none() .is_none()
{ {
// TODO use ring crate to hash return utils::string_from_bytes(
return Ok(room_id.as_str().to_owned()); 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 let pdu_ids_to_hash = self
@ -304,11 +306,13 @@ impl Rooms {
.collect::<Result<Vec<Vec<u8>>>>() .collect::<Result<Vec<Vec<u8>>>>()
})??; })??;
let mut hasher = DefaultHasher::new(); let hash = digest::digest(
pdu_ids_to_hash.hash(&mut hasher); &digest::SHA256,
let hash = hasher.finish().to_string(); &pdu_ids_to_hash.into_iter().flatten().collect::<Vec<u8>>(),
);
// TODO not sure how you want to hash this // 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. /// 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> { pub fn deserialize<'de, T: serde::Deserialize<'de>>(val: &'de sled::IVec) -> Result<T, Error> {
serde_json::from_slice::<T>(val.as_ref()) 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."))
} }