Skip to content

Commit

Permalink
Fix filesha512
Browse files Browse the repository at this point in the history
  • Loading branch information
slashexx committed Oct 16, 2024
1 parent cff0936 commit 04e6452
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions kclvm/runtime/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,28 +283,30 @@ pub extern "C" fn kclvm_crypto_filesha512(
let ctx = mut_ptr_as_ref(ctx);

if let Some(filepath) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
match File::open(&filepath) {
Ok(mut file) => {
let mut hasher = Sha512::new();
let mut buffer = Vec::new();

if let Err(e) = file.read_to_end(&mut buffer) {
return ValueRef::str(&format!("failed to read file '{}': {}", filepath, e)).into_raw(ctx);
}

hasher.update(&buffer);
let hash_result = hasher.finalize();

let hex = hash_result.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<String>();

return ValueRef::str(&hex).into_raw(ctx);
}
Err(e) => {
return ValueRef::str(&format!("failed to access file '{}': {}", filepath, e)).into_raw(ctx);
// Open the file
let mut file = File::open(&filepath)
.unwrap_or_else(|e| panic!("failed to access file '{}': {}", filepath, e));

// Create a SHA512 hasher instance
let mut hasher = Sha512::new();

// Read the file content in chunks and update the hasher
let mut buffer = [0; 4096]; // Use a fixed-size buffer for chunked reading
while let Ok(bytes_read) = file.read(&mut buffer) {
if bytes_read == 0 {
break; // End of file
}
hasher.update(&buffer[..bytes_read]);
}

// Compute the SHA512 hash
let hash_result = hasher.finalize();

let hex = hash_result.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<String>();

return ValueRef::str(&hex).into_raw(ctx);
}
panic!("filesha512() missing 1 required positional argument: 'filepath'");
}
Expand Down

0 comments on commit 04e6452

Please sign in to comment.