oboron/keygen.rs
1use data_encoding::BASE64URL_NOPAD;
2use rand::RngCore;
3
4/// Generate a cryptographically secure random 64-byte key and return it as a base64 string.
5///
6/// This is a convenience function that generates a key and encodes it as a base64 string,
7/// useful for storage, display, or transmission. The base64 string will be 86 characters long.
8/// This function ensures the returned key does not contain any dashes
9/// (to make it double-click-selectable in GUIs).
10///
11/// # Examples
12///
13/// ```
14/// use oboron::generate_key;
15///
16/// let key = generate_key();
17/// assert_eq!(key.len(), 86);
18/// ```
19#[must_use]
20pub fn generate_key() -> String {
21 loop {
22 let mut key_bytes = [0u8; 64];
23 rand::thread_rng().fill_bytes(&mut key_bytes);
24 let key_base64 = BASE64URL_NOPAD.encode(&key_bytes);
25 if !key_base64.contains('-') && !key_base64.contains('_') {
26 return key_base64;
27 }
28 }
29}
30
31/// Generate a cryptographically secure random 64-byte key suitable for use with MasterKey.
32///
33/// This function generates a random key using a cryptographically secure random number generator.
34/// The key can be used directly with `MasterKey::from_bytes()`.
35///
36/// # Examples
37///
38/// ```
39/// use oboron::generate_key_bytes;
40///
41/// let key = generate_key_bytes();
42/// assert_eq!(key.len(), 64);
43/// ```
44#[must_use]
45#[cfg(feature = "bytes-keys")]
46pub fn generate_key_bytes() -> [u8; 64] {
47 let decoded = BASE64URL_NOPAD
48 .decode(generate_key().as_bytes())
49 .expect("Failed to decode base64");
50 decoded.try_into().expect("Decoded key is not 64 bytes")
51}
52
53/// Generate a cryptographically secure random 64-byte key and return it as a hex string.
54///
55/// This is a convenience function that generates a key and encodes it as a hexadecimal string,
56/// useful for storage, display, or transmission. The hex string will be 128 characters long.
57///
58/// # Examples
59///
60/// ```
61/// use oboron::generate_key_hex;
62///
63/// let key_hex = generate_key_hex();
64/// assert_eq!(key_hex.len(), 128); // 64 bytes * 2 hex chars per byte
65/// ```
66#[must_use]
67#[cfg(feature = "hex-keys")]
68pub fn generate_key_hex() -> String {
69 let decoded = BASE64URL_NOPAD
70 .decode(generate_key().as_bytes())
71 .expect("Failed to decode base64");
72 let key_bytes: [u8; 64] = decoded.try_into().expect("Decoded key is not 64 bytes");
73 hex::encode(&key_bytes)
74}
75
76/// Generate a random 32-byte secret and return it as a base64 string.
77///
78/// This is a convenience function that generates a key and encodes it as a base64 string,
79/// useful for storage, display, or transmission. The base64 string will be 32 characters long.
80/// This function ensures the returned key does not contain any dashes
81/// (to make it double-click-selectable in GUIs).
82///
83/// # Examples
84///
85/// ```
86/// use oboron::generate_secret;
87///
88/// let secret = generate_secret();
89/// assert_eq!(secret.len(), 43);
90/// ```
91#[must_use]
92pub fn generate_secret() -> String {
93 loop {
94 let mut key_bytes = [0u8; 32];
95 rand::thread_rng().fill_bytes(&mut key_bytes);
96 let key_base64 = BASE64URL_NOPAD.encode(&key_bytes);
97 if !key_base64.contains('-') && !key_base64.contains('_') {
98 return key_base64;
99 }
100 }
101}
102
103/// Generate a random 32-byte secret suitable for use with ZSecret.
104///
105/// This function generates a random secret using a cryptographically secure random number generator.
106/// The key can be used directly with `ZSecret::from_bytes()`.
107///
108/// # Examples
109///
110/// ```
111/// use oboron::generate_secret_bytes;
112///
113/// let secret_bytes = generate_secret_bytes();
114/// assert_eq!(secret_bytes.len(), 32);
115/// ```
116#[must_use]
117#[cfg(feature = "bytes-keys")]
118pub fn generate_secret_bytes() -> [u8; 32] {
119 let decoded = BASE64URL_NOPAD
120 .decode(generate_secret().as_bytes())
121 .expect("Failed to decode base64");
122 decoded.try_into().expect("Decoded key is not 32 bytes")
123}
124
125/// Generate a cryptographically secure random 32-byte key and return it as a hex string.
126///
127/// This is a convenience function that generates a secret and encodes it as a hexadecimal string,
128/// useful for storage, display, or transmission. The hex string will be 64 characters long.
129///
130/// # Examples
131///
132/// ```
133/// use oboron::generate_secret_hex;
134///
135/// let secret_hex = generate_secret_hex();
136/// assert_eq!(secret_hex.len(), 64); // 32 bytes * 2 hex chars per byte
137/// ```
138#[must_use]
139#[cfg(feature = "hex-keys")]
140pub fn generate_secret_hex() -> String {
141 let decoded = BASE64URL_NOPAD
142 .decode(generate_secret().as_bytes())
143 .expect("Failed to decode base64");
144 let secret_bytes: [u8; 32] = decoded.try_into().expect("Decoded secret is not 32 bytes");
145 hex::encode(&secret_bytes)
146}