Skip to content

Commit

Permalink
Chain update Sha256 one UTF16 char at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
russellbanks committed Oct 30, 2024
1 parent b7d6323 commit 5714d4a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "package-family-name"
version = "2.0.0"
version = "2.0.1"
edition = "2021"
license = "MIT OR Apache-2.0"
categories = ["no-std"]
Expand Down
22 changes: 12 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ pub struct PackageFamilyName {
publisher_id: PublisherId,
}

impl fmt::Display for PackageFamilyName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}_{}", self.identity_name, self.publisher_id)
}
}

impl PackageFamilyName {
pub fn new(identity_name: &str, identity_publisher: &str) -> Self {
PackageFamilyName {
Expand All @@ -38,18 +32,25 @@ impl PackageFamilyName {
}

pub fn get_id(identity_publisher: &str) -> PublisherId {
const HASH_TRUNCATION_LENGTH: usize = 8;

let publisher_sha_256 = identity_publisher
.encode_utf16()
.flat_map(u16::to_le_bytes)
.fold(Sha256::new(), |buf, byte| buf.chain_update([byte]))
.fold(Sha256::new(), |hasher, char| hasher.chain_update(char.to_le_bytes()))
.finalize();

let crockford_encoded = CROCKFORD_LOWER.encode(&publisher_sha_256[..8]);
let crockford_encoded = CROCKFORD_LOWER.encode(&publisher_sha_256[..HASH_TRUNCATION_LENGTH]);

PublisherId(heapless::String::from_str(&crockford_encoded).unwrap())
}
}

impl fmt::Display for PackageFamilyName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}_{}", self.identity_name, self.publisher_id)
}
}

#[cfg(test)]
mod tests {
use alloc::string::ToString;
Expand All @@ -61,6 +62,7 @@ mod tests {
let package_family_name = PackageFamilyName::new("AppName", "Publisher Software");
assert_eq!(package_family_name.to_string(), "AppName_zj75k085cmj1a");
}

#[test]
fn test_publisher_id() {
let publisher_id = PackageFamilyName::get_id("Publisher Software");
Expand All @@ -71,6 +73,6 @@ mod tests {
fn test_different_publishers() {
let publisher_id1 = PackageFamilyName::get_id("Publisher Software");
let publisher_id2 = PackageFamilyName::get_id("Another Publisher");
assert_ne!(publisher_id1.to_string(), publisher_id2.to_string());
assert_ne!(publisher_id1, publisher_id2);
}
}
2 changes: 1 addition & 1 deletion src/publisher_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl FromStr for PublisherId {

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() != PUBLISHER_ID_LENGTH {
return Err("expected publisher id length of 13");
return Err("Expected Publisher ID length of 13");
}
Ok(PublisherId(heapless::String::from_str(s).unwrap()))
}
Expand Down
2 changes: 1 addition & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::string::ToString;
use core::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

impl<'identity> Serialize for PackageFamilyName {
impl Serialize for PackageFamilyName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down

0 comments on commit 5714d4a

Please sign in to comment.