diff --git a/Cargo.lock b/Cargo.lock index a0a3445..fd027f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -204,6 +204,15 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest", +] + [[package]] name = "blake2b_simd" version = "0.5.11" @@ -520,6 +529,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -1120,6 +1130,7 @@ dependencies = [ "async-compression", "async-trait", "base64 0.21.4", + "blake2", "chrono", "chrono_lc", "clap", @@ -1142,6 +1153,7 @@ dependencies = [ "num-traits", "once_cell", "os_str_bytes", + "paste", "path-clean", "pin-project", "rand", @@ -2236,6 +2248,12 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" version = "1.0.109" diff --git a/Cargo.toml b/Cargo.toml index 39addd9..8462f26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,12 +98,17 @@ serde_json = { version = "1.0", features = ["preserve_order"] } serde_yaml = "0.9" toml = { version = "0.8", features = ["preserve_order"] } +paste = "1.0.14" + base64 = "0.21.4" hex = "0.4.3" +digest = "0.10.7" + md-5 = "0.10.6" sha1 = "0.10.6" sha2 = "0.10.8" -digest = { version = "0.10.7", default-features = true } +blake2 = "0.10.6" + ### NET diff --git a/src/lune/builtins/serde/crypto.rs b/src/lune/builtins/serde/crypto.rs index 83f0c17..3c98a9d 100644 --- a/src/lune/builtins/serde/crypto.rs +++ b/src/lune/builtins/serde/crypto.rs @@ -40,15 +40,35 @@ macro_rules! impl_hash_algo { } } } + + impl Crypto { + $( + paste::item! { + pub fn [<$algo:lower>](content: Option) -> Self { + let constructed = Self { + algo: Arc::new(Mutex::new(CryptoAlgo::$algo(Box::new($Type::new())))), + }; + + match content { + Some(inner) => constructed.update(inner.to_string()).clone(), + None => constructed, + } + } + } + )* + } } } -// enum CryptoAlgo +// Macro call creates the CryptoAlgo enum and implementations for it +// It also adds a method corresponding to the enum in the `Crypto` struct impl_hash_algo! { Sha1 => sha1::Sha1, Sha256 => sha2::Sha256, Sha512 => sha2::Sha512, - Md5 => md5::Md5 + Md5 => md5::Md5, + Blake2s256 => blake2::Blake2s256, + Blake2b512 => blake2::Blake2b512 } #[derive(Clone)] @@ -101,62 +121,19 @@ impl FromLua<'_> for EncodingKind { } } -impl Crypto { - pub fn sha1(content: Option) -> Crypto { - let constructed = Self { - algo: Arc::new(Mutex::new(CryptoAlgo::Sha1(Box::new(sha1::Sha1::new())))), - }; +trait CryptoResult { + fn update(&self, content: impl AsRef<[u8]>) -> &Self; + fn digest(&self, encoding: EncodingKind) -> Result; +} - match content { - Some(inner) => constructed.update(inner.to_string()).clone(), - None => constructed, - } - } - - pub fn sha256(content: Option) -> Crypto { - let constructed = Self { - algo: Arc::new(Mutex::new(CryptoAlgo::Sha256( - Box::new(sha2::Sha256::new()), - ))), - }; - - match content { - Some(inner) => constructed.update(inner.to_string()).clone(), - None => constructed, - } - } - - pub fn sha512(content: Option) -> Crypto { - let constructed = Self { - algo: Arc::new(Mutex::new(CryptoAlgo::Sha512( - Box::new(sha2::Sha512::new()), - ))), - }; - - match content { - Some(inner) => constructed.update(inner.to_string()).clone(), - None => constructed, - } - } - - pub fn md5(content: Option) -> Crypto { - let constructed = Self { - algo: Arc::new(Mutex::new(CryptoAlgo::Md5(Box::new(md5::Md5::new())))), - }; - - match content { - Some(inner) => constructed.update(inner.to_string()).clone(), - None => constructed, - } - } - - pub fn update(&self, content: impl AsRef<[u8]>) -> &Crypto { +impl CryptoResult for Crypto { + fn update(&self, content: impl AsRef<[u8]>) -> &Crypto { (self.algo.lock().unwrap()).update(content); self } - pub fn digest(&self, encoding: EncodingKind) -> Result { + fn digest(&self, encoding: EncodingKind) -> Result { (*self.algo.lock().unwrap()).digest(encoding) } } diff --git a/src/lune/builtins/serde/mod.rs b/src/lune/builtins/serde/mod.rs index ab77d70..2c3b924 100644 --- a/src/lune/builtins/serde/mod.rs +++ b/src/lune/builtins/serde/mod.rs @@ -29,6 +29,12 @@ pub fn create(lua: &'static Lua) -> LuaResult { Ok(Crypto::sha512(content)) })? .with_function("md5", |_, content: Option| Ok(Crypto::md5(content)))? + .with_function("blake2s256", |_, content: Option| { + Ok(Crypto::blake2s256(content)) + })? + .with_function("blake2b512", |_, content: Option| { + Ok(Crypto::blake2b512(content)) + })? .build()?, )? .build_readonly()