warlocks_cauldron/providers/
cryptographic.rs

1use blake2::{Digest, digest::core_api::CoreWrapper};
2pub use uuid::Uuid;
3
4use super::dependencies::*;
5
6
7/// Methods collection that provides cryptographic data
8pub struct Cryptographic;
9
10impl Cryptographic {
11    /// Generate UUID4 struct
12    /// 
13    /// return example: Uuid
14    pub fn uuid_object() -> Uuid {
15        Uuid::new_v4()
16    }
17
18    /// Generate UUID4 string
19    /// 
20    /// return example: hex string
21    pub fn uuid() -> String {
22        Self::uuid_object().to_string()
23    }
24
25    /// Generate random hash
26    /// 
27    /// return example: hex string
28    ///
29    /// # Arguments
30    /// * `algorithm` - Enum object
31    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    /// Generate byte string containing ``entropy`` bytes
50    /// 
51    /// The string has ``entropy`` random bytes, each byte
52    /// converted to two hex digits.
53    /// 
54    /// return example: vec!\[0u8, 1u8, 0u8\]
55    /// 
56    /// # Arguments
57    /// * `entropy` - Number of bytes (default: 32)
58    pub fn token_bytes(entropy: usize) -> Vec<u8> {
59        urandom(entropy)
60    }
61
62    /// Return a random text string, in hexadecimal
63    /// 
64    /// The string has *entropy* random bytes, each byte converted to two
65    /// hex digits.  If *entropy* is ``None`` or not supplied, a reasonable
66    /// default is used.
67    /// 
68    /// return example: hex string
69    /// 
70    /// # Arguments
71    /// * `entropy` - Number of bytes (default: 32)
72    pub fn token_hex(entropy: usize) -> String {
73        hex::encode(Self::token_bytes(entropy))
74    }
75
76    /// Return a random URL-safe text string, in Base64 encoding
77    /// 
78    /// return example: urlsafe string
79    ///
80    /// # Arguments
81    /// * `entropy` - Number of bytes (default: 32)
82    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    /// Generate BIP-39-compatible mnemonic phrase
93    /// 
94    /// return example: mnemonic string
95    pub fn mnemonic_phrase() -> String {
96        get_random_elements(WORDLIST.iter(), randint(12, 24))
97            .iter().join(" ")
98    }
99}