From 3dda0889f29443e256b27e293f763ff700dc3c57 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 14 Oct 2023 23:16:27 -0700 Subject: [PATCH] refactor: refactor some more stuff and finalize, hash still inaccurate --- src/lune/builtins/serde/crypto.rs | 53 +++++++++++++++++++------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/lune/builtins/serde/crypto.rs b/src/lune/builtins/serde/crypto.rs index 3c4e6ed..9e63a30 100644 --- a/src/lune/builtins/serde/crypto.rs +++ b/src/lune/builtins/serde/crypto.rs @@ -5,14 +5,11 @@ use crate::lune::builtins::{ }; use anyhow::Result; use base64::{engine::general_purpose as Base64, Engine as _}; -use digest::DynDigest; use sha1::Digest as _; -// use ring::digest::{self, digest, Digest as RingDigest}; use std::sync::Mutex; // TODO: Proper error handling, remove unwraps -// Code compiles but trait object returns an incorrect hash! Love my life :3 #[derive(Clone)] pub struct Crypto { algo: Arc>, @@ -73,13 +70,39 @@ impl FromLua<'_> for EncodingKind { } impl CryptoAlgo { - pub fn get_hasher(&self) -> &dyn DynDigest { + pub fn update(&self, content: impl AsRef<[u8]>) { // TODO: Replace boilerplate using a macro match self { - CryptoAlgo::Sha1(hasher) => &**hasher, - CryptoAlgo::Sha256(hasher) => &**hasher, - CryptoAlgo::Sha512(hasher) => &**hasher, + CryptoAlgo::Sha1(hasher) => sha1::Digest::update(&mut (**hasher).clone(), content), + CryptoAlgo::Sha256(hasher) => sha2::Digest::update(&mut (**hasher).clone(), content), + CryptoAlgo::Sha512(hasher) => sha2::Digest::update(&mut (**hasher).clone(), content), + }; + } + + pub fn digest(&self, encoding: EncodingKind) -> Result { + let computed: Vec = match self { + CryptoAlgo::Sha1(hasher) => { + let hash = sha1::Digest::finalize((**hasher).clone()); + + hash.to_vec() + } + CryptoAlgo::Sha256(hasher) => { + let hash = sha2::Digest::finalize((**hasher).clone()); + + hash.to_vec() + } + CryptoAlgo::Sha512(hasher) => { + let hash = sha2::Digest::finalize((**hasher).clone()); + + hash.to_vec() + } + }; + + match encoding { + EncodingKind::Utf8 => String::from_utf8(computed).map_err(anyhow::Error::from), + EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)), + EncodingKind::Hex => Ok(hex::encode::<&[u8]>(&computed)), } } } @@ -123,25 +146,13 @@ impl Crypto { } pub fn update(&self, content: impl AsRef<[u8]>) -> &Crypto { - let mut binding = (*self.algo.lock().unwrap()).get_hasher().box_clone(); - let hasher = binding.as_mut(); - - hasher.update(content.as_ref()); + (*self.algo.lock().unwrap()).update(content); self } pub fn digest(&self, encoding: EncodingKind) -> Result { - let algo = self.algo.lock().unwrap(); - let hasher = algo.get_hasher(); - - let computed = &*(*hasher).box_clone().finalize_reset(); - - match encoding { - EncodingKind::Utf8 => String::from_utf8(computed.to_vec()).map_err(anyhow::Error::from), - EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)), - EncodingKind::Hex => Ok(hex::encode::<&[u8]>(computed)), - } + (*self.algo.lock().unwrap()).digest(encoding) } }