1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! # genrs Library
//!
//! A versatile key and UUID generation library that allows you to:
//!
//! - Generate secure random keys of arbitrary length
//! - Encode keys in either hexadecimal (`Hex`) or Base64 (`Base64`) format
//! - Generate UUIDs of any version (V1, V3, V4, V5)
//!
//! ## Example usage
//!
//! ```
//! use genrs_lib::{encode_key, generate_key, generate_uuid, EncodingFormat, UuidVersion};
//!
//! // Generate a random key
//! let key = generate_key(32);
//! let encoded_key = encode_key(key, EncodingFormat::Base64).unwrap();
//! println!("Generated and encoded key: {}", encoded_key);
//!
//! // Generate a UUID V4
//! let uuid_v4 = generate_uuid(UuidVersion::V4, None, None).unwrap();
//! println!("Generated UUID V4: {}", uuid_v4);
//! ```
//!
//! ## Features
//!
//! - **Key Generation**: Uses a cryptographically secure random number generator (CSPRNG) to generate random keys of arbitrary length.
//! - **Key Encoding**: Supports `Hex` and `Base64` encoding formats for ease of transmission and storage.
//! - **UUID Generation**: Create universally unique identifiers (UUIDs) for V1 (timestamp-based), V3 (namespace + name, MD5), V4 (random), and V5 (namespace + name, SHA-1).
//!
//! ### Referenced Libraries
//!
//! - [`rand`](https://docs.rs/rand/0.8.4/rand/) for secure random number generation.
//! - [`uuid`](https://docs.rs/uuid/0.8.2/uuid/) for UUID generation.
//! - [`hex`](https://docs.rs/hex/0.4.2/hex/) for encoding keys in hexadecimal format.
//! - [`base64`](https://docs.rs/base64/0.13.0/base64/) for encoding keys in Base64 format.
use Engine;
use ;
use ;
/// Enum to represent the encoding format for the key.
///
/// # Examples
///
/// ```
/// use genrs_lib::{encode_key, generate_key, EncodingFormat};
///
/// let key = generate_key(32);
/// let encoded_key = encode_key(key, EncodingFormat::Base64).unwrap();
/// println!("Generated and encoded key: {}", encoded_key);
/// ```
///
/// Refer to the `encode_key` function for encoding usage.
/// Generates a random key of the given length in bytes.
///
/// # Examples
///
/// ```
/// use genrs_lib::generate_key;
///
/// let key = generate_key(16);
/// assert_eq!(key.len(), 16);
/// ```
///
/// This function fills a vector of the specified length with secure random bytes
/// using the system's entropy source.
///
/// # Panics
///
/// Will panic if the system's entropy source is unavailable.
///
/// Refer to the `encode_key` function for encoding the generated key.
/// Encodes the given key into the specified format (`Hex` or `Base64`).
///
/// # Examples
///
/// ```
/// use genrs_lib::{encode_key, generate_key, EncodingFormat};
///
/// let key = generate_key(16);
/// let encoded_key = encode_key(key, EncodingFormat::Hex).unwrap();
/// println!("Hex encoded key: {}", encoded_key);
/// ```
///
/// # Errors
///
/// Returns an error if the format is unsupported. However, this should never happen,
/// as the format is now restricted to the `EncodingFormat` enum.
/// Enum to represent UUID versions.
///
/// # Examples
///
/// ```
/// use genrs_lib::{generate_uuid, UuidVersion};
///
/// let uuid_v4 = generate_uuid(UuidVersion::V4, None, None).unwrap();
/// println!("Generated UUID V4: {}", uuid_v4);
/// ```
///
/// Refer to the `generate_uuid` function for usage.
/// Generates a UUID of the specified version.
///
/// - **UUID V1**: Generates a UUID based on the current system time and a random node ID.
/// - **UUID V3 and V5**: Require a namespace and name for generating a UUID based on the MD5 or SHA-1 hash.
/// - **UUID V4**: Generates a purely random UUID.
///
/// # Examples
///
/// ```
/// use uuid::Uuid;
/// use genrs_lib::{generate_uuid, UuidVersion};
///
/// let uuid_v1 = generate_uuid(UuidVersion::V1, None, None).unwrap();
/// println!("Generated UUID V1: {}", uuid_v1);
///
/// let namespace = Uuid::new_v4();
/// let uuid_v3 = generate_uuid(UuidVersion::V3, Some(namespace), Some("example")).unwrap();
/// println!("Generated UUID V3: {}", uuid_v3);
/// ```
///
/// # Errors
///
/// Returns an error if the required parameters (namespace, name) for UUID V3 or V5 are missing.