Skip to content

Commit

Permalink
More work on base64 and base32 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
lookbusy1344 committed May 1, 2024
1 parent 6ee040e commit 881c6d1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pub enum HashAlgorithm {
Blake2S256,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum ValueEncoding {
Hex,
Base64,
Base32,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Basic hash string. This is a wrapper around a String
pub struct BasicHash(pub String);
Expand Down
28 changes: 13 additions & 15 deletions src/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::classes::BasicHash;
use crate::classes::{BasicHash, ValueEncoding};
use byteorder::{BigEndian, ByteOrder};
use data_encoding::{BASE32, BASE64};
use digest::{Digest, Output};
Expand Down Expand Up @@ -53,9 +53,19 @@ fn hash_file_whole<D: Digest>(filename: &str) -> anyhow::Result<Output<D>> {
}

#[inline]
pub fn hash_file_hex<D: Digest>(filename: &str) -> anyhow::Result<BasicHash> {
pub fn hash_file_encoded<D: Digest>(
filename: &str,
encoding: ValueEncoding,
) -> anyhow::Result<BasicHash> {
let h = hash_file::<D>(filename)?;
Ok(BasicHash(hex::encode(h)))

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

Ok(BasicHash(encoded))
}

#[inline]
Expand All @@ -65,18 +75,6 @@ pub fn hash_file_u32<D: Digest>(filename: &str) -> anyhow::Result<BasicHash> {
Ok(BasicHash(format!("{number:010}")))
}

#[inline]
pub fn hash_file_base64<D: Digest>(filename: &str) -> anyhow::Result<BasicHash> {
let h = hash_file::<D>(filename)?;
Ok(BasicHash(BASE64.encode(&h)))
}

#[inline]
pub fn hash_file_base32<D: Digest>(filename: &str) -> anyhow::Result<BasicHash> {
let h = hash_file::<D>(filename)?;
Ok(BasicHash(BASE32.encode(&h)))
}

/// check if file exists
pub fn file_exists(path: impl AsRef<Path>) -> bool {
let path_ref = path.as_ref();
Expand Down
30 changes: 16 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::classes::{
};
//use crate::hasher::hash_file_crc32;
use blake2::{Blake2b512, Blake2s256};
use classes::ValueEncoding;
use glob::GlobResult;
use hasher::{file_exists, hash_file_hex, hash_file_u32};
use hasher::{file_exists, hash_file_encoded, hash_file_u32};
use md5::Md5;
use rayon::prelude::*;
use sha1::Sha1;
Expand Down Expand Up @@ -236,24 +237,25 @@ fn file_hashes_mt(config: &ConfigSettings, paths: &[String]) {
/// calculate the hash of a file using given algorithm
fn call_hasher(algo: HashAlgorithm, path: &str) -> anyhow::Result<BasicHash> {
match algo {
// old algorithms
// special case, u32 encoded
HashAlgorithm::CRC32 => hash_file_u32::<crc32::Crc32>(path),
HashAlgorithm::MD5 => hash_file_hex::<Md5>(path),
HashAlgorithm::SHA1 => hash_file_hex::<Sha1>(path),
// old algorithms
HashAlgorithm::MD5 => hash_file_encoded::<Md5>(path, ValueEncoding::Hex),
HashAlgorithm::SHA1 => hash_file_encoded::<Sha1>(path, ValueEncoding::Hex),
// SHA2
HashAlgorithm::SHA2_224 => hash_file_hex::<Sha224>(path),
HashAlgorithm::SHA2_256 => hash_file_hex::<Sha256>(path),
HashAlgorithm::SHA2_384 => hash_file_hex::<Sha384>(path),
HashAlgorithm::SHA2_512 => hash_file_hex::<Sha512>(path),
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),
// SHA3
HashAlgorithm::SHA3_256 => hash_file_hex::<Sha3_256>(path),
HashAlgorithm::SHA3_384 => hash_file_hex::<Sha3_384>(path),
HashAlgorithm::SHA3_512 => hash_file_hex::<Sha3_512>(path),
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),
// WHIRLPOOL
HashAlgorithm::Whirlpool => hash_file_hex::<Whirlpool>(path),
HashAlgorithm::Whirlpool => hash_file_encoded::<Whirlpool>(path, ValueEncoding::Hex),
// BLAKE2
HashAlgorithm::Blake2S256 => hash_file_hex::<Blake2s256>(path),
HashAlgorithm::Blake2B512 => hash_file_hex::<Blake2b512>(path),
HashAlgorithm::Blake2S256 => hash_file_encoded::<Blake2s256>(path, ValueEncoding::Hex),
HashAlgorithm::Blake2B512 => hash_file_encoded::<Blake2b512>(path, ValueEncoding::Hex),
}
}

Expand Down

0 comments on commit 881c6d1

Please sign in to comment.