From c2ce35fc06d9cd424f66794add6f110bed1a5dff Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 13 Jun 2020 08:54:52 -0700 Subject: [PATCH] fix(postgres): update usage of Digest --- sqlx-core/src/postgres/connection/sasl.rs | 26 +++++++++++----------- sqlx-core/src/postgres/message/password.rs | 12 +++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/sqlx-core/src/postgres/connection/sasl.rs b/sqlx-core/src/postgres/connection/sasl.rs index b35e20e8..22b9ada8 100644 --- a/sqlx-core/src/postgres/connection/sasl.rs +++ b/sqlx-core/src/postgres/connection/sasl.rs @@ -4,7 +4,7 @@ use crate::postgres::message::{ Authentication, AuthenticationSasl, MessageFormat, SaslInitialResponse, SaslResponse, }; use crate::postgres::PgConnectOptions; -use hmac::{Hmac, Mac}; +use hmac::{Hmac, Mac, NewMac}; use rand::Rng; use sha2::digest::Digest; use sha2::Sha256; @@ -99,9 +99,9 @@ pub(crate) async fn authenticate( // ClientKey := HMAC(SaltedPassword, "Client Key") let mut mac = Hmac::::new_varkey(&salted_password).map_err(Error::protocol)?; - mac.input(b"Client Key"); + mac.update(b"Client Key"); - let client_key = mac.result().code(); + let client_key = mac.finalize().into_bytes(); // StoredKey := H(ClientKey) let stored_key = Sha256::digest(&client_key); @@ -123,9 +123,9 @@ pub(crate) async fn authenticate( // ClientSignature := HMAC(StoredKey, AuthMessage) let mut mac = Hmac::::new_varkey(&stored_key).map_err(Error::protocol)?; - mac.input(&auth_message.as_bytes()); + mac.update(&auth_message.as_bytes()); - let client_signature = mac.result().code(); + let client_signature = mac.finalize().into_bytes(); // ClientProof := ClientKey XOR ClientSignature let client_proof: Vec = client_key @@ -136,13 +136,13 @@ pub(crate) async fn authenticate( // ServerKey := HMAC(SaltedPassword, "Server Key") let mut mac = Hmac::::new_varkey(&salted_password).map_err(Error::protocol)?; - mac.input(b"Server Key"); + mac.update(b"Server Key"); - let server_key = mac.result().code(); + let server_key = mac.finalize().into_bytes(); // ServerSignature := HMAC(ServerKey, AuthMessage) let mut mac = Hmac::::new_varkey(&server_key).map_err(Error::protocol)?; - mac.input(&auth_message.as_bytes()); + mac.update(&auth_message.as_bytes()); // client-final-message = client-final-message-without-proof "," proof let client_final_message = format!( @@ -199,16 +199,16 @@ fn gen_nonce() -> String { fn hi<'a>(s: &'a str, salt: &'a [u8], iter_count: u32) -> Result<[u8; 32], Error> { let mut mac = Hmac::::new_varkey(s.as_bytes()).map_err(Error::protocol)?; - mac.input(&salt); - mac.input(&1u32.to_be_bytes()); + mac.update(&salt); + mac.update(&1u32.to_be_bytes()); - let mut u = mac.result().code(); + let mut u = mac.finalize().into_bytes(); let mut hi = u; for _ in 1..iter_count { let mut mac = Hmac::::new_varkey(s.as_bytes()).map_err(Error::protocol)?; - mac.input(u.as_slice()); - u = mac.result().code(); + mac.update(u.as_slice()); + u = mac.finalize().into_bytes(); hi = hi.iter().zip(u.iter()).map(|(&a, &b)| a ^ b).collect(); } diff --git a/sqlx-core/src/postgres/message/password.rs b/sqlx-core/src/postgres/message/password.rs index 32a57fba..8b0a8d66 100644 --- a/sqlx-core/src/postgres/message/password.rs +++ b/sqlx-core/src/postgres/message/password.rs @@ -49,19 +49,19 @@ impl Encode<'_> for Password<'_> { let mut hasher = Md5::new(); - hasher.input(password); - hasher.input(username); + hasher.update(password); + hasher.update(username); let mut output = String::with_capacity(35); - let _ = write!(output, "{:x}", hasher.result_reset()); + let _ = write!(output, "{:x}", hasher.finalize_reset()); - hasher.input(&output); - hasher.input(salt); + hasher.update(&output); + hasher.update(salt); output.clear(); - let _ = write!(output, "md5{:x}", hasher.result()); + let _ = write!(output, "md5{:x}", hasher.finalize()); buf.put_str_nul(&output); }