Compare commits

..

No commits in common. "7a3db5424fb8bacf887c5a9607ff14b87f562d89" and "0037bca01749cb9bb0d1077852b2da0b6667e29a" have entirely different histories.

4 changed files with 19 additions and 31 deletions

View file

@ -1,15 +1,16 @@
//! an ia hash is made up of two parts: the hash algorithm and the hash itself.
//! this is simply to allow forward-compatibility.
//! hashes can be deserialised from strings representing the format `algorithm:value`.
//! hash values are stored as a slice of bytes, returned as a base 16 encoded string
//! when required to be presented as a string output.
use crate::error;
use digest::DynDigest;
use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
use digest::{Digest, DynDigest};
use serde::{
de::Visitor,
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
fmt::Display,
io::{Read, Write},
io::Write,
str::FromStr,
};
@ -25,25 +26,17 @@ pub enum HashAlgorithm {
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HashValue(Box<[u8]>);
pub struct HashValue(Vec<u8>);
impl HashValue {
pub fn new<T: AsRef<[u8]>>(val: T) -> Self {
Self(val.as_ref().to_owned().into_boxed_slice())
pub fn new(val: Box<[u8]>) -> Self {
Self(val.as_ref().to_vec())
}
}
impl Display for HashValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match String::from_utf8(self.0.to_vec()) {
Ok(s) => write!(f, "{}", s),
Err(_) => write!(
f,
"{}",
base16ct::lower::encode_string(&self.0)
// String::from_utf8_lossy(self.0.to_vec().as_slice())
),
}
write!(f, "{}", hex::encode(self.0.as_slice()))
}
}
@ -58,7 +51,7 @@ pub struct Hash {
impl Display for Hash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.algorithm, self.value.to_string())
write!(f, "{}:{}", self.algorithm, self.value)
}
}
@ -80,7 +73,7 @@ impl Hash {
pub fn new(alg: HashAlgorithm, val: String) -> Result<Self, error::Hash> {
Ok(Self {
algorithm: alg,
value: HashValue::new(val.as_str().as_bytes()),
value: HashValue(val.as_bytes().to_owned()),
})
}
}
@ -142,6 +135,6 @@ impl Serialize for Hash {
where
S: Serializer,
{
serializer.serialize_str(&format!("{}", self.to_string()))
serializer.serialize_str(&format!("{}:{}", self.algorithm, self.value))
}
}

View file

@ -100,15 +100,11 @@ impl File {
self.reset().unwrap();
let mut hasher = Hash::hasher_for(&alg)?;
// io::copy(self, &mut hasher)
// .map_err(|_| error::Hash::Internal)
// .and_then(|_| hasher.flush().map_err(|_| error::Hash::Internal))?;
io::copy(self, &mut hasher).map_err(|_| error::Hash::Internal)?;
let hash_value = hasher.finalize_reset();
hasher.flush().map_err(|_| error::Hash::Internal)?;
hasher.update(buf.as_bytes());
let hash_bytes = hasher.finalize();
let hash_value = HashValue::new(&hash_bytes);
let hash_value = HashValue::new(hash_value);
Ok(Hash {
algorithm: alg,

View file

@ -60,6 +60,7 @@ impl<'a> Fetch<'a> {
);
let mut file = fetcher.fetch(source, self.prefix).unwrap();
println!("{:?}", file.hash(HashAlgorithm::Sha2).unwrap());
Ok(file)
}

View file

@ -59,9 +59,7 @@ fn can_fetch() {
}
#[test]
fn can_hash_source_file() {
let mut input_bytes = vec![];
test_source_file().read_to_end(&mut input_bytes).unwrap();
fn can_hash_source() {
assert_eq!(
test_source_file()
.hash(ia::HashAlgorithm::Sha2)