postgres_protocol/authentication/
mod.rs

1//! Authentication protocol support.
2use md5::{Digest, Md5};
3
4pub mod sasl;
5
6/// Hashes authentication information in a way suitable for use in response
7/// to an `AuthenticationMd5Password` message.
8///
9/// The resulting string should be sent back to the database in a
10/// `PasswordMessage` message.
11#[inline]
12pub fn md5_hash(username: &[u8], password: &[u8], salt: [u8; 4]) -> String {
13    let mut md5 = Md5::new();
14    md5.update(password);
15    md5.update(username);
16    let output = md5.finalize_reset();
17    md5.update(format!("{:x}", output));
18    md5.update(salt);
19    format!("md5{:x}", md5.finalize())
20}
21
22#[cfg(test)]
23mod test {
24    use super::*;
25
26    #[test]
27    fn md5() {
28        let username = b"md5_user";
29        let password = b"password";
30        let salt = [0x2a, 0x3d, 0x8f, 0xe0];
31
32        assert_eq!(
33            md5_hash(username, password, salt),
34            "md562af4dd09bbb41884907a838a3233294"
35        );
36    }
37}