doe/crypto.rs
1/// AES
2/// ```ignore
3///let data = "this is data".as_bytes();
4///let enc_data = doe::crypto::aes::aes_ecb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
5///let dec_data = doe::crypto::aes::aes_ecb_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
6///println!("data:{:?}",&data);
7///println!("enc_data:{:?}",&enc_data);
8///println!("dec_data:{:?}",&dec_data);
9/// ```
10#[allow(warnings)]
11#[cfg(feature = "crypto")]
12pub mod crypto {
13 /// AES
14 /// ```ignore
15 ///let data = "this is data".as_bytes();
16 ///let enc_data = doe::crypto::aes::aes_ecb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
17 ///let dec_data = doe::crypto::aes::aes_ecb_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
18 ///println!("data:{:?}",&data);
19 ///println!("enc_data:{:?}",&enc_data);
20 ///println!("dec_data:{:?}",&dec_data);
21 /// ```
22 ///
23 pub mod aes {
24 /// AES ECB Encrypt
25 ///
26 /// key must be 32 bytes
27 ///
28 /// iv must be 16 bytes
29 /// ```ignore
30 ///let data = "this is data".as_bytes();
31 ///let enc_data = doe::crypto::aes::aes_ecb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
32 /// ```
33 pub fn aes_ecb_encrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
34 use aes::Aes256;
35 use block_modes::block_padding::Pkcs7;
36 use block_modes::{BlockMode, Ecb};
37
38 type Aes256Ecb = Ecb<Aes256, Pkcs7>;
39
40 let cipher = Aes256Ecb::new_from_slices(key, iv)
41 .map_err(|e| format!("Failed to create ECB cipher: {}", e))?;
42
43 Ok(cipher.encrypt_vec(data))
44 }
45
46 /// AES ECB Decrypt
47 ///
48 /// key must be 32 bytes
49 ///
50 /// iv must be 16 bytes
51 /// ```ignore
52 ///let data = "this is data".as_bytes();
53 ///let enc_data = doe::crypto::aes::aes_ecb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
54 ///let dec_data = doe::crypto::aes::aes_ecb_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
55 /// ```
56 pub fn aes_ecb_decrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
57 use aes::Aes256;
58 use block_modes::block_padding::Pkcs7;
59 use block_modes::{BlockMode, Ecb};
60
61 type Aes256Ecb = Ecb<Aes256, Pkcs7>;
62
63 let cipher = Aes256Ecb::new_from_slices(key, iv)
64 .map_err(|e| format!("Failed to create ECB cipher: {}", e))?;
65
66 cipher.decrypt_vec(data)
67 .map_err(|e| format!("AES ECB decryption failed: {}", e))
68 }
69
70 /// AES CBC Encrypt
71 ///
72 /// key must be 32 bytes
73 ///
74 /// iv must be 16 bytes
75 /// ```ignore
76 ///let data = "this is data".as_bytes();
77 ///let enc_data = doe::crypto::aes::aes_cbc_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
78 /// ```
79 pub fn aes_cbc_encrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
80 use aes::Aes256;
81 use block_modes::block_padding::Pkcs7;
82 use block_modes::{BlockMode, Cbc};
83
84 type Aes256Cbc = Cbc<Aes256, Pkcs7>;
85
86 let cipher = Aes256Cbc::new_from_slices(key, iv)
87 .map_err(|e| format!("Failed to create CBC cipher: {}", e))?;
88
89 Ok(cipher.encrypt_vec(data))
90 }
91
92 /// AES CBC Decrypt
93 ///
94 /// key must be 32 bytes
95 ///
96 /// iv must be 16 bytes
97 /// ```ignore
98 ///let data = "this is data".as_bytes();
99 ///let enc_data = doe::crypto::aes::aes_cbc_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
100 ///let dec_data = doe::crypto::aes::aes_cbc_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
101 /// ```
102 pub fn aes_cbc_decrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
103 use aes::Aes256;
104 use block_modes::block_padding::Pkcs7;
105 use block_modes::{BlockMode, Cbc};
106
107 type Aes256Cbc = Cbc<Aes256, Pkcs7>;
108
109 let cipher = Aes256Cbc::new_from_slices(key, iv)
110 .map_err(|e| format!("Failed to create CBC cipher: {}", e))?;
111
112 cipher.decrypt_vec(data)
113 .map_err(|e| format!("AES CBC decryption failed: {}", e))
114 }
115
116 /// AES CFB Encrypt
117 ///
118 /// key must be 32 bytes
119 ///
120 /// iv must be 16 bytes
121 /// ```ignore
122 ///let data = "this is data".as_bytes();
123 ///let enc_data = doe::crypto::aes::aes_cfb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
124 /// ```
125 pub fn aes_cfb_encrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
126 use aes::Aes256;
127 use block_modes::block_padding::Pkcs7;
128 use block_modes::{BlockMode, Cfb};
129
130 type Aes256Cfb = Cfb<Aes256, Pkcs7>;
131
132 let cipher = Aes256Cfb::new_from_slices(key, iv)
133 .map_err(|e| format!("Failed to create CFB cipher: {}", e))?;
134
135 Ok(cipher.encrypt_vec(data))
136 }
137
138 /// AES CFB Decrypt
139 ///
140 /// key must be 32 bytes
141 ///
142 /// iv must be 16 bytes
143 /// ```ignore
144 ///let data = "this is data".as_bytes();
145 ///let enc_data = doe::crypto::aes::aes_cfb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
146 ///let dec_data = doe::crypto::aes::aes_cfb_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
147 /// ```
148 pub fn aes_cfb_decrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
149 use aes::Aes256;
150 use block_modes::block_padding::Pkcs7;
151 use block_modes::{BlockMode, Cfb};
152
153 type Aes256Cfb = Cfb<Aes256, Pkcs7>;
154
155 let cipher = Aes256Cfb::new_from_slices(key, iv)
156 .map_err(|e| format!("Failed to create CFB cipher: {}", e))?;
157
158 cipher.decrypt_vec(data)
159 .map_err(|e| format!("AES CFB decryption failed: {}", e))
160 }
161
162 /// AES OFB Encrypt
163 ///
164 /// key must be 32 bytes
165 ///
166 /// iv must be 16 bytes
167 /// ```ignore
168 ///let data = "this is data".as_bytes();
169 ///let enc_data = doe::crypto::aes::aes_ofb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
170 /// ```
171 pub fn aes_ofb_encrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
172 use aes::Aes256;
173 use block_modes::block_padding::Pkcs7;
174 use block_modes::{BlockMode, Ofb};
175
176 type Aes256Ofb = Ofb<Aes256, Pkcs7>;
177
178 let cipher = Aes256Ofb::new_from_slices(key, iv)
179 .map_err(|e| format!("Failed to create OFB cipher: {}", e))?;
180
181 Ok(cipher.encrypt_vec(data))
182 }
183
184 /// AES OFB Decrypt
185 ///
186 /// key must be 32 bytes
187 ///
188 /// iv must be 16 bytes
189 /// ```ignore
190 ///let data = "this is data".as_bytes();
191 ///let enc_data = doe::crypto::aes::aes_ofb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
192 ///let dec_data = doe::crypto::aes::aes_ofb_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
193 /// ```
194 pub fn aes_ofb_decrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
195 use aes::Aes256;
196 use block_modes::block_padding::Pkcs7;
197 use block_modes::{BlockMode, Ofb};
198
199 type Aes256Ofb = Ofb<Aes256, Pkcs7>;
200
201 let cipher = Aes256Ofb::new_from_slices(key, iv)
202 .map_err(|e| format!("Failed to create OFB cipher: {}", e))?;
203
204 cipher.decrypt_vec(data)
205 .map_err(|e| format!("AES OFB decryption failed: {}", e))
206 }
207
208 /// AES IGE Encrypt
209 ///
210 /// key must be 32 bytes
211 ///
212 /// iv must be 32 bytes
213 ///
214 /// data must be 16*n bytes
215 /// ```ignore
216 ///let data = "this is data".as_bytes();
217 ///let enc_data = doe::crypto::aes::aes_ige_encrypt("12345678123456781234567812345678".as_bytes(),"12345678123456781234567812345678".as_bytes(),data).unwrap();
218 /// ```
219 pub fn aes_ige_encrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
220 use aes::cipher::generic_array::typenum::U16;
221 use aes::cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt};
222 use aes::Aes256;
223
224 if key.len() != 32 {
225 return Err("Key must be 32 bytes".to_string());
226 }
227 if iv.len() != 32 {
228 return Err("IV must be 32 bytes".to_string());
229 }
230 if data.len() % 16 != 0 {
231 return Err("Data must be a multiple of 16 bytes".to_string());
232 }
233 use aes::NewBlockCipher;
234 let cipher = Aes256::new(GenericArray::from_slice(key));
235 let mut prev_block: GenericArray<u8, U16> =
236 GenericArray::from_slice(&iv[..16]).to_owned();
237 let mut prev_prev_block: GenericArray<u8, U16> =
238 GenericArray::from_slice(&iv[16..]).to_owned();
239 let mut ciphertext = Vec::with_capacity(data.len());
240
241 for chunk in data.chunks(16) {
242 let mut block = GenericArray::clone_from_slice(chunk);
243 for i in 0..16 {
244 block[i] ^= prev_block[i] ^ prev_prev_block[i];
245 }
246 cipher.encrypt_block(&mut block);
247 for i in 0..16 {
248 block[i] ^= prev_block[i];
249 }
250 ciphertext.extend_from_slice(&block);
251 prev_prev_block.copy_from_slice(&prev_block);
252 prev_block.copy_from_slice(&block);
253 }
254
255 Ok(ciphertext)
256 }
257
258 /// AES IGE Decrypt
259 ///
260 /// key must be 32 bytes
261 ///
262 /// iv must be 32 bytes
263 ///
264 /// data must be 16*n bytes
265 /// ```ignore
266 ///let data = "this is data".as_bytes();
267 ///let enc_data = doe::crypto::aes::aes_ige_encrypt("12345678123456781234567812345678".as_bytes(),"12345678123456781234567812345678".as_bytes(),data).unwrap();
268 ///let dec_data = doe::crypto::aes::aes_ige_decrypt("12345678123456781234567812345678".as_bytes(),"12345678123456781234567812345678".as_bytes(),&enc_data).unwrap();
269 /// ```
270 pub fn aes_ige_decrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
271 use aes::cipher::generic_array::typenum::U16;
272 use aes::cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt};
273 use aes::Aes256;
274 use aes::NewBlockCipher;
275 if key.len() != 32 {
276 return Err("Key must be 32 bytes".to_string());
277 }
278 if iv.len() != 32 {
279 return Err("IV must be 32 bytes".to_string());
280 }
281 if data.len() % 16 != 0 {
282 return Err("Data must be a multiple of 16 bytes".to_string());
283 }
284
285 let cipher = Aes256::new(GenericArray::from_slice(key));
286 let mut prev_block: GenericArray<u8, U16> =
287 GenericArray::from_slice(&iv[..16]).to_owned();
288 let mut prev_prev_block: GenericArray<u8, U16> =
289 GenericArray::from_slice(&iv[16..]).to_owned();
290 let mut plaintext = Vec::with_capacity(data.len());
291
292 for chunk in data.chunks(16) {
293 let mut block = GenericArray::clone_from_slice(chunk);
294 for i in 0..16 {
295 block[i] ^= prev_block[i];
296 }
297 cipher.decrypt_block(&mut block);
298 for i in 0..16 {
299 block[i] ^= prev_block[i] ^ prev_prev_block[i];
300 }
301 plaintext.extend_from_slice(&block);
302 prev_prev_block.copy_from_slice(&prev_block);
303 prev_block.copy_from_slice(chunk);
304 }
305
306 Ok(plaintext)
307 }
308
309 /// AES CTR Encrypt
310 ///
311 /// key must be 32 bytes
312 ///
313 /// iv must be 16 bytes
314 /// ```ignore
315 ///let data = "this is data".as_bytes();
316 ///let enc_data = doe::crypto::aes::aes_ctr_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
317 /// ```
318 pub fn aes_ctr_encrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
319 use aes::cipher::NewCipher;
320 use aes::cipher::StreamCipher;
321 use aes::cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt};
322 use aes::Aes256;
323 use ctr::Ctr128BE;
324
325 type Aes256Ctr = Ctr128BE<Aes256>;
326
327 if key.len() != 32 {
328 return Err("Key must be 32 bytes".to_string());
329 }
330 if iv.len() != 16 {
331 return Err("IV must be 16 bytes".to_string());
332 }
333
334 let mut cipher = Aes256Ctr::new(
335 GenericArray::from_slice(key),
336 GenericArray::from_slice(iv),
337 );
338 let mut ciphertext = data.to_vec();
339 cipher.apply_keystream(&mut ciphertext);
340
341 Ok(ciphertext)
342 }
343
344 /// AES CTR Decrypt
345 ///
346 /// key must be 32 bytes
347 ///
348 /// iv must be 16 bytes
349 /// ```ignore
350 ///let data = "this is data".as_bytes();
351 ///let enc_data = doe::crypto::aes::aes_ctr_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
352 ///let dec_data = doe::crypto::aes::aes_ctr_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
353 /// ```
354 pub fn aes_ctr_decrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, String> {
355 use aes::cipher::NewCipher;
356 use aes::cipher::StreamCipher;
357 use aes::cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt};
358 use aes::Aes256;
359 use ctr::Ctr128BE;
360
361 type Aes256Ctr = Ctr128BE<Aes256>;
362
363 if key.len() != 32 {
364 return Err("Key must be 32 bytes".to_string());
365 }
366 if iv.len() != 16 {
367 return Err("IV must be 16 bytes".to_string());
368 }
369
370 let mut cipher = Aes256Ctr::new(
371 GenericArray::from_slice(key),
372 GenericArray::from_slice(iv),
373 );
374 let mut ciphertext = data.to_vec();
375 cipher.apply_keystream(&mut ciphertext);
376
377 Ok(ciphertext)
378 }
379 }
380
381 ///RSA example
382 ///```ignore
383 ///let priv_key = doe::crypto::rsa::gen_priv_key(2048).unwrap();
384 ///let pub_key = doe::crypto::rsa::gen_pub_key(&priv_key);
385 ///let data = b"hello world";
386 ///let enc_data = doe::crypto::rsa::encrypt(&pub_key, data).unwrap();
387 ///let dec_data = doe::crypto::rsa::decrypt(&priv_key, &enc_data).unwrap();
388 ///assert_eq!(data, dec_data.as_slice());
389 /// ```
390 ///
391 pub mod rsa {
392 use rsa::pkcs8::DecodePrivateKey;
393 pub use rsa::*;
394
395 ///RSA example
396 ///```ignore
397 ///let priv_key = doe::crypto::rsa::gen_priv_key(2048).unwrap();
398 ///let pub_key = doe::crypto::rsa::gen_pub_key(&priv_key);
399 ///let data = b"hello world";
400 ///let enc_data = doe::crypto::rsa::encrypt(&pub_key, data).unwrap();
401 ///let dec_data = doe::crypto::rsa::decrypt(&priv_key, &enc_data).unwrap();
402 ///assert_eq!(data, dec_data.as_slice());
403 /// ```
404 ///
405 pub fn gen_priv_key(bits: usize) -> std::result::Result<RsaPrivateKey, String> {
406 let mut rng = rand::thread_rng();
407 RsaPrivateKey::new(&mut rng, bits)
408 .map_err(|e| format!("Failed to generate RSA private key: {}", e))
409 }
410
411 ///RSA example
412 ///```ignore
413 ///let pem = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----";
414 ///let priv_key = doe::crypto::rsa::gen_priv_key_from_pkcs8_pem(pem).unwrap();
415 ///let pub_key = doe::crypto::rsa::gen_pub_key(&priv_key);
416 ///let data = b"hello world";
417 ///let enc_data = doe::crypto::rsa::encrypt(&pub_key, data).unwrap();
418 ///let dec_data = doe::crypto::rsa::decrypt(&priv_key, &enc_data).unwrap();
419 ///assert_eq!(data, dec_data.as_slice());
420 /// ```
421 pub fn gen_priv_key_from_pkcs8_pem(s: &str) -> std::result::Result<RsaPrivateKey, String> {
422 RsaPrivateKey::from_pkcs8_pem(s)
423 .map_err(|e| format!("Failed to parse RSA private key from PEM: {}", e))
424 }
425
426 ///RSA example
427 ///```ignore
428 ///let priv_key = doe::crypto::rsa::gen_priv_key(2048).unwrap();
429 ///let pub_key = doe::crypto::rsa::gen_pub_key(&priv_key);
430 ///let data = b"hello world";
431 ///let enc_data = doe::crypto::rsa::encrypt(&pub_key, data).unwrap();
432 ///let dec_data = doe::crypto::rsa::decrypt(&priv_key, &enc_data).unwrap();
433 ///assert_eq!(data, dec_data.as_slice());
434 /// ```
435 ///
436 pub fn gen_pub_key(priv_key: &RsaPrivateKey) -> RsaPublicKey {
437 RsaPublicKey::from(priv_key)
438 }
439
440 ///RSA example
441 ///```ignore
442 ///let priv_key = doe::crypto::rsa::gen_priv_key(2048).unwrap();
443 ///let pub_key = doe::crypto::rsa::gen_pub_key(&priv_key);
444 ///let data = b"hello world";
445 ///let enc_data = doe::crypto::rsa::encrypt(&pub_key, data).unwrap();
446 ///let dec_data = doe::crypto::rsa::decrypt(&priv_key, &enc_data).unwrap();
447 ///assert_eq!(data, dec_data.as_slice());
448 /// ```
449 ///
450 pub fn encrypt(pub_key: &RsaPublicKey, data: &[u8]) -> std::result::Result<Vec<u8>, String> {
451 let mut rng = rand::thread_rng();
452 pub_key
453 .encrypt(&mut rng, Pkcs1v15Encrypt, data)
454 .map_err(|e| format!("RSA encryption failed: {}", e))
455 }
456
457 ///RSA example
458 ///```ignore
459 ///let priv_key = doe::crypto::rsa::gen_priv_key(2048).unwrap();
460 ///let pub_key = doe::crypto::rsa::gen_pub_key(&priv_key);
461 ///let data = b"hello world";
462 ///let enc_data = doe::crypto::rsa::encrypt(&pub_key, data).unwrap();
463 ///let dec_data = doe::crypto::rsa::decrypt(&priv_key, &enc_data).unwrap();
464 ///assert_eq!(data, dec_data.as_slice());
465 /// ```
466 ///
467 pub fn decrypt(priv_key: &RsaPrivateKey, enc_data: &[u8]) -> std::result::Result<Vec<u8>, String> {
468 priv_key
469 .decrypt(Pkcs1v15Encrypt, enc_data)
470 .map_err(|e| format!("RSA decryption failed: {}", e))
471 }
472 }
473
474 pub mod sha1 {
475 /// not secure
476 /// ```ignore
477 /// let hash = doe::crypto::sha1::sha1(b"hello world");
478 /// println!("SHA1 hash: {}", hash);
479 /// ```
480 pub fn sha1(data: &[u8]) -> String {
481 use sha1::{Digest, Sha1};
482 let mut hasher = Sha1::new();
483 hasher.update(data);
484 let result = hasher.finalize();
485 hex::encode(result)
486 }
487 }
488
489 pub mod sha2 {
490 /// ```ignore
491 /// let hash = doe::crypto::sha2::sha_224(b"hello world");
492 /// println!("SHA224 hash: {}", hash);
493 /// ```
494 pub fn sha_224(data: &[u8]) -> String {
495 use sha2::{Digest, Sha224};
496 let mut hasher = Sha224::new();
497 hasher.update(data);
498 let result = hasher.finalize();
499 hex::encode(result)
500 }
501
502 /// ```ignore
503 /// let hash = doe::crypto::sha2::sha_256(b"hello world");
504 /// println!("SHA256 hash: {}", hash);
505 /// ```
506 fn sha_256(data: &[u8]) -> String {
507 use sha2::{Digest, Sha256};
508 let mut hasher = Sha256::new();
509 hasher.update(data);
510 let result = hasher.finalize();
511 hex::encode(result)
512 }
513
514 /// ```ignore
515 /// let hash = doe::crypto::sha2::sha_512_224(b"hello world");
516 /// println!("SHA512/224 hash: {}", hash);
517 /// ```
518 fn sha_512_224(data: &[u8]) -> String {
519 use sha2::{Digest, Sha512_224};
520 let mut hasher = Sha512_224::new();
521 hasher.update(data);
522 let result = hasher.finalize();
523 hex::encode(result)
524 }
525
526 /// ```ignore
527 /// let hash = doe::crypto::sha2::sha_512_256(b"hello world");
528 /// println!("SHA512/256 hash: {}", hash);
529 /// ```
530 fn sha_512_256(data: &[u8]) -> String {
531 use sha2::{Digest, Sha512_256};
532 let mut hasher = Sha512_256::new();
533 hasher.update(data);
534 let result = hasher.finalize();
535 hex::encode(result)
536 }
537
538 /// ```ignore
539 /// let hash = doe::crypto::sha2::sha_384(b"hello world");
540 /// println!("SHA384 hash: {}", hash);
541 /// ```
542 fn sha_384(data: &[u8]) -> String {
543 use sha2::{Digest, Sha384};
544 let mut hasher = Sha384::new();
545 hasher.update(data);
546 let result = hasher.finalize();
547 hex::encode(result)
548 }
549
550 /// ```ignore
551 /// let hash = doe::crypto::sha2::sha_512(b"hello world");
552 /// println!("SHA512 hash: {}", hash);
553 /// ```
554 pub fn sha_512(data: &[u8]) -> String {
555 use sha2::{Digest, Sha512};
556 let mut hasher = Sha512::new();
557 hasher.update(data);
558 let result = hasher.finalize().to_vec();
559 hex::encode_upper(result)
560 }
561 }
562
563 pub mod sha3 {
564 /// ```ignore
565 /// let hash = doe::crypto::sha3::sha_224(b"hello world");
566 /// println!("SHA3-224 hash: {}", hash);
567 /// ```
568 pub fn sha_224(data: &[u8]) -> String {
569 use sha3::{Digest, Sha3_224};
570 let mut hasher = Sha3_224::new();
571 hasher.update(data);
572 let result = hasher.finalize().to_vec();
573 hex::encode_upper(result)
574 }
575
576 /// ```ignore
577 /// let hash = doe::crypto::sha3::sha_256(b"hello world");
578 /// println!("SHA3-256 hash: {}", hash);
579 /// ```
580 pub fn sha_256(data: &[u8]) -> String {
581 use sha3::{Digest, Sha3_256};
582 let mut hasher = Sha3_256::new();
583 hasher.update(data);
584 let result = hasher.finalize().to_vec();
585 hex::encode_upper(result)
586 }
587
588 /// ```ignore
589 /// let hash = doe::crypto::sha3::sha_384(b"hello world");
590 /// println!("SHA3-384 hash: {}", hash);
591 /// ```
592 pub fn sha_384(data: &[u8]) -> String {
593 use sha3::{Digest, Sha3_384};
594 let mut hasher = Sha3_384::new();
595 hasher.update(data);
596 let result = hasher.finalize().to_vec();
597 hex::encode_upper(result)
598 }
599
600 /// ```ignore
601 /// let hash = doe::crypto::sha3::sha_512(b"hello world");
602 /// println!("SHA3-512 hash: {}", hash);
603 /// ```
604 pub fn sha_512(data: &[u8]) -> String {
605 use sha3::{Digest, Sha3_512};
606 let mut hasher = Sha3_512::new();
607 hasher.update(data);
608 let result = hasher.finalize().to_vec();
609 hex::encode_upper(result)
610 }
611
612 /// ```ignore
613 /// let hash = doe::crypto::sha3::shake_128(b"hello world");
614 /// println!("SHAKE128 hash: {}", hash);
615 /// ```
616 pub fn shake_128(data: &[u8]) -> String {
617 use sha3::{
618 digest::{ExtendableOutput, Update, XofReader},
619 Shake128,
620 };
621 let mut hasher = Shake128::default();
622 hasher.update(data);
623 let mut reader = hasher.finalize_xof();
624 // 128 bytes of output
625 let mut result = [0u8; 128];
626 reader.read(&mut result);
627 hex::encode_upper(result)
628 }
629
630 /// ```ignore
631 /// let hash = doe::crypto::sha3::shake_256(b"hello world");
632 /// println!("SHAKE256 hash: {}", hash);
633 /// ```
634 pub fn shake_256(data: &[u8]) -> String {
635 use sha3::{
636 digest::{ExtendableOutput, Update, XofReader},
637 Shake256,
638 };
639 let mut hasher = Shake256::default();
640 hasher.update(data);
641 let mut reader = hasher.finalize_xof();
642 // 256 bytes of output
643 let mut result = [0u8; 256];
644 reader.read(&mut result);
645 hex::encode_upper(result)
646 }
647 }
648
649 /// ```ignore
650 /// let hash = doe::crypto::md5(b"hello world");
651 /// println!("MD5 hash: {}", hash);
652 /// ```
653 pub fn md5(data: &[u8]) -> String {
654 use md5::compute;
655 let result = compute(data);
656 result
657 .to_vec()
658 .iter()
659 .map(|x| format!("{:02x}", x))
660 .collect::<String>()
661 }
662
663 /// in defalut 32 bytes
664 /// ```ignore
665 /// let hash = doe::crypto::blake3(b"hello world");
666 /// println!("BLAKE3 hash: {}", hash);
667 /// ```
668 pub fn blake3(data: &[u8]) -> String {
669 let hasher = blake3::hash(data);
670 hex::encode_upper(hasher.as_bytes())
671 }
672
673 /// can set the output size
674 /// ```ignore
675 /// let hash = doe::crypto::blake3_xof(b"hello world", 64);
676 /// println!("BLAKE3 XOF hash: {}", hash);
677 /// ```
678 pub fn blake3_xof(data: &[u8], size: usize) -> String {
679 let hasher = blake3::Hasher::new();
680 let mut reader = hasher.finalize_xof();
681 let mut result = vec![0u8; size];
682 reader.fill(&mut result);
683 hex::encode_upper(result)
684 }
685}
686
687#[cfg(feature = "crypto")]
688pub use crypto::*;