apkverify:Test apk digest computation

Test: atest libapkverify.test
Change-Id: I33ade0f39f28494f6a0f7ed0cb324466ed2ae495
This commit is contained in:
Alice Wang 2022-09-09 14:08:19 +00:00
parent 25109dd9e5
commit 9807322a5e
2 changed files with 19 additions and 6 deletions

View File

@ -187,11 +187,11 @@ impl Digester {
// v2/v3 digests are computed after prepending "header" byte and "size" info. // v2/v3 digests are computed after prepending "header" byte and "size" info.
fn digest(&self, data: &[u8], header: &[u8], size: u32) -> Result<DigestBytes> { fn digest(&self, data: &[u8], header: &[u8], size: u32) -> Result<DigestBytes> {
let mut ctx = Hasher::new(self.algorithm)?; let mut hasher = Hasher::new(self.algorithm)?;
ctx.update(header)?; hasher.update(header)?;
ctx.update(&size.to_le_bytes())?; hasher.update(&size.to_le_bytes())?;
ctx.update(data)?; hasher.update(data)?;
Ok(ctx.finish()?) Ok(hasher.finish()?)
} }
} }
@ -313,6 +313,8 @@ mod tests {
use std::fs::File; use std::fs::File;
use std::mem::size_of_val; use std::mem::size_of_val;
use crate::v3::to_hex_string;
const CENTRAL_DIRECTORY_HEADER_SIGNATURE: u32 = 0x02014b50; const CENTRAL_DIRECTORY_HEADER_SIGNATURE: u32 = 0x02014b50;
#[test] #[test]
@ -345,4 +347,15 @@ mod tests {
(apk_sections.eocd_offset + apk_sections.eocd_size) as u64 (apk_sections.eocd_offset + apk_sections.eocd_size) as u64
); );
} }
#[test]
fn test_apk_digest() {
let apk_file = File::open("tests/data/v3-only-with-dsa-sha256-1024.apk").unwrap();
let mut apk_sections = ApkSections::new(apk_file).unwrap();
let digest = apk_sections.compute_digest(SIGNATURE_DSA_WITH_SHA256).unwrap();
assert_eq!(
"0DF2426EA33AEDAF495D88E5BE0C6A1663FF0A81C5ED12D5B2929AE4B4300F2F",
to_hex_string(&digest[..])
);
}
} }

View File

@ -299,7 +299,7 @@ impl ReadFromBytes for Digest {
} }
#[inline] #[inline]
fn to_hex_string(buf: &[u8]) -> String { pub(crate) fn to_hex_string(buf: &[u8]) -> String {
buf.iter().map(|b| format!("{:02X}", b)).collect() buf.iter().map(|b| format!("{:02X}", b)).collect()
} }