parent
3b1c01a2f9
commit
38fbe63e41
@ -1,5 +1,89 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "block-buffer"
|
||||||
|
version = "0.7.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b"
|
||||||
|
dependencies = [
|
||||||
|
"block-padding",
|
||||||
|
"byte-tools",
|
||||||
|
"byteorder",
|
||||||
|
"generic-array",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "block-padding"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5"
|
||||||
|
dependencies = [
|
||||||
|
"byte-tools",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byte-tools"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "digest"
|
||||||
|
version = "0.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
|
||||||
|
dependencies = [
|
||||||
|
"generic-array",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fake-simd"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "generator-native"
|
name = "generator-native"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
dependencies = [
|
||||||
|
"sha2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "generic-array"
|
||||||
|
version = "0.12.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec"
|
||||||
|
dependencies = [
|
||||||
|
"typenum",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "opaque-debug"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69"
|
||||||
|
dependencies = [
|
||||||
|
"block-buffer",
|
||||||
|
"digest",
|
||||||
|
"fake-simd",
|
||||||
|
"opaque-debug",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "typenum"
|
||||||
|
version = "1.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
|
||||||
|
@ -0,0 +1,95 @@
|
|||||||
|
use sha2::{Sha256, Digest};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test
|
||||||
|
{
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn hash_iter_okay()
|
||||||
|
{
|
||||||
|
let hash_this = vec![vec![0,1,2,3,4],
|
||||||
|
vec![5,6,7,8,9],
|
||||||
|
vec![10,11,12,13,14],
|
||||||
|
vec![15,16,17,18,19]];
|
||||||
|
|
||||||
|
let mut digest = Sha256::new();
|
||||||
|
let mut digest2 = Sha256::new();
|
||||||
|
for byte in hash_this.into_iter().into_hash_iter(&mut digest)
|
||||||
|
{
|
||||||
|
digest2.input(&byte[..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(digest.result()[..], digest2.result()[..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn copy_slice_works()
|
||||||
|
{
|
||||||
|
let slice = [0xab, 0xad, 0xca, 0xfe];
|
||||||
|
let mut output = [0u8; 4];
|
||||||
|
|
||||||
|
assert_eq!(copy_slice(&mut output, &slice), 4);
|
||||||
|
assert_eq!(slice[..], output[..]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HashingIter<'a, I, T>
|
||||||
|
where I: Iterator<Item=T>,
|
||||||
|
T: AsRef<[u8]>
|
||||||
|
{
|
||||||
|
iter: I,
|
||||||
|
digest: &'a mut Sha256,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl<'a, I, T> Iterator for HashingIter<'a, I, T>
|
||||||
|
where I: Iterator<Item=T>,
|
||||||
|
T: AsRef<[u8]>
|
||||||
|
{
|
||||||
|
type Item = T;
|
||||||
|
fn next(&mut self) -> Option<Self::Item>
|
||||||
|
{
|
||||||
|
match self.iter.next() {
|
||||||
|
Some(value) => {
|
||||||
|
self.digest.input(value.as_ref());
|
||||||
|
Some(value)
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait HashingIterExt: Iterator + Sized
|
||||||
|
where <Self as Iterator>::Item: AsRef<[u8]>
|
||||||
|
{
|
||||||
|
fn into_hash_iter<'a>(self, hash: &'a mut Sha256) -> HashingIter<'a, Self, <Self as Iterator>::Item>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I, T> HashingIterExt for I
|
||||||
|
where I: Iterator<Item=T>,
|
||||||
|
T: AsRef<[u8]>
|
||||||
|
{
|
||||||
|
fn into_hash_iter<'a>(self, hash: &'a mut Sha256) -> HashingIter<'a, I, T>
|
||||||
|
{
|
||||||
|
HashingIter {
|
||||||
|
iter: self,
|
||||||
|
digest: hash,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn copy_slice<T>(mut dst: impl AsMut<[T]>, src: impl AsRef<[T]>) -> usize
|
||||||
|
where T: Clone
|
||||||
|
{
|
||||||
|
let dst = dst.as_mut();
|
||||||
|
let src = src.as_ref();
|
||||||
|
|
||||||
|
let mut i=0;
|
||||||
|
for (d, s) in dst.iter_mut().zip(src.iter())
|
||||||
|
{
|
||||||
|
*d = s.clone();
|
||||||
|
i+=1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i
|
||||||
|
}
|
Loading…
Reference in new issue