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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Provides a variety of common hash functions
use Digest;
pub use Error;
pub use BcryptError;
pub use Version;
/// Computes the MD5 hash of the input byte slice
///
/// # Parameters
///
/// * `data` - A reference to the byte slice to compute the MD5 hash for
///
/// # Returns
///
/// An MD5 digest of the input data
///
/// # Examples
///
/// ```rust
/// let input = "hello world";
/// let hash = md5(input);
/// ```
///
/// # Notes
///
/// This function uses the `compute` method from the `md5` crate to calculate the MD5 hash
/// Compute the BLAKE3 hash of input data
///
/// 计算输入数据的 BLAKE3 哈希值
///
/// BLAKE3 is a modern cryptographic hash function that provides high performance and security
///
/// BLAKE3 是一个现代的加密哈希函数,提供高性能和安全性
///
/// # Parameters / 参数
///
/// * `data` - Input byte slice reference to be hashed / 要计算哈希的输入字节切片引用
///
/// # Returns / 返回值
///
/// Returns the hexadecimal string representation of the BLAKE3 hash
///
/// 返回 BLAKE3 哈希的十六进制字符串表示
///
/// # Examples / 示例
///
/// ```rust
/// let input = b"hello world";
/// let hash = blake3(input);
/// println!("BLAKE3 hash: {}", hash);
/// ```
///
/// # Notes / 注意
///
/// - BLAKE3 is a fast and secure hash algorithm / BLAKE3 是一个快速且安全的哈希算法
/// - The output is a fixed-length 256-bit hash (64-character hexadecimal string) / 输出是定长的 256 位(64个字符的十六进制字符串)
/// - Always produces the same hash for the same input / 对于相同的输入,总是产生相同的哈希值
/// Generates a bcrypt hash for the given password
///
/// # Parameters
///
/// * `password` - A byte slice containing the password to be hashed
///
/// # Returns
///
/// A `Result` containing the bcrypt hashed password string or a `BcryptError`
///
/// # Examples
///
/// ```rust
/// let password = "my_secure_password";
/// match bcrypt(password) {
/// Ok(hash) => println!("Hashed password: {}", hash),
/// Err(e) => eprintln!("Hashing failed: {}", e),
/// }
/// ```
///
/// # Notes
///
/// Uses the default bcrypt cost factor for password hashing
/// This function generates a bcrypt hash using a custom cost and version
///
/// 使用自定义成本和版本生成 bcrypt 哈希
///
/// # Parameters / 参数
///
/// * `password` - The password to hash, as a byte slice / 要哈希的密码,作为字节切片
/// * `cost` - The cost parameter for hashing, indicating the computational complexity / 哈希成本参数,表示计算复杂度
/// * `version` - The bcrypt version used to format the hash / bcrypt 版本,用于格式化哈希
///
/// # Returns / 返回值
///
/// Returns a `Result` containing the bcrypt hash as a string if successful, or a `BcryptError` if an error occurs.
///
/// 返回一个包含 bcrypt 哈希的 `Result`,如果成功则包含哈希字符串,否则包含 `BcryptError`
///
/// # Example / 示例
///
/// ```
/// let password = "my_password";
/// let cost = 12;
/// let version = bcrypt::Version::TwoB;
/// let hash = bcrypt_custom(password, cost, version).unwrap();
/// println!("bcrypt hash: {}", hash);
/// ```
/// Verifies a password against a bcrypt hash
///
/// # Parameters
///
/// * `password` - A byte slice containing the password to verify
/// * `hash` - The bcrypt hash string to compare against
///
/// # Returns
///
/// A `Result` containing a boolean indicating whether the password matches the hash,
/// or a `BcryptError` if verification fails
///
/// # Examples
///
/// ```rust
/// let password = "my_password";
/// let hash = bcrypt(password).unwrap();
/// match verify_bcrypt(password, &hash) {
/// Ok(true) => println!("Password verified successfully"),
/// Ok(false) => println!("Password verification failed"),
/// Err(e) => eprintln!("Verification error: {}", e),
/// }
/// ```
/// for password
/// for password
/// Hashes a password using the Argon2 algorithm (recommended for password hashing).
///
/// This function uses the OWASP5 configuration for Argon2, which is considered
/// secure for password hashing purposes.
///
/// # Parameters
///
/// * `password` - A byte slice containing the password to be hashed.
/// * `salt` - A byte slice containing the salt to be used in the hashing process.
///
/// # Returns
///
/// * `Result<String, argon2::Error>` - A Result containing either:
/// - `Ok(String)`: The hashed password as a String if successful.
/// - `Err(argon2::Error)`: An error if the hashing process fails.
///
/// # Example
///
/// ```
/// let password = "my_secure_password";
/// let salt = "random_salt";
/// let hashed_password = argon2(password, salt)?;
/// ```
/// Verifies a password against an Argon2 encoded hash
///
/// # Parameters
///
/// * `hash` - The Argon2 encoded hash string
/// * `password` - A byte slice containing the password to verify
///
/// # Returns
///
/// A `Result` containing a boolean indicating whether the password matches the hash,
/// or an `Argon2Error` if verification fails
///
/// # Examples
///
/// ```rust
/// let password = "my_secure_password";
/// let hash = "some_argon2_encoded_hash";
/// match verify_argon2(hash, password) {
/// Ok(true) => println!("Password verified successfully"),
/// Ok(false) => println!("Password verification failed"),
/// Err(e) => eprintln!("Verification error: {}", e),
/// }
/// ```
///
/// # Notes
///
/// Uses Argon2's `verify_encoded` method for password verification