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
use OsRng;
use SaltString;
use ;
use ;
use io;
/// Hashes a stream of data using SHA-256.
///
/// This function reads data from the provided reader in chunks and computes
/// a SHA-256 hash of the entire stream. It's memory-efficient as it doesn't
/// load the whole stream into memory.
///
/// # Arguments
///
/// * `reader` - A mutable reference to a type implementing `Read`, such as a `File` or `Cursor`.
///
/// # Returns
///
/// Returns a `Result` containing:
/// - `Ok(Vec<u8>)`: The 32-byte SHA-256 hash of the stream.
/// - `Err(std::io::Error)`: An I/O error if reading from the stream fails.
/// Hashes a byte slice using SHA-256.
/// Hashes a password with Argon2id.
///
/// This function takes a password as a string slice and hashes it
/// using the Argon2 algorithm, which is considered one of the most secure for password storage.
/// A random salt is generated for each hash to enhance security and ensure
/// that even two identical passwords will have different hashes.
///
/// # Arguments
///
/// * `password` - A reference to a string slice representing the password to be hashed.
///
/// # Returns
///
/// Returns a `Result` containing:
/// - `Ok(String)`: The hashed password encoded as a string on success.
/// - `Err(String)`: An error message detailing the reason for failure if hashing fails.
///
/// # Example
///
/// ```rust
/// use ironcrypt::hash_password;
///
/// let password = "MySecureP@ssw0rd";
/// match hash_password(password) {
/// Ok(hashed) => println!("Hashed password: {}", hashed),
/// Err(e) => println!("Error: {}", e),
/// }
/// ```
///
/// # Remarks
///
/// - The salt is automatically generated using `SaltString::generate` and is incorporated
/// into the final hash, making it ready for future verification.
/// - Use this function to securely store passwords in your database
/// by using the resulting hash instead of the plaintext password.
///
/// # Errors
///
/// The function may return an `Err` if:
/// - The salt generation or the hashing process fails.
/// - An internal error occurs during the call to `hash_password` from the Argon2 library.