mirror of
https://github.com/lune-org/lune.git
synced 2025-04-19 19:34:02 +01:00
feat: implement md5 hashing algo
This commit is contained in:
parent
7fd5e60a8b
commit
eedf6534fb
4 changed files with 25 additions and 8 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -1137,7 +1137,7 @@ dependencies = [
|
||||||
"include_dir",
|
"include_dir",
|
||||||
"itertools",
|
"itertools",
|
||||||
"lz4_flex",
|
"lz4_flex",
|
||||||
"md5",
|
"md-5",
|
||||||
"mlua",
|
"mlua",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
@ -1207,10 +1207,14 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "md5"
|
name = "md-5"
|
||||||
version = "0.7.0"
|
version = "0.10.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
|
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
|
|
|
@ -100,7 +100,7 @@ toml = { version = "0.8", features = ["preserve_order"] }
|
||||||
|
|
||||||
base64 = "0.21.4"
|
base64 = "0.21.4"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
md5 = "0.7.0"
|
md-5 = "0.10.6"
|
||||||
sha1 = "0.10.6"
|
sha1 = "0.10.6"
|
||||||
sha2 = "0.10.8"
|
sha2 = "0.10.8"
|
||||||
digest = { version = "0.10.7", default-features = true }
|
digest = { version = "0.10.7", default-features = true }
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::{engine::general_purpose as Base64, Engine as _};
|
use base64::{engine::general_purpose as Base64, Engine as _};
|
||||||
use digest::Digest as _;
|
use digest::Digest as _;
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use std::sync::Mutex;
|
|
||||||
|
|
||||||
// TODO: Proper error handling, remove unwraps
|
// TODO: Proper error handling, remove unwraps
|
||||||
|
|
||||||
|
@ -47,7 +47,8 @@ macro_rules! impl_hash_algo {
|
||||||
impl_hash_algo! {
|
impl_hash_algo! {
|
||||||
Sha1 => sha1::Sha1,
|
Sha1 => sha1::Sha1,
|
||||||
Sha256 => sha2::Sha256,
|
Sha256 => sha2::Sha256,
|
||||||
Sha512 => sha2::Sha512
|
Sha512 => sha2::Sha512,
|
||||||
|
Md5 => md5::Md5
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -138,6 +139,17 @@ impl Crypto {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
pub fn update(&self, content: impl AsRef<[u8]>) -> &Crypto {
|
||||||
(self.algo.lock().unwrap()).update(content);
|
(self.algo.lock().unwrap()).update(content);
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
|
||||||
.with_function("decode", serde_decode)?
|
.with_function("decode", serde_decode)?
|
||||||
.with_async_function("compress", serde_compress)?
|
.with_async_function("compress", serde_compress)?
|
||||||
.with_async_function("decompress", serde_decompress)?
|
.with_async_function("decompress", serde_decompress)?
|
||||||
.with_table(
|
.with_value(
|
||||||
"crypto",
|
"crypto",
|
||||||
TableBuilder::new(lua)?
|
TableBuilder::new(lua)?
|
||||||
.with_function("sha1", |_, content: Option<String>| {
|
.with_function("sha1", |_, content: Option<String>| {
|
||||||
|
@ -28,6 +28,7 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
|
||||||
.with_function("sha512", |_, content: Option<String>| {
|
.with_function("sha512", |_, content: Option<String>| {
|
||||||
Ok(Crypto::sha512(content))
|
Ok(Crypto::sha512(content))
|
||||||
})?
|
})?
|
||||||
|
.with_function("md5", |_, content: Option<String>| Ok(Crypto::md5(content)))?
|
||||||
.build()?,
|
.build()?,
|
||||||
)?
|
)?
|
||||||
.build_readonly()
|
.build_readonly()
|
||||||
|
|
Loading…
Add table
Reference in a new issue