postgres_protocol_sm3/authentication/
mod.rs

1//! Authentication protocol support.
2use md5::{Digest, Md5};
3use sm3::Sm3;
4
5pub mod sasl;
6
7/// Hashes authentication information in a way suitable for use in response
8/// to an `AuthenticationMd5Password` message.
9///
10/// The resulting string should be sent back to the database in a
11/// `PasswordMessage` message.
12#[inline]
13pub fn md5_hash(username: &[u8], password: &[u8], salt: [u8; 4]) -> String {
14    let mut md5 = Md5::new();
15    md5.update(password);
16    md5.update(username);
17    let output = md5.finalize_reset();
18    md5.update(format!("{:x}", output));
19    md5.update(salt);
20    format!("md5{:x}", md5.finalize())
21}
22
23/// Hashes authentication information in a way suitable for use in response
24/// to an `AuthenticationSm3Password` message.
25///
26/// The resulting string should be sent back to the database in a
27/// `PasswordMessage` message.
28#[inline]
29pub fn sm3_hash(username: &[u8], password: &[u8], salt: [u8; 4]) -> String {
30    let mut sm3 = Sm3::new();
31    sm3.update(password);
32    sm3.update(username);
33    let output = sm3.finalize_reset();
34    sm3.update(format!("{:x}", output));
35    sm3.update(salt);
36    format!("sm3{:x}", sm3.finalize())
37}
38
39#[cfg(test)]
40mod test {
41    use super::*;
42
43    #[test]
44    fn md5() {
45        let username = b"md5_user";
46        let password = b"password";
47        let salt = [0x2a, 0x3d, 0x8f, 0xe0];
48
49        assert_eq!(
50            md5_hash(username, password, salt),
51            "md562af4dd09bbb41884907a838a3233294"
52        );
53    }
54
55    #[test]
56    fn sm3() {
57        let username = b"sm3_user";
58        let password = b"password";
59        let salt = [0x2a, 0x3d, 0x8f, 0xe0];
60
61        assert_eq!(
62            sm3_hash(username, password, salt),
63            "sm360f647fba99d2a5375a6dc448fd58604c80f67757939595e2601e4885de8dd3b"
64        );
65    }
66}