-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add seed constant to contract ID (#26)
- Loading branch information
Showing
3 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,58 @@ | ||
use crate::Bytes32; | ||
use sha2::{Digest, Sha256}; | ||
|
||
pub fn hash(data: &[u8]) -> Bytes32 { | ||
let mut hasher = Sha256::new(); | ||
use std::iter; | ||
|
||
hasher.update(data); | ||
#[derive(Debug, Default, Clone)] | ||
pub struct Hasher(Sha256); | ||
|
||
<[u8; Bytes32::size_of()]>::from(hasher.finalize()).into() | ||
impl Hasher { | ||
pub fn input<B>(&mut self, data: B) | ||
where | ||
B: AsRef<[u8]>, | ||
{ | ||
self.0.update(data) | ||
} | ||
|
||
pub fn chain<B>(self, data: B) -> Self | ||
where | ||
B: AsRef<[u8]>, | ||
{ | ||
Self(self.0.chain(data)) | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
self.0.reset(); | ||
} | ||
|
||
pub fn hash<B>(data: B) -> Bytes32 | ||
where | ||
B: AsRef<[u8]>, | ||
{ | ||
let mut hasher = Sha256::new(); | ||
|
||
hasher.update(data); | ||
|
||
<[u8; Bytes32::size_of()]>::from(hasher.finalize()).into() | ||
} | ||
|
||
pub fn digest(&self) -> Bytes32 { | ||
<[u8; Bytes32::size_of()]>::from(self.0.clone().finalize()).into() | ||
} | ||
} | ||
|
||
impl<B> iter::FromIterator<B> for Hasher | ||
where | ||
B: AsRef<[u8]>, | ||
{ | ||
fn from_iter<T>(iter: T) -> Self | ||
where | ||
T: IntoIterator<Item = B>, | ||
{ | ||
let mut hasher = Hasher::default(); | ||
|
||
iter.into_iter().for_each(|i| hasher.input(i)); | ||
|
||
hasher | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters