Move hash into its own module

This commit is contained in:
Greg Fitzgerald 2018-03-06 17:20:37 -07:00
parent 5d990502cb
commit 5a44c36b1f
11 changed files with 37 additions and 28 deletions

View File

@ -2,7 +2,8 @@
//! event log to record transactions. Its users can deposit funds and
//! transfer funds to other users.
use log::{Entry, Sha256Hash};
use hash::Sha256Hash;
use log::Entry;
use event::Event;
use transaction::Transaction;
use signature::{PublicKey, Signature};

View File

@ -2,7 +2,8 @@ use std::io;
use accountant::Accountant;
use transaction::Transaction;
use signature::PublicKey;
use log::{Entry, Sha256Hash};
use hash::Sha256Hash;
use log::Entry;
use std::net::UdpSocket;
use bincode::{deserialize, serialize};

View File

@ -7,7 +7,8 @@ use std::io;
use bincode::{deserialize, serialize};
use transaction::Transaction;
use signature::{PublicKey, Signature};
use log::{Entry, Sha256Hash};
use hash::Sha256Hash;
use log::Entry;
use ring::signature::Ed25519KeyPair;
use accountant_skel::{Request, Response};

View File

@ -1,7 +1,8 @@
extern crate silk;
use silk::historian::Historian;
use silk::log::{verify_slice, Entry, Sha256Hash};
use silk::hash::Sha256Hash;
use silk::log::{verify_slice, Entry};
use silk::signature::{generate_keypair, get_pubkey};
use silk::transaction::Transaction;
use silk::event::Event;

View File

@ -3,7 +3,8 @@
use event::Event;
use transaction::Transaction;
use signature::{generate_keypair, get_pubkey, PublicKey};
use log::{create_entries, hash, Entry, Sha256Hash};
use log::{create_entries, Entry};
use hash::{hash, Sha256Hash};
use ring::rand::SystemRandom;
use ring::signature::Ed25519KeyPair;
use untrusted::Input;

19
src/hash.rs Normal file
View File

@ -0,0 +1,19 @@
use generic_array::GenericArray;
use generic_array::typenum::U32;
use sha2::{Digest, Sha256};
pub type Sha256Hash = GenericArray<u8, U32>;
/// Return a Sha256 hash for the given data.
pub fn hash(val: &[u8]) -> Sha256Hash {
let mut hasher = Sha256::default();
hasher.input(val);
hasher.result()
}
/// Return the hash of the given hash extended with the given value.
pub fn extend_and_hash(id: &Sha256Hash, val: &[u8]) -> Sha256Hash {
let mut hash_data = id.to_vec();
hash_data.extend_from_slice(val);
hash(&hash_data)
}

View File

@ -5,7 +5,8 @@ use std::thread::{spawn, JoinHandle};
use std::collections::HashSet;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::time::Instant;
use log::{hash, Entry, Sha256Hash};
use hash::{hash, Sha256Hash};
use log::Entry;
use logger::{ExitReason, Logger};
use signature::Signature;
use event::Event;

View File

@ -1,4 +1,5 @@
#![cfg_attr(feature = "unstable", feature(test))]
pub mod hash;
pub mod log;
pub mod logger;
pub mod event;

View File

@ -13,15 +13,11 @@
/// fastest processor. Duration should therefore be estimated by assuming that the hash
/// was generated by the fastest processor at the time the entry was logged.
use generic_array::GenericArray;
use generic_array::typenum::U32;
use hash::{extend_and_hash, hash, Sha256Hash};
use serde::Serialize;
use event::Event;
use sha2::{Digest, Sha256};
use rayon::prelude::*;
pub type Sha256Hash = GenericArray<u8, U32>;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Entry<T> {
pub num_hashes: u64,
@ -50,20 +46,6 @@ impl<T: Serialize> Entry<T> {
}
}
/// Return a Sha256 hash for the given data.
pub fn hash(val: &[u8]) -> Sha256Hash {
let mut hasher = Sha256::default();
hasher.input(val);
hasher.result()
}
/// Return the hash of the given hash extended with the given value.
pub fn extend_and_hash(id: &Sha256Hash, val: &[u8]) -> Sha256Hash {
let mut hash_data = id.to_vec();
hash_data.extend_from_slice(val);
hash(&hash_data)
}
/// Creates the hash 'num_hashes' after start_hash. If the event contains
/// signature, the final hash will be a hash of both the previous ID and
/// the signature.

View File

@ -7,7 +7,8 @@
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
use std::time::{Duration, Instant};
use log::{create_entry_mut, Entry, Sha256Hash};
use hash::Sha256Hash;
use log::{create_entry_mut, Entry};
use event::Event;
use serde::Serialize;
use std::fmt::Debug;

View File

@ -4,7 +4,7 @@ use signature::{get_pubkey, verify_signature, PublicKey, Signature};
use ring::signature::Ed25519KeyPair;
use serde::Serialize;
use bincode::serialize;
use log::Sha256Hash;
use hash::Sha256Hash;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Transaction<T> {
@ -51,8 +51,8 @@ impl<T: Serialize> Transaction<T> {
mod tests {
use super::*;
use bincode::{deserialize, serialize};
use log::*;
use signature::*;
use hash::hash;
#[test]
fn test_claim() {