mirror of
https://github.com/lune-org/lune.git
synced 2025-04-04 10:30:54 +01:00
feat: impl Blake2 algo & reduce more boilerplate
* Expands the `impl_hash_algo!` macro to implement required methods in the Crypto struct for each CryptoAlgo * Adds Blake2 hashers (Blake2s256 & Blake2b512)
This commit is contained in:
parent
eedf6534fb
commit
8cafaed68e
4 changed files with 59 additions and 53 deletions
18
Cargo.lock
generated
18
Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -40,15 +40,35 @@ macro_rules! impl_hash_algo {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Crypto {
|
||||
$(
|
||||
paste::item! {
|
||||
pub fn [<$algo:lower>]<T: ToString>(content: Option<T>) -> 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<T: ToString>(content: Option<T>) -> 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<String>;
|
||||
}
|
||||
|
||||
match content {
|
||||
Some(inner) => constructed.update(inner.to_string()).clone(),
|
||||
None => constructed,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sha256<T: ToString>(content: Option<T>) -> 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<T: ToString>(content: Option<T>) -> 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<T: ToString>(content: Option<T>) -> 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<String> {
|
||||
fn digest(&self, encoding: EncodingKind) -> Result<String> {
|
||||
(*self.algo.lock().unwrap()).digest(encoding)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,12 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
|
|||
Ok(Crypto::sha512(content))
|
||||
})?
|
||||
.with_function("md5", |_, content: Option<String>| Ok(Crypto::md5(content)))?
|
||||
.with_function("blake2s256", |_, content: Option<String>| {
|
||||
Ok(Crypto::blake2s256(content))
|
||||
})?
|
||||
.with_function("blake2b512", |_, content: Option<String>| {
|
||||
Ok(Crypto::blake2b512(content))
|
||||
})?
|
||||
.build()?,
|
||||
)?
|
||||
.build_readonly()
|
||||
|
|
Loading…
Add table
Reference in a new issue