warlocks_cauldron/providers/
cryptographic.rs1use blake2::{Digest, digest::core_api::CoreWrapper};
2pub use uuid::Uuid;
3
4use super::dependencies::*;
5
6
7pub struct Cryptographic;
9
10impl Cryptographic {
11 pub fn uuid_object() -> Uuid {
15 Uuid::new_v4()
16 }
17
18 pub fn uuid() -> String {
22 Self::uuid_object().to_string()
23 }
24
25 pub fn hash(fmt: Option<Algorithm>) -> String {
32 let uuid = Self::uuid().as_bytes().to_vec();
33
34 let bytes = match validate_variant(fmt, None) {
35 Algorithm::MD5 => md5::Md5::new_with_prefix(uuid).finalize().to_vec(),
36 Algorithm::SHA1 => sha1::Sha1::new_with_prefix(uuid).finalize().to_vec(),
37 Algorithm::SHA224 => sha2::Sha224::new_with_prefix(uuid).finalize().to_vec(),
38 Algorithm::SHA256 => sha2::Sha256::new_with_prefix(uuid).finalize().to_vec(),
39 Algorithm::SHA384 => sha2::Sha384::new_with_prefix(uuid).finalize().to_vec(),
40 Algorithm::SHA512 => sha2::Sha512::new_with_prefix(uuid).finalize().to_vec(),
41 Algorithm::BLAKE2B => blake2::Blake2b512::new_with_prefix(uuid).finalize().to_vec(),
42 Algorithm::BLAKE2S => blake2::Blake2s256::new_with_prefix(uuid).finalize().to_vec(),
43 _ => panic!("Incompatible algorithm!"),
44 };
45
46 String::from_utf8(bytes).expect("Cant convert hash to String!")
47 }
48
49 pub fn token_bytes(entropy: usize) -> Vec<u8> {
59 urandom(entropy)
60 }
61
62 pub fn token_hex(entropy: usize) -> String {
73 hex::encode(Self::token_bytes(entropy))
74 }
75
76 pub fn token_urlsafe(entropy: usize) -> String {
83 let bytes = Self::token_bytes(entropy);
84 let engine = base64::engine::fast_portable::FastPortable::from(
85 &base64::alphabet::URL_SAFE,
86 base64::engine::fast_portable::NO_PAD,
87 );
88
89 base64::encode_engine(bytes, &engine)
90 }
91
92 pub fn mnemonic_phrase() -> String {
96 get_random_elements(WORDLIST.iter(), randint(12, 24))
97 .iter().join(" ")
98 }
99}