json_digest/
nonce.rs

1use super::*;
2
3/// Multibase-encoded random content, e.g. "urvU8F6HmEol5zOmHh_nnS1RiX5r3T2t9U_d_kQY7ZC-I"
4///
5/// The amount of entropy is chosen as 264 bits to end up with full digits in the base64
6/// encoding used.
7#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
8#[serde(transparent)]
9pub struct Nonce264(pub String);
10
11impl Nonce264 {
12    /// Generates a new [`Nonce264`]. Uses the `getrandom` crate to find the best source of entropy on the platform.
13    /// In JavaScript tests you might need to refer to <https://github.com/jsdom/jsdom/issues/1612> for
14    /// how to fix phantom browsers to comply with HTML5 specs.
15    pub fn generate() -> Self {
16        use rand::{thread_rng, RngCore};
17        let mut arr = [0u8; 33];
18        thread_rng().fill_bytes(&mut arr[..]);
19        let encoded = multibase::encode(multibase::Base::Base64Url, &arr[..]);
20        Self(encoded)
21    }
22}