postgres-protocol: use RustCrypto md-5 crate

Swap the md5 crate for the md-5 crate.  Despite the latter's somewhat
more suspicious name, it is part of the wider RustCrypto ecosystem, and
shares code with the sha2 crate that postgres-protocol already uses.
This commit is contained in:
Nikhil Benesch 2021-01-08 13:35:45 -05:00
parent e29439a559
commit 7537e8a918
2 changed files with 9 additions and 10 deletions

View File

@ -14,7 +14,7 @@ byteorder = "1.0"
bytes = "1.0" bytes = "1.0"
fallible-iterator = "0.2" fallible-iterator = "0.2"
hmac = "0.10" hmac = "0.10"
md5 = "0.7" md-5 = "0.9"
memchr = "2.0" memchr = "2.0"
rand = "0.8" rand = "0.8"
sha2 = "0.9" sha2 = "0.9"

View File

@ -1,5 +1,5 @@
//! Authentication protocol support. //! Authentication protocol support.
use md5::Context; use md5::{Digest, Md5};
pub mod sasl; pub mod sasl;
@ -10,14 +10,13 @@ pub mod sasl;
/// `PasswordMessage` message. /// `PasswordMessage` message.
#[inline] #[inline]
pub fn md5_hash(username: &[u8], password: &[u8], salt: [u8; 4]) -> String { pub fn md5_hash(username: &[u8], password: &[u8], salt: [u8; 4]) -> String {
let mut context = Context::new(); let mut md5 = Md5::new();
context.consume(password); md5.update(password);
context.consume(username); md5.update(username);
let output = context.compute(); let output = md5.finalize_reset();
context = Context::new(); md5.update(format!("{:x}", output));
context.consume(format!("{:x}", output)); md5.update(&salt);
context.consume(&salt); format!("md5{:x}", md5.finalize())
format!("md5{:x}", context.compute())
} }
#[cfg(test)] #[cfg(test)]