Mercurial > core / rust/lib/hash/src/lib.rs
changeset 25: |
94253682df1f |
parent: |
1227f932b628
|
child: |
920ded613f45 |
author: |
ellis <ellis@rwest.io> |
date: |
Tue, 31 Oct 2023 00:16:50 -0400 |
permissions: |
-rw-r--r-- |
description: |
krypt, hash test refactor |
1 //! hash - wrapper for hash algorithms and types 3 pub use blake3::{derive_key, hash, keyed_hash, Hash as B3Hash, Hasher as B3Hasher, OutputReader}; 6 use serde::{Deserialize, Serialize}; 8 // hashbrown is now the default for the Rust stdlib. We only need to 9 // re-export in no_std envs. 11 pub use hashbrown::{HashMap, HashSet}; 12 pub use std::hash::{Hash, Hasher}; 14 pub const KEY_LEN: usize = 32; 15 pub const OUT_LEN: usize = 32; 16 pub const OUT_LEN_HEX: usize = OUT_LEN * 2; 21 /// a simple Id abstraction with help functions. I'm finding this easier than 22 /// state machines and traits for the time-being. 23 #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Serialize, Deserialize, Hash)] 24 pub struct Id(pub Vec<u8>); 27 pub fn rand() -> Self { 28 let mut rng = rand::thread_rng(); 29 let vals: Vec<u8> = (0..KEY_LEN).map(|_| rng.gen_range(0..u8::MAX)).collect(); 33 pub fn state_hash(&self, state: &mut B3Hasher) -> Self { 34 let mut output = vec![0; OUT_LEN]; 35 state.update(&self.0); 36 let mut res = state.finalize_xof(); 37 res.fill(&mut output); 41 pub fn to_hex(&self) -> String { 48 /// identifies a unique Peer 49 #[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)] 55 pub fn new() -> Self { 59 pub fn rand() -> Self { 60 let pd = rand::thread_rng().gen::<[u8; 32]>(); 64 pub fn from_bytes(data: &[u8]) -> Self { 65 let pd = blake3::hash(data); 66 let hash = pd.as_bytes(); 71 impl Default for PeerId { 72 fn default() -> Self { 73 PeerId { id: [0; 32] }