fix(postgres): update usage of Digest

This commit is contained in:
Ryan Leckey 2020-06-13 08:54:52 -07:00
parent e2ac01e190
commit c2ce35fc06
2 changed files with 19 additions and 19 deletions

View File

@ -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::<Sha256>::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::<Sha256>::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<u8> = client_key
@ -136,13 +136,13 @@ pub(crate) async fn authenticate(
// ServerKey := HMAC(SaltedPassword, "Server Key")
let mut mac = Hmac::<Sha256>::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::<Sha256>::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::<Sha256>::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::<Sha256>::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();
}

View File

@ -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);
}