Skip to content

Commit

Permalink
use opaque blake3 as hash
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot committed Jan 30, 2025
1 parent 27f5801 commit 9b8781d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
28 changes: 26 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/lib/core/core_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ extension UiFlightPositionExtension on UiFlightPosition {

extension ImageDataExtension on Uint8List {
ImageData toImageData() =>
ImageData(data: this, sha256: calculateSha256(this));
ImageData(data: this, hash: ImageData.computeHash(this));
}
2 changes: 1 addition & 1 deletion app/lib/util/cached_memory_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CachedMemoryImage extends ImageProvider<CachedMemoryImage> {
);

factory CachedMemoryImage.fromImageData(ImageData imageData) =>
CachedMemoryImage(imageData.sha256, imageData.data);
CachedMemoryImage(imageData.hash, imageData.data);

final String tag;
final Uint8List bytes;
Expand Down
12 changes: 5 additions & 7 deletions applogic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ crate-type = ["cdylib", "staticlib", "lib"]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }

[dependencies]
phnxcoreclient = { path = "../coreclient" }
phnxapiclient = { path = "../apiclient" }
phnxtypes = { path = "../types" }

tracing = "0.1"
tracing-subscriber = { workspace = true }
parking_lot = "0.12"
uuid = { version = "1", features = ["v4"] }
phnxcoreclient = { path = "../coreclient" }
phnxapiclient = { path = "../apiclient" }
phnxtypes = { path = "../types" }
anyhow = { version = "1", features = ["backtrace"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand All @@ -36,9 +37,6 @@ flutter_rust_bridge = { version = "=2.7.0", features = ["chrono", "uuid"] }
notify-rust = "4"
chrono = { workspace = true }
jni = "0.21"

# Workspace dependencies
tokio-util = "0.7.13"
tokio-stream = "0.1.17"
sha2 = "0.10.8"
hex = "0.4.3"
blake3 = "1.5.5"
19 changes: 12 additions & 7 deletions applogic/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use phnxcoreclient::{
Message, MessageId, MimiContent, SystemMessage, UserProfile,
};
pub use phnxcoreclient::{ConversationId, ConversationMessageId};
use sha2::{Digest, Sha256};
use uuid::Uuid;

/// Mirror of the [`ConversationId`] types
Expand Down Expand Up @@ -445,29 +444,35 @@ impl UiUserProfile {
pub struct ImageData {
/// The image data
pub(crate) data: Vec<u8>,
/// SHA256 hash of the image as hex string
pub(crate) sha256: String,
/// Opaque hash of the image data as hex string
pub(crate) hash: String,
}

impl fmt::Debug for ImageData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ImageData")
.field("data", &self.data.len())
.field("sha256", &self.sha256)
.field("hash", &self.hash)
.finish()
}
}

impl ImageData {
pub(crate) fn from_bytes(data: Vec<u8>) -> Self {
let sha256 = Sha256::digest(data.as_slice());
let sha256 = hex::encode(sha256);
Self { data, sha256 }
let hash = Self::compute_hash(&data);
Self { data, hash }
}

pub(crate) fn from_asset(asset: Asset) -> Self {
match asset {
Asset::Value(bytes) => Self::from_bytes(bytes),
}
}

/// Computes opaque hashsum of the data and returns it as a hex string.
#[frb(sync, positional)]
pub fn compute_hash(bytes: &[u8]) -> String {
let hash = blake3::hash(bytes);
hash.to_hex().to_string()
}
}
10 changes: 0 additions & 10 deletions applogic/src/api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@

//! Misc. functions
use flutter_rust_bridge::frb;
use sha2::{Digest, Sha256};

pub fn delete_databases(client_db_path: String) -> anyhow::Result<()> {
phnxcoreclient::delete_databases(client_db_path.as_str())
}

/// Computes sha256 hashsum of the data and returns it as a hex string.
#[frb(sync, positional)]
pub fn calculate_sha256(data: &[u8]) -> String {
let sha256 = Sha256::digest(data);
hex::encode(sha256)
}

0 comments on commit 9b8781d

Please sign in to comment.