changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / obj/src/hash.rs

changeset 11: d8f806f1d327
author: ellis <ellis@rwest.io>
date: Sun, 14 May 2023 21:27:04 -0400
permissions: -rw-r--r--
description: obj updates
1 //! hash - wrapper for hash algorithms and types
2 
3 pub use blake3::{derive_key, hash, keyed_hash, Hash as B3Hash, Hasher as B3Hasher, OutputReader};
4 pub use hex;
5 pub use sha2::Sha512;
6 
7 pub use std::hash::{Hash, Hasher};
8 
9 pub const KEY_LEN: usize = 32;
10 pub const OUT_LEN: usize = 32;
11 pub const OUT_LEN_HEX: usize = OUT_LEN * 2;
12 
13 #[cfg(test)]
14 mod tests {
15  use crate::*;
16  use super::*;
17  #[test]
18  fn id_state_hash() {
19  let id = id::Id(vec![0; KEY_LEN]);
20  let hash = id.state_hash(&mut B3Hasher::new());
21  assert_eq!(hash, id.state_hash(&mut B3Hasher::new()));
22  }
23 
24  #[test]
25  fn id_hex() {
26  let id = id::Id(vec![255; KEY_LEN]);
27 
28  assert_eq!(
29  hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap(),
30  id.0
31  );
32  }
33 
34  #[test]
35  fn rand_id() {
36  let id = id::Id::rand();
37  let hash = id.state_hash(&mut B3Hasher::new());
38  assert_eq!(hash, id.state_hash(&mut B3Hasher::new()));
39  }
40 
41  #[test]
42  fn random_demon_id_is_valid() {
43  use id::PeerId;
44  for _ in 0..5000 {
45  let did = PeerId::rand();
46  let did2 = PeerId::rand();
47  assert_eq!(did, did);
48  assert_ne!(did, did2);
49  }
50  }
51 }