Skip to content

Commit

Permalink
feat(api): simplify from content api
Browse files Browse the repository at this point in the history
This makes it more intuitive to form an XorName from a piece of data.
  • Loading branch information
oetyng committed Aug 26, 2021
1 parent 12c502e commit 441acc7
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 MaidSafe.net limited.
// Copyright 2021 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// http://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
Expand Down Expand Up @@ -111,13 +111,18 @@ pub const XOR_NAME_LEN: usize = 32;
pub struct XorName(pub [u8; XOR_NAME_LEN]);

impl XorName {
/// Generate a XorName for the given content.
pub fn from_content(content: &[u8]) -> Self {
Self::from_content_parts(&[content])
}

/// Generate a XorName for the given content (for content-addressable-storage)
pub fn from_content(content_parts: &[&[u8]]) -> Self {
pub fn from_content_parts(content_parts: &[&[u8]]) -> Self {
let mut sha3 = Sha3::v256();
for part in content_parts {
sha3.update(part);
}
let mut hash = [0u8; 32];
let mut hash = [0u8; XOR_NAME_LEN];
sha3.finalize(&mut hash);
Self(hash)
}
Expand Down Expand Up @@ -655,18 +660,18 @@ mod tests {

#[test]
fn xor_name_from_content() {
let alpha_1 = XorName::from_content(&[b"abcdefg", b"hijk"]);
let alpha_2 = XorName::from_content(&[b"abcdefg", b"hijk"]);
let alpha_3 = XorName::from_content(&[b"abcdefg"]);
let alpha_1 = XorName::from_content_parts(&[b"abcdefg", b"hijk"]);
let alpha_2 = XorName::from_content_parts(&[b"abcdefg", b"hijk"]);
let alpha_3 = XorName::from_content(b"abcdefg");

assert_eq!(alpha_1, alpha_2);
assert_ne!(alpha_1, alpha_3);
}

#[test]
fn xor_name_from_content_is_agnostic_to_where_content_parts_splits() {
let alpha_1 = XorName::from_content(&[b"abcdefg", b"hijk"]);
let alpha_2 = XorName::from_content(&[b"abcdefghijk"]);
let alpha_1 = XorName::from_content_parts(&[b"abcdefg", b"hijk"]);
let alpha_2 = XorName::from_content(b"abcdefghijk");
assert_eq!(alpha_1, alpha_2);
}

Expand Down

0 comments on commit 441acc7

Please sign in to comment.