diff --git a/native/cpp/CommonCpp/CryptoTools/rust-utils/Cargo.toml b/native/cpp/CommonCpp/CryptoTools/rust-utils/Cargo.toml new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/CryptoTools/rust-utils/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rust-utils" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.4" +regex = "1" diff --git a/native/cpp/CommonCpp/CryptoTools/rust-utils/src/lib.rs b/native/cpp/CommonCpp/CryptoTools/rust-utils/src/lib.rs new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/CryptoTools/rust-utils/src/lib.rs @@ -0,0 +1,58 @@ +use rand::{distributions::Alphanumeric, Rng}; +use regex::Regex; + +#[derive(PartialEq)] +enum DeviceIDType { + KEYSERVER, + WEB, + MOBILE, +} + +fn generate_device_id(device_type: DeviceIDType) -> String { + let suffix: String = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(64) + .map(char::from) + .collect(); + if device_type == DeviceIDType::KEYSERVER { + return "ks:".to_string() + &suffix; + } else if device_type == DeviceIDType::WEB { + return "web:".to_string() + &suffix; + } else if device_type == DeviceIDType::MOBILE { + return "mobile:".to_string() + &suffix; + } else { + panic!("Unhandled DeviceIDType"); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ks_works() { + let result = generate_device_id(DeviceIDType::KEYSERVER); + println!("{}", result); + assert!(Regex::new(r"^(ks|mobile|web):[a-zA-Z0-9]{64}$") + .unwrap() + .is_match(&result)); + } + + #[test] + fn web_works() { + let result = generate_device_id(DeviceIDType::WEB); + println!("{}", result); + assert!(Regex::new(r"^(ks|mobile|web):[a-zA-Z0-9]{64}$") + .unwrap() + .is_match(&result)); + } + + #[test] + fn moble_works() { + let result = generate_device_id(DeviceIDType::MOBILE); + println!("{}", result); + assert!(Regex::new(r"^(ks|mobile|web):[a-zA-Z0-9]{64}$") + .unwrap() + .is_match(&result)); + } +}