| 1 | //! Authentication protocol support. |
| 2 | use craterust-analyzer cargo postgres-protocol 0.6.12 crate/::hexrust-analyzer cargo postgres-protocol 0.6.12 hex/::LowerHexWrapperrust-analyzer cargo postgres-protocol 0.6.12 hex/LowerHexWrapper#; |
| 3 | use md5rust-analyzer cargo md-5 0.11.0 crate/::{Digestrust-analyzer cargo digest 0.11.3 digest/Digest#, Md5rust-analyzer cargo md-5 0.11.0 Md5#}; |
| 4 | |
| 5 | pub mod saslrust-analyzer cargo postgres-protocol 0.6.12 authentication/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] |
| 13 | pub fn md5_hashrust-analyzer cargo postgres-protocol 0.6.12 authentication/md5_hash().(username: &[u8], password: &[u8], salt: [u8; 4]) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 14 | let mut md5 = Md5rust-analyzer cargo md-5 0.11.0 Md5#::newrust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]new().(); |
| 15 | md5.updaterust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]update().(password); |
| 16 | md5.updaterust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]update().(username); |
| 17 | let output = LowerHexWrapperrust-analyzer cargo postgres-protocol 0.6.12 hex/LowerHexWrapper#(md5.finalize_resetrust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]finalize_reset().()); |
| 18 | md5.updaterust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]update().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{output:x}")); |
| 19 | md5.updaterust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]update().(salt); |
| 20 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("md5{:x}", LowerHexWrapperrust-analyzer cargo postgres-protocol 0.6.12 hex/LowerHexWrapper#(md5.finalizerust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]finalize().())) |
| 21 | } |
| 22 | |
| 23 | #[cfg(test)] |
| 24 | mod testrust-analyzer cargo postgres-protocol 0.6.12 authentication/test/ { |
| 25 | use superrust-analyzer cargo postgres-protocol 0.6.12 authentication/::*; |
| 26 | |
| 27 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 28 | fn md5rust-analyzer cargo postgres-protocol 0.6.12 authentication/test/md5().() { |
| 29 | let username = b"md5_user"; |
| 30 | let password = b"password"; |
| 31 | let salt = [0x2a, 0x3d, 0x8f, 0xe0]; |
| 32 | |
| 33 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 34 | md5_hashrust-analyzer cargo postgres-protocol 0.6.12 authentication/md5_hash().(username, password, salt), |
| 35 | "md562af4dd09bbb41884907a838a3233294" |
| 36 | ); |
| 37 | } |
| 38 | } |