π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Trim the core hash module
Manav Rathif6286bc2bf06
diff --git a/rust/apps/cli-next/tests/support/export_resources.rs b/rust/apps/cli-next/tests/support/export_resources.rsindex e666645f3d..ab9a158b05 100644--- a/rust/apps/cli-next/tests/support/export_resources.rs+++ b/rust/apps/cli-next/tests/support/export_resources.rs@@ -75,10 +75,7 @@ fn export_recovers_after_real_disk_full() -> TestResult {export(&home, &root, &[]);assert_eq!(fs::metadata(root.join("Album/After.bin"))?.len(), size);assert_eq!(- b64::encode(&hash::hash_reader(- &mut fs::File::open(root.join("Album/After.bin"))?,- Some(64)- )?),+ digest(fs::File::open(root.join("Album/After.bin"))?)?,repeat_hash(31, size));assert_eq!(@@ -144,8 +141,7 @@ fn export_large_original_and_live_photo_with_bounded_memory() -> TestResult {] {let path = root.join("Large").join(name);assert_eq!(fs::metadata(&path)?.len(), length);- let actual = b64::encode(&hash::hash_reader(&mut fs::File::open(path)?, Some(64))?);- assert_eq!(actual, expected);+ assert_eq!(digest(fs::File::open(path)?)?, expected);}let reads = museum.object_store().reads();assert_eq!(reads, 2);@@ -219,7 +215,20 @@ fn export_many_records_unchanged_resource_check() -> TestResult {}fn repeat_hash(byte: u8, size: u64) -> String {- b64::encode(&hash::hash_reader(&mut std::io::repeat(byte).take(size), Some(64)).unwrap())+ digest(std::io::repeat(byte).take(size)).unwrap()+}++fn digest(mut reader: impl Read) -> TestResult<String> {+ let mut state = hash::HashState::new(Some(64), None)?;+ let mut bytes = vec![0u8; 64 * 1024];+ loop {+ let read = reader.read(&mut bytes)?;+ if read == 0 {+ break;+ }+ state.update(&bytes[..read]);+ }+ Ok(b64::encode(&state.finalize()))}fn measure(home: &TestHome, root: &Path, label: &str) -> TestResult<Value> {diff --git a/rust/crates/core/src/crypto/hash.rs b/rust/crates/core/src/crypto/hash.rsindex 681baf8dc5..e1c3c1836c 100644--- a/rust/crates/core/src/crypto/hash.rs+++ b/rust/crates/core/src/crypto/hash.rs@@ -2,7 +2,6 @@// `crypto_generichash`.use blake2b_simd::{Params as Blake2bParams, State as Blake2bState};-use std::io::Read;use crate::crypto::{Error, Result};@@ -13,49 +12,18 @@ pub const HASH_BYTES_MAX: usize = 64;// Libsodium's default hash output length is 32 bytes.pub const HASH_BYTES: usize = 32;-pub const HASH_CHUNK_SIZE: usize = 4 * 1024 * 1024;-pub const KEY_BYTES_MIN: usize = 16;pub const KEY_BYTES_MAX: usize = 64;pub fn hash(data: &[u8], out_len: Option<usize>, key: Option<&[u8]>) -> Result<Vec<u8>> {- let out_len = out_len.unwrap_or(HASH_BYTES);-- if !(HASH_BYTES_MIN..=HASH_BYTES_MAX).contains(&out_len) {- return Err(Error::InvalidKeyLength {- expected: HASH_BYTES_MAX,- actual: out_len,- });- }-- let mut params = Blake2bParams::new();- params.hash_length(out_len);-- if let Some(k) = key {- // Libsodium accepts an empty key or 16-64 bytes.- if !k.is_empty() && (k.len() < KEY_BYTES_MIN || k.len() > KEY_BYTES_MAX) {- return Err(Error::InvalidKeyLength {- expected: KEY_BYTES_MAX,- actual: k.len(),- });- }- if !k.is_empty() {- params.key(k);- }- }-- let hash = params.to_state().update(data).finalize();- Ok(hash.as_bytes()[..out_len].to_vec())-}--pub fn hash_default(data: &[u8]) -> Result<Vec<u8>> {- hash(data, Some(HASH_BYTES), None)+ let mut state = HashState::new(out_len, key)?;+ state.update(data);+ Ok(state.finalize())}pub struct HashState {state: Blake2bState,- out_len: usize,}impl HashState {@@ -87,50 +55,22 @@ impl HashState {let state = params.to_state();- Ok(HashState { state, out_len })+ Ok(HashState { state })}- pub fn update(&mut self, data: &[u8]) -> Result<()> {+ pub fn update(&mut self, data: &[u8]) {self.state.update(data);- Ok(())}- pub fn finalize(self) -> Result<Vec<u8>> {- let hash = self.state.finalize();- Ok(hash.as_bytes()[..self.out_len].to_vec())+ pub fn finalize(self) -> Vec<u8> {+ self.state.finalize().as_bytes().to_vec()}}-pub fn hash_state_new() -> Result<HashState> {- HashState::new(Some(HASH_BYTES), None)-}--pub fn hash_reader<R: Read>(reader: &mut R, out_len: Option<usize>) -> Result<Vec<u8>> {- let mut state = HashState::new(out_len, None)?;- let mut buffer = vec![0u8; 4096];-- loop {- let bytes_read = reader.read(&mut buffer)?;- if bytes_read == 0 {- break;- }- state.update(&buffer[..bytes_read])?;- }-- state.finalize()-}-#[cfg(test)]mod tests {use super::*;- #[test]- fn test_hash_default() {- let data = b"Hello, World!";- let hash = hash_default(data).unwrap();- assert_eq!(hash.len(), HASH_BYTES);- }-#[test]fn test_hash_with_length() {let data = b"Test data";@@ -144,8 +84,8 @@ mod tests {#[test]fn test_hash_deterministic() {let data = b"Deterministic test";- let hash1 = hash_default(data).unwrap();- let hash2 = hash_default(data).unwrap();+ let hash1 = hash(data, None, None).unwrap();+ let hash2 = hash(data, None, None).unwrap();assert_eq!(hash1, hash2);}@@ -154,8 +94,8 @@ mod tests {let data1 = b"First";let data2 = b"Second";- let hash1 = hash_default(data1).unwrap();- let hash2 = hash_default(data2).unwrap();+ let hash1 = hash(data1, None, None).unwrap();+ let hash2 = hash(data2, None, None).unwrap();assert_ne!(hash1, hash2);}@@ -202,11 +142,4 @@ mod tests {let hash2 = hash(data, Some(64), Some(&key_max)).unwrap();assert_eq!(hash2.len(), 64);}-- #[test]- fn test_empty_data() {- let data = b"";- let hash = hash_default(data).unwrap();- assert_eq!(hash.len(), HASH_BYTES);- }}diff --git a/rust/crates/photos-export/src/fs.rs b/rust/crates/photos-export/src/fs.rsindex 9df60b29c3..899a069be0 100644--- a/rust/crates/photos-export/src/fs.rs+++ b/rust/crates/photos-export/src/fs.rs@@ -34,7 +34,7 @@ impl HashWriter {pub fn finish(self) -> Result<(File, u64, String)> {self.file.sync_all()?;- Ok((self.file, self.size, b64::encode(&self.digest.finalize()?)))+ Ok((self.file, self.size, b64::encode(&self.digest.finalize())))}}@@ -44,9 +44,7 @@ impl Write for HashWriter {return Err(io::Error::other(super::Cancelled));}let written = self.file.write(bytes)?;- self.digest- .update(&bytes[..written])- .map_err(io::Error::other)?;+ self.digest.update(&bytes[..written]);self.size += written as u64;Ok(written)}@@ -66,10 +64,10 @@ pub fn hash_cancellable(path: &Path, cancel: &watch::Receiver<bool>) -> Result<(if read == 0 {break;}- digest.update(&bytes[..read])?;+ digest.update(&bytes[..read]);size += read as u64;}- Ok((size, b64::encode(&digest.finalize()?)))+ Ok((size, b64::encode(&digest.finalize())))}pub fn directory(root: &Path, relative: &str) -> Result<()> {diff --git a/rust/crates/photos-export/src/legacy/verify.rs b/rust/crates/photos-export/src/legacy/verify.rsindex 48ae8541dd..918d059fae 100644--- a/rust/crates/photos-export/src/legacy/verify.rs+++ b/rust/crates/photos-export/src/legacy/verify.rs@@ -216,14 +216,14 @@ async fn hashes(Digest::new(cancel).map_err(io::Error::other)})?.into_iter()- .map(|(role, _, writer)| Ok((role, writer.finish()?)))- .collect::<Result<Vec<_>>>()?+ .map(|(role, _, writer)| (role, writer.finish()))+ .collect()} else {let download = files::download(session, file.id, &file.key, &file.header, || {Digest::new(cancel).map_err(io::Error::other)});let writer = tokio::select! { result=download=>result?, _=receiver.changed()=>return Err(crate::Cancelled.into()) };- vec![(Role::Original, writer.finish()?)]+ vec![(Role::Original, writer.finish())]};Ok(hashes)}@@ -240,8 +240,8 @@ impl Digest {cancel: cancel.clone(),})}- fn finish(self) -> Result<String> {- Ok(b64::encode(&self.state.finalize()?))+ fn finish(self) -> String {+ b64::encode(&self.state.finalize())}}@@ -250,7 +250,7 @@ impl Write for Digest {if *self.cancel.borrow() {return Err(io::Error::other(crate::Cancelled));}- self.state.update(bytes).map_err(io::Error::other)?;+ self.state.update(bytes);Ok(bytes.len())}fn flush(&mut self) -> io::Result<()> {