json-digest 0.1.0

Allow selective masking of JSON subdocuments for privacy, preserving the digest of the whole document.
Documentation
use alloc::string::String;
#[cfg(feature = "std")]
use rand::{Rng, rng};
use serde::{Deserialize, Serialize};

/// Multibase-encoded random content, e.g. "urvU8F6HmEol5zOmHh_nnS1RiX5r3T2t9U_d_kQY7ZC-I"
///
/// The amount of entropy is chosen as 264 bits to end up with full digits in the base64
/// encoding used.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(transparent)]
pub struct Nonce264(pub String);

impl Nonce264 {
    /// Generates a new [`Nonce264`]. Uses the `getrandom` crate to find the best source of entropy on the platform.
    /// In JavaScript tests you might need to refer to <https://github.com/jsdom/jsdom/issues/1612> for
    /// how to fix phantom browsers to comply with HTML5 specs.
    #[cfg(feature = "std")]
    pub fn generate() -> Self {
        let mut arr = [0u8; 33];
        rng().fill_bytes(&mut arr[..]);
        let encoded = multibase::encode(multibase::Base::Base64Url, &arr[..]);
        Self(encoded)
    }
}