Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ serde_json = "1"
serde_yaml = "0.9"
sha1 = "0.10"
sha2 = "0.10"
blake3 = "1"
shell-escape = "0.1"
shell-words = "1"
signal-hook = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion e2e/backend/test_github
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ assert_contains "cat mise.lock" '[tools."github:jdx/mise-test-fixtures"]'
assert_contains "cat mise.lock" 'version = "1.0.0"'
assert_contains "cat mise.lock" 'backend = "github:jdx/mise-test-fixtures"'
assert_contains "cat mise.lock" '[tools."github:jdx/mise-test-fixtures".checksums]'
assert_contains "cat mise.lock" '"hello-world-1.0.0.tar.gz" = "sha256:dbca4f08377d70dc0828f5822fc47ecec3895cd9a6dacbc54cc984d346a571af"'
assert_contains "cat mise.lock" '"hello-world-1.0.0.tar.gz" = "blake3:71f774faa03daf1a58cc3339f8c73e6557348c8e0a2f3fb8148cc26e26bad83f"'
2 changes: 1 addition & 1 deletion e2e/backend/test_gitlab
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ assert_contains "cat mise.lock" '[tools."gitlab:jdxcode/mise-test-fixtures"]'
assert_contains "cat mise.lock" 'version = "1.0.0"'
assert_contains "cat mise.lock" 'backend = "gitlab:jdxcode/mise-test-fixtures"'
assert_contains "cat mise.lock" '[tools."gitlab:jdxcode/mise-test-fixtures".checksums]'
assert_contains "cat mise.lock" '"hello-world-1.0.0.tar.gz" = "sha256:dbca4f08377d70dc0828f5822fc47ecec3895cd9a6dacbc54cc984d346a571af"'
assert_contains "cat mise.lock" '"hello-world-1.0.0.tar.gz" = "blake3:71f774faa03daf1a58cc3339f8c73e6557348c8e0a2f3fb8148cc26e26bad83f"'
2 changes: 1 addition & 1 deletion e2e/backend/test_http
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ assert_contains "cat mise.lock" '[tools."http:hello-lock"]'
assert_contains "cat mise.lock" 'version = "1.0.0"'
assert_contains "cat mise.lock" 'backend = "http:hello-lock"'
assert_contains "cat mise.lock" '[tools."http:hello-lock".checksums]'
assert_contains "cat mise.lock" '"hello-world-1.0.0.tar.gz" = "sha256:dbca4f08377d70dc0828f5822fc47ecec3895cd9a6dacbc54cc984d346a571af"'
assert_contains "cat mise.lock" '"hello-world-1.0.0.tar.gz" = "blake3:71f774faa03daf1a58cc3339f8c73e6557348c8e0a2f3fb8148cc26e26bad83f"'
1 change: 1 addition & 0 deletions src/aqua/aqua_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub struct AquaFile {
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum AquaChecksumAlgorithm {
Blake3,
Sha1,
Sha256,
Sha512,
Expand Down
6 changes: 0 additions & 6 deletions src/backend/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ impl Backend for UnifiedGitBackend {
HTTP.download_file(&asset_url, &file_path, Some(&ctx.pr))
.await?;

// Only add checksum if it doesn't already exist (for lockfile verification)
if let std::collections::btree_map::Entry::Vacant(e) = tv.checksums.entry(filename) {
let hash = hash::file_hash_sha256(&file_path, Some(&ctx.pr))?;
e.insert(format!("sha256:{hash}"));
}

// Verify
self.verify_artifact(&tv, &file_path, &opts)?;

Expand Down
6 changes: 0 additions & 6 deletions src/backend/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ impl Backend for HttpBackend {
ctx.pr.set_message(format!("download {filename}"));
HTTP.download_file(&url, &file_path, Some(&ctx.pr)).await?;

// Only add checksum if it doesn't already exist (for lockfile verification)
if let std::collections::btree_map::Entry::Vacant(e) = tv.checksums.entry(filename) {
let hash = hash::file_hash_sha256(&file_path, Some(&ctx.pr))?;
e.insert(format!("sha256:{hash}"));
}

// Verify
self.verify_artifact(&tv, &file_path, &opts)?;

Expand Down
4 changes: 2 additions & 2 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,8 @@ pub trait Backend: Debug + Send + Sync {
}
} else if Settings::get().lockfile && Settings::get().experimental {
ctx.pr.set_message(format!("generate checksum {filename}"));
let hash = hash::file_hash_sha256(file, Some(&ctx.pr))?;
tv.checksums.insert(filename, format!("sha256:{hash}"));
let hash = hash::file_hash_blake3(file, Some(&ctx.pr))?;
tv.checksums.insert(filename, format!("blake3:{hash}"));
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/backend/ubi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ impl Backend for UbiBackend {
} else if Settings::get().lockfile && Settings::get().experimental {
ctx.pr
.set_message(format!("checksum generate {checksum_key}"));
let hash = hash::file_hash_sha256(file, Some(&ctx.pr))?;
tv.checksums.insert(checksum_key, format!("sha256:{hash}"));
let hash = hash::file_hash_blake3(file, Some(&ctx.pr))?;
tv.checksums.insert(checksum_key, format!("blake3:{hash}"));
}
Ok(())
}
Expand Down
29 changes: 29 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::path::Path;
use crate::file;
use crate::file::display_path;
use crate::ui::progress_report::SingleReport;
use blake3::Hasher as Blake3Hasher;
use digest::Digest;
use eyre::{Result, bail};
use md5::Md5;
Expand Down Expand Up @@ -62,6 +63,33 @@ where
Ok(format!("{hash:x}"))
}

pub fn hash_blake3_to_str(s: &str) -> String {
let mut hasher = Blake3Hasher::new();
hasher.update(s.as_bytes());
hasher.finalize().to_hex().to_string()
}

pub fn file_hash_blake3(path: &Path, pr: Option<&Box<dyn SingleReport>>) -> Result<String> {
let mut file = file::open(path)?;
if let Some(pr) = pr {
pr.set_length(file.metadata()?.len());
}
let mut hasher = Blake3Hasher::new();
let mut buf = [0; 32 * 1024];
loop {
let n = file.read(&mut buf)?;
if n == 0 {
break;
}
hasher.update(&buf[..n]);
if let Some(pr) = pr {
pr.inc(n as u64);
}
}
let hash = hasher.finalize();
Ok(format!("{}", hash.to_hex()))
}

pub fn ensure_checksum(
path: &Path,
checksum: &str,
Expand All @@ -70,6 +98,7 @@ pub fn ensure_checksum(
) -> Result<()> {
let use_external_hasher = file::size(path).unwrap_or(u64::MAX) > 10 * 1024 * 1024;
let actual = match algo {
"blake3" => file_hash_blake3(path, pr)?,
"sha512" => {
if use_external_hasher && file::which("sha512sum").is_some() {
let out = cmd!("sha512sum", path).read()?;
Expand Down
10 changes: 5 additions & 5 deletions src/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static TERA: Lazy<Tera> = Lazy::new(|| {
move |input: &Value, args: &HashMap<String, Value>| match input {
Value::String(s) => {
let path = Path::new(s);
let mut hash = hash::file_hash_sha256(path, None).unwrap();
let mut hash = hash::file_hash_blake3(path, None).unwrap();
if let Some(len) = args.get("len").and_then(Value::as_u64) {
hash = hash.chars().take(len as usize).collect();
}
Expand All @@ -108,7 +108,7 @@ static TERA: Lazy<Tera> = Lazy::new(|| {
"hash",
move |input: &Value, args: &HashMap<String, Value>| match input {
Value::String(s) => {
let mut hash = hash::hash_sha256_to_str(s);
let mut hash = hash::hash_blake3_to_str(s);
if let Some(len) = args.get("len").and_then(Value::as_u64) {
hash = hash.chars().take(len as usize).collect();
}
Expand Down Expand Up @@ -343,7 +343,7 @@ pub fn tera_exec(
cmd = cmd.dir(dir);
}
let result = if cache.is_some() || cache_duration.is_some() {
let cachehash = hash::hash_sha256_to_str(
let cachehash = hash::hash_blake3_to_str(
&(dir
.as_ref()
.map(|d| d.to_string_lossy().to_string())
Expand Down Expand Up @@ -553,15 +553,15 @@ mod tests {
async fn test_hash() {
let _config = Config::get().await.unwrap();
let s = render("{{ \"foo\" | hash(len=8) }}");
assert_eq!(s, "2c26b46b");
assert_eq!(s, "04e0bb39");
}

#[tokio::test]
#[cfg(unix)]
async fn test_hash_file() {
let _config = Config::get().await.unwrap();
let s = render("{{ \"../fixtures/shorthands.toml\" | hash_file(len=64) }}");
insta::assert_snapshot!(s, @"518349c5734814ff9a21ab8d00ed2da6464b1699910246e763a4e6d5feb139fa");
insta::assert_snapshot!(s, @"ce17f44735ea2083038e61c4b291ed31593e6cf4d93f5dc147e97e62962ac4e6");
}

#[tokio::test]
Expand Down
Loading