mirror of
https://github.com/jht5945/buildj.git
synced 2025-12-30 02:40:03 +08:00
update verify_file_integrity
This commit is contained in:
@@ -41,29 +41,24 @@ pub fn is_buildin_args(args: &Vec<String>) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify_file_integrity(integrity: &str, file_name: &str) -> XResult<bool> {
|
pub fn verify_file_integrity(integrity: &str, file_name: &str) -> XResult<bool> {
|
||||||
let digest_hex: &str;
|
match integrity.find('-') {
|
||||||
let calc_digest_hex: String;
|
None => Err(new_box_error(&format!("Not supported integrigty: {}", integrity))),
|
||||||
if integrity.starts_with("sha256:hex-") {
|
Some(index) => {
|
||||||
digest_hex = &integrity[11..];
|
let digest_hex = &integrity[index+1..];
|
||||||
calc_digest_hex = calc_file_sha256(file_name)?;
|
let calc_digest_hex = match &integrity[0..index] {
|
||||||
} else if integrity.starts_with("sha512:hex-") {
|
"sha256:hex" => calc_file_sha256(file_name)?,
|
||||||
digest_hex = &integrity[11..];
|
"sha512:hex" => calc_file_sha512(file_name)?,
|
||||||
calc_digest_hex = calc_file_sha512(file_name)?;
|
"sha1:hex" => calc_file_sha1(file_name)?,
|
||||||
} else if integrity.starts_with("sha1:hex-") {
|
"md5:hex" => calc_file_md5(file_name)?,
|
||||||
digest_hex = &integrity[9..];
|
_ => return Err(new_box_error(&format!("Not supported integrigty: {}", integrity))),
|
||||||
calc_digest_hex = calc_file_sha1(file_name)?;
|
};
|
||||||
} else if integrity.starts_with("md5:hex-") {
|
|
||||||
digest_hex = &integrity[8..];
|
|
||||||
calc_digest_hex = calc_file_md5(file_name)?;
|
|
||||||
} else {
|
|
||||||
return Err(new_box_error(&format!("Not supported integrigty: {}", integrity)));
|
|
||||||
}
|
|
||||||
|
|
||||||
let integrity_verify_result = digest_hex == calc_digest_hex.as_str();
|
let integrity_verify_result = digest_hex == calc_digest_hex.as_str();
|
||||||
if ! integrity_verify_result {
|
if ! integrity_verify_result {
|
||||||
print_message(MessageType::ERROR, &format!("Verify integrity failed, expected: {}, actual: {}", digest_hex, calc_digest_hex));
|
print_message(MessageType::ERROR, &format!("Verify integrity failed, expected: {}, actual: {}", digest_hex, calc_digest_hex));
|
||||||
}
|
}
|
||||||
Ok(integrity_verify_result)
|
Ok(integrity_verify_result)
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn calc_sha256(d: &[u8]) -> String {
|
pub fn calc_sha256(d: &[u8]) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user