Skip to content

Commit

Permalink
ksud: use sha1 library to calc hash
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Apr 24, 2024
1 parent ac4c6f7 commit 063d5c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions userspace/ksud/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 userspace/ksud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rust-embed = { version = "8", features = [
which = "6"
getopts = "0.2"
sha256 = "1"
sha1 = "0.10"
tempdir = "0.3"
chrono = "0.4"
hole-punch = { git = "https://github.com/tiann/hole-punch" }
Expand Down
31 changes: 19 additions & 12 deletions userspace/ksud/src/boot_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,20 +435,27 @@ fn do_patch(
Ok(())
}

#[cfg(target_os = "android")]
fn calculate_sha1(file_path: impl AsRef<Path>) -> io::Result<String> {
let mut file = File::open(file_path.as_ref())?;
let mut hasher = Sha1::new();
let mut buffer = [0; 1024];

loop {
let n = file.read(&mut buffer)?;
if n == 0 {
break;
}
hasher.update(&buffer[..n]);
}

let result = hasher.finalize();
Ok(format!("{:x}", result))
}

#[cfg(target_os = "android")]
fn do_backup(magiskboot: &Path, workdir: &Path, image: &str) -> Result<()> {
// calc boot sha1
let output = Command::new(magiskboot)
.current_dir(workdir)
.arg("sha1")
.arg(image)
.output()?;
ensure!(
output.status.success(),
"Cannot calculate sha1 of original boot!"
);
let output = String::from_utf8(output.stdout)?;
let sha1 = output.trim();
let sha1 = calculate_sha1(image)?;
let filename = format!("{KSU_BACKUP_FILE_PREFIX}{sha1}");

println!("- Backup stock boot image");
Expand Down

0 comments on commit 063d5c8

Please sign in to comment.