Skip to content

Commit

Permalink
Renamed ValueEncoding to OutputEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
lookbusy1344 committed May 1, 2024
1 parent 881c6d1 commit e013c87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum HashAlgorithm {

#[derive(Debug, Copy, Clone, PartialEq, Eq, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum ValueEncoding {
pub enum OutputEncoding {
Hex,
Base64,
Base32,
Expand Down
13 changes: 7 additions & 6 deletions src/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::classes::{BasicHash, ValueEncoding};
use crate::classes::{BasicHash, OutputEncoding};
use byteorder::{BigEndian, ByteOrder};
use data_encoding::{BASE32, BASE64};
use digest::{Digest, Output};
Expand Down Expand Up @@ -55,19 +55,20 @@ fn hash_file_whole<D: Digest>(filename: &str) -> anyhow::Result<Output<D>> {
#[inline]
pub fn hash_file_encoded<D: Digest>(
filename: &str,
encoding: ValueEncoding,
output_encoding: OutputEncoding,
) -> anyhow::Result<BasicHash> {
let h = hash_file::<D>(filename)?;

let encoded = match encoding {
ValueEncoding::Hex => hex::encode(h),
ValueEncoding::Base64 => BASE64.encode(&h),
ValueEncoding::Base32 => BASE32.encode(&h),
let encoded = match output_encoding {
OutputEncoding::Hex => hex::encode(h),
OutputEncoding::Base64 => BASE64.encode(&h),
OutputEncoding::Base32 => BASE32.encode(&h),
};

Ok(BasicHash(encoded))
}

/// Hash a file using CRC32, and return the result as a `BasicHash` u32
#[inline]
pub fn hash_file_u32<D: Digest>(filename: &str) -> anyhow::Result<BasicHash> {
let h = hash_file::<D>(filename)?;
Expand Down
26 changes: 13 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::classes::{
};
//use crate::hasher::hash_file_crc32;
use blake2::{Blake2b512, Blake2s256};
use classes::ValueEncoding;
use classes::OutputEncoding;
use glob::GlobResult;
use hasher::{file_exists, hash_file_encoded, hash_file_u32};
use md5::Md5;
Expand Down Expand Up @@ -240,22 +240,22 @@ fn call_hasher(algo: HashAlgorithm, path: &str) -> anyhow::Result<BasicHash> {
// special case, u32 encoded
HashAlgorithm::CRC32 => hash_file_u32::<crc32::Crc32>(path),
// old algorithms
HashAlgorithm::MD5 => hash_file_encoded::<Md5>(path, ValueEncoding::Hex),
HashAlgorithm::SHA1 => hash_file_encoded::<Sha1>(path, ValueEncoding::Hex),
HashAlgorithm::MD5 => hash_file_encoded::<Md5>(path, OutputEncoding::Hex),
HashAlgorithm::SHA1 => hash_file_encoded::<Sha1>(path, OutputEncoding::Hex),
// SHA2
HashAlgorithm::SHA2_224 => hash_file_encoded::<Sha224>(path, ValueEncoding::Hex),
HashAlgorithm::SHA2_256 => hash_file_encoded::<Sha256>(path, ValueEncoding::Hex),
HashAlgorithm::SHA2_384 => hash_file_encoded::<Sha384>(path, ValueEncoding::Hex),
HashAlgorithm::SHA2_512 => hash_file_encoded::<Sha512>(path, ValueEncoding::Hex),
HashAlgorithm::SHA2_224 => hash_file_encoded::<Sha224>(path, OutputEncoding::Hex),
HashAlgorithm::SHA2_256 => hash_file_encoded::<Sha256>(path, OutputEncoding::Hex),
HashAlgorithm::SHA2_384 => hash_file_encoded::<Sha384>(path, OutputEncoding::Hex),
HashAlgorithm::SHA2_512 => hash_file_encoded::<Sha512>(path, OutputEncoding::Hex),
// SHA3
HashAlgorithm::SHA3_256 => hash_file_encoded::<Sha3_256>(path, ValueEncoding::Hex),
HashAlgorithm::SHA3_384 => hash_file_encoded::<Sha3_384>(path, ValueEncoding::Hex),
HashAlgorithm::SHA3_512 => hash_file_encoded::<Sha3_512>(path, ValueEncoding::Hex),
HashAlgorithm::SHA3_256 => hash_file_encoded::<Sha3_256>(path, OutputEncoding::Hex),
HashAlgorithm::SHA3_384 => hash_file_encoded::<Sha3_384>(path, OutputEncoding::Hex),
HashAlgorithm::SHA3_512 => hash_file_encoded::<Sha3_512>(path, OutputEncoding::Hex),
// WHIRLPOOL
HashAlgorithm::Whirlpool => hash_file_encoded::<Whirlpool>(path, ValueEncoding::Hex),
HashAlgorithm::Whirlpool => hash_file_encoded::<Whirlpool>(path, OutputEncoding::Hex),
// BLAKE2
HashAlgorithm::Blake2S256 => hash_file_encoded::<Blake2s256>(path, ValueEncoding::Hex),
HashAlgorithm::Blake2B512 => hash_file_encoded::<Blake2b512>(path, ValueEncoding::Hex),
HashAlgorithm::Blake2S256 => hash_file_encoded::<Blake2s256>(path, OutputEncoding::Hex),
HashAlgorithm::Blake2B512 => hash_file_encoded::<Blake2b512>(path, OutputEncoding::Hex),
}
}

Expand Down

0 comments on commit e013c87

Please sign in to comment.