use openehr_base::v1_3::base_types::identification::object_version_id::ObjectVersionId;
use sha2::Digest;
use crate::engine::seam::identity::IdentityRequest;
const ALPHABET: &[u8; 32] = b"abcdefghijklmnopqrstuvwxyz234567";
pub const DIGEST_CHARACTERS: usize = 52;
pub const ROOT_PATH: &str = "/";
#[must_use]
pub fn versioned_object_uid(uid: &str) -> String {
ObjectVersionId::new(uid).map_or_else(
|_| uid.to_owned(),
|version| version.object_id_str().to_owned(),
)
}
#[must_use]
pub fn deterministic_id(versioned_object_uid: &str, entry_path: &str) -> String {
let mut hasher = sha2::Sha256::new();
for field in [versioned_object_uid.as_bytes(), entry_path.as_bytes()] {
hasher.update(u64::try_from(field.len()).unwrap_or(u64::MAX).to_be_bytes());
hasher.update(field);
}
base32(hasher.finalize().as_slice())
}
#[must_use]
pub fn created_id(
versioned_object_uid: &str,
entry_path: &str,
request: &IdentityRequest,
) -> String {
let mut hasher = sha2::Sha256::new();
let mut frame = |field: &[u8]| {
hasher.update(u64::try_from(field.len()).unwrap_or(u64::MAX).to_be_bytes());
hasher.update(field);
};
frame(b"created");
frame(versioned_object_uid.as_bytes());
frame(entry_path.as_bytes());
frame(request.resource_type().as_bytes());
frame(request.parent_type().as_bytes());
frame(request.mapping().as_bytes());
frame(
&u64::try_from(request.occurrence().len())
.unwrap_or(u64::MAX)
.to_be_bytes(),
);
for position in request.occurrence() {
frame(&position.to_be_bytes());
}
frame(
&u64::try_from(request.unique().len())
.unwrap_or(u64::MAX)
.to_be_bytes(),
);
for value in request.unique() {
frame(value.as_bytes());
}
base32(hasher.finalize().as_slice())
}
#[must_use]
pub fn base32(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len().saturating_mul(8).div_ceil(5));
let mut buffer: u32 = 0;
let mut bits: u32 = 0;
for &byte in bytes {
buffer = (buffer << 8) | u32::from(byte);
bits = bits.saturating_add(8);
while bits >= 5 {
bits = bits.saturating_sub(5);
out.push(symbol((buffer >> bits) & 0x1f));
}
}
if bits > 0 {
out.push(symbol((buffer << (5_u32.saturating_sub(bits))) & 0x1f));
}
out
}
fn symbol(value: u32) -> char {
let index = usize::try_from(value).unwrap_or(0);
ALPHABET.get(index).map_or('a', |&byte| char::from(byte))
}
#[cfg(test)]
mod tests {
use super::DIGEST_CHARACTERS;
use super::base32;
use super::deterministic_id;
use super::versioned_object_uid;
#[test]
fn a_deterministic_id_is_52_characters_inside_the_r4_grammar() {
let id = deterministic_id(
"d2b3c1a0-0000-4000-8000-000000000001",
"/content[openEHR-EHR-EVALUATION.problem_diagnosis.v1]",
);
assert_eq!(id.len(), DIGEST_CHARACTERS);
assert!(
id.chars()
.all(|character| character.is_ascii_lowercase() || ('2'..='7').contains(&character))
);
}
#[test]
fn the_id_moves_with_either_input_and_never_with_the_version() {
let uid = "d2b3c1a0-0000-4000-8000-000000000001";
let first = deterministic_id(uid, "/content[openEHR-EHR-EVALUATION.problem_diagnosis.v1]");
assert_ne!(first, deterministic_id(uid, "/content[2]"));
assert_ne!(
first,
deterministic_id(
"d2b3c1a0-0000-4000-8000-000000000002",
"/content[openEHR-EHR-EVALUATION.problem_diagnosis.v1]"
)
);
assert_eq!(
versioned_object_uid("d2b3c1a0-0000-4000-8000-000000000001::ferrobridge.example::3"),
uid
);
}
#[test]
fn framing_keeps_a_field_boundary() {
assert_ne!(deterministic_id("ab", "c"), deterministic_id("a", "bc"));
}
#[test]
fn base32_renders_the_rfc_4648_vectors_in_lower_case() {
assert_eq!(base32(b""), "");
assert_eq!(base32(b"f"), "my");
assert_eq!(base32(b"fo"), "mzxq");
assert_eq!(base32(b"foo"), "mzxw6");
assert_eq!(base32(b"foob"), "mzxw6yq");
assert_eq!(base32(b"fooba"), "mzxw6ytb");
assert_eq!(base32(b"foobar"), "mzxw6ytboi");
}
}