diff options
author | gennyble <gen@nyble.dev> | 2024-10-11 21:54:58 -0500 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2024-10-11 21:54:58 -0500 |
commit | c2a15e4447d7535fc3b9f8fabcfacf26947a84d2 (patch) | |
tree | 4b566334c8e55518fdf5fb2b0af58eaf49b37d69 /src/atom/urn.rs | |
parent | b0f6242b8b936d47b32c227cab5b18b4902cf9c4 (diff) | |
download | awake-c2a15e4447d7535fc3b9f8fabcfacf26947a84d2.tar.gz awake-c2a15e4447d7535fc3b9f8fabcfacf26947a84d2.zip |
truly i do not know what any of this is; git reinit
Diffstat (limited to 'src/atom/urn.rs')
-rw-r--r-- | src/atom/urn.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/atom/urn.rs b/src/atom/urn.rs new file mode 100644 index 0000000..9ec73ae --- /dev/null +++ b/src/atom/urn.rs @@ -0,0 +1,49 @@ +use core::fmt; + +use rand::{rngs::OsRng, Rng}; + +//TODO: gen- Check URN are valid +// https://www.rfc-editor.org/rfc/rfc2141#section-2.1 +pub struct Urn { + /// Namespace Identifier + nid: String, + /// Namespace Specific string. This is the unique part of the identifier + nss: String, +} + +impl Urn { + /// awake specific function for generating a URN in the form of: + /// `urn:NID:1234-5678` + /// Where 1234-5678 is a randomly generated eight digit ID broken in two. + pub fn new_random<S: Into<String>>(nid: S) -> Self { + let first = random_base58(4); + let second = random_base58(4); + + Self { + nid: nid.into(), + nss: format!("{first}-{second}"), + } + } +} + +impl fmt::Display for Urn { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let Urn { nid, nss } = self; + + write!(f, "urn:{nid}:{nss}") + } +} + +const BASE58: &'static [u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; +const USER_ID_LENGTH: usize = 6; +const SESSION_ID_LENGTH: usize = 12; + +/// Random Base58 string, `count` characters long, using OsRng which is assumed +/// to be secure +/// > assumed that system always provides high-quality cryptographically secure random data +fn random_base58(count: usize) -> String { + let mut rng = OsRng::default(); + std::iter::from_fn(|| Some(BASE58[rng.gen_range(0..BASE58.len())] as char)) + .take(count) + .collect() +} |