Skip to content

Commit

Permalink
U32 encoding handled in the same way as hex and base64. Although its …
Browse files Browse the repository at this point in the history
…use is limited to crc32
  • Loading branch information
lookbusy1344 committed May 1, 2024
1 parent e013c87 commit 36c016a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum OutputEncoding {
Hex,
Base64,
Base32,
U32,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
12 changes: 4 additions & 8 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,15 @@ pub fn hash_file_encoded<D: Digest>(
OutputEncoding::Hex => hex::encode(h),
OutputEncoding::Base64 => BASE64.encode(&h),
OutputEncoding::Base32 => BASE32.encode(&h),
OutputEncoding::U32 => {
let number = BigEndian::read_u32(&h);
format!("{number:010}")
}
};

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)?;
let number = BigEndian::read_u32(&h);
Ok(BasicHash(format!("{number:010}")))
}

/// check if file exists
pub fn file_exists(path: impl AsRef<Path>) -> bool {
let path_ref = path.as_ref();
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::classes::{
use blake2::{Blake2b512, Blake2s256};
use classes::OutputEncoding;
use glob::GlobResult;
use hasher::{file_exists, hash_file_encoded, hash_file_u32};
use hasher::{file_exists, hash_file_encoded};
use md5::Md5;
use rayon::prelude::*;
use sha1::Sha1;
Expand Down Expand Up @@ -238,7 +238,7 @@ fn file_hashes_mt(config: &ConfigSettings, paths: &[String]) {
fn call_hasher(algo: HashAlgorithm, path: &str) -> anyhow::Result<BasicHash> {
match algo {
// special case, u32 encoded
HashAlgorithm::CRC32 => hash_file_u32::<crc32::Crc32>(path),
HashAlgorithm::CRC32 => hash_file_encoded::<crc32::Crc32>(path, OutputEncoding::U32),
// old algorithms
HashAlgorithm::MD5 => hash_file_encoded::<Md5>(path, OutputEncoding::Hex),
HashAlgorithm::SHA1 => hash_file_encoded::<Sha1>(path, OutputEncoding::Hex),
Expand Down

0 comments on commit 36c016a

Please sign in to comment.