diff --git a/.gitignore b/.gitignore --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ lib/node_modules native/cpp/CommonCpp/CryptoTools/opaque-ke-cxx/target +native/cpp/CommonCpp/CryptoTools/rust-utils/target web/node_modules web/dist diff --git a/native/cpp/CommonCpp/CryptoTools/rust-utils/Cargo.lock b/native/cpp/CommonCpp/CryptoTools/rust-utils/Cargo.lock new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/CryptoTools/rust-utils/Cargo.lock @@ -0,0 +1,108 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b" + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + +[[package]] +name = "rust-utils" +version = "0.1.0" +dependencies = [ + "rand", + "regex", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 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" +regex = "1.6" 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,60 @@ +use rand::{distributions::Alphanumeric, Rng}; +#[cfg(test)] +use regex::Regex; + +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(); + match device_type { + DeviceIDType::KEYSERVER => format!("ks:{}", &suffix), + DeviceIDType::WEB => format!("web:{}", &suffix), + DeviceIDType::MOBILE => format!("mobile:{}", &suffix), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn generate_device_id_ks() { + for _x in 1..100 { + 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 generate_device_id_web() { + for _x in 1..100 { + 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 generate_device_id_moble() { + for _x in 1..100 { + 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)); + } + } +}