Skip to content

Commit

Permalink
chore: replace blake3 with blake2
Browse files Browse the repository at this point in the history
Currently the main user of this is polkadot, using blake2 rather than
blake3 allows re-use of an existing artifact and avoids dependency bloat.
  • Loading branch information
drahnr committed Feb 24, 2022
1 parent 5ae6f4f commit 084cb55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/drahnr/expander.git"
fs-err = "2"
proc-macro2 = "1"
quote = "1"
blake3 = "1.3"
blake2 = "0.10"

[dev-dependencies]
baz = { path = "./tests/baz" }
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,20 @@ fn expand_to_file(
) -> Result<TokenStream, std::io::Error> {
let token_str = tokens.to_string();
let mut bytes = token_str.as_bytes();
let hash = blake3::hash(bytes);
let hash = <blake2::Blake2s256 as blake2::Digest>::digest(bytes);

let mut shortened = hash.to_hex();
shortened.truncate(12);
let digest: &[u8; 32] = hash.as_ref();

// take the leading 12 hex characters
let mut shortened_hex = String::with_capacity(12);
const TABLE: &[u8] = b"0123456789abcdef";
for &byte in digest.iter().take(6) {
shortened_hex.push(TABLE[((byte >> 4) & 0xf) as usize] as char);
shortened_hex.push(TABLE[((byte >> 0) & 0xf) as usize] as char);
}

let dest =
std::path::PathBuf::from(dest.display().to_string() + "-" + shortened.as_str() + ".rs");
std::path::PathBuf::from(dest.display().to_string() + "-" + shortened_hex.as_str() + ".rs");

if verbose {
eprintln!("expander: writing {}", dest.display());
Expand Down

0 comments on commit 084cb55

Please sign in to comment.