mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
feat: finalize sha1, sha256 & sha512 implementations
This commit is contained in:
parent
40cfc59f75
commit
d33d1d3087
2 changed files with 21 additions and 27 deletions
|
@ -70,9 +70,9 @@ impl FromLua<'_> for EncodingKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CryptoAlgo {
|
impl CryptoAlgo {
|
||||||
pub fn update(&mut self, content: impl AsRef<[u8]>) {
|
|
||||||
// TODO: Replace boilerplate using a macro
|
// TODO: Replace boilerplate using a macro
|
||||||
|
|
||||||
|
pub fn update(&mut self, content: impl AsRef<[u8]>) {
|
||||||
match self {
|
match self {
|
||||||
CryptoAlgo::Sha1(hasher) => {
|
CryptoAlgo::Sha1(hasher) => {
|
||||||
let mut new_hasher = (**hasher).clone();
|
let mut new_hasher = (**hasher).clone();
|
||||||
|
@ -95,27 +95,34 @@ impl CryptoAlgo {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn digest(&self, encoding: EncodingKind) -> Result<String> {
|
pub fn digest(&mut self, encoding: EncodingKind) -> Result<String> {
|
||||||
let computed: Vec<u8> = match self {
|
let computed: Vec<u8> = match self {
|
||||||
CryptoAlgo::Sha1(hasher) => {
|
CryptoAlgo::Sha1(hasher) => {
|
||||||
let hash = sha1::Digest::finalize((**hasher).clone());
|
let mut new_hasher = (**hasher).clone();
|
||||||
|
let hash = sha1::Digest::finalize_reset(&mut new_hasher);
|
||||||
|
|
||||||
|
*self = CryptoAlgo::Sha1(Box::new(new_hasher));
|
||||||
|
|
||||||
hash.to_vec()
|
hash.to_vec()
|
||||||
}
|
}
|
||||||
CryptoAlgo::Sha256(hasher) => {
|
CryptoAlgo::Sha256(hasher) => {
|
||||||
let hash = sha2::Digest::finalize((**hasher).clone());
|
let mut new_hasher = (**hasher).clone();
|
||||||
|
let hash = sha2::Digest::finalize_reset(&mut new_hasher);
|
||||||
|
|
||||||
|
*self = CryptoAlgo::Sha256(Box::new(new_hasher));
|
||||||
|
|
||||||
hash.to_vec()
|
hash.to_vec()
|
||||||
}
|
}
|
||||||
CryptoAlgo::Sha512(hasher) => {
|
CryptoAlgo::Sha512(hasher) => {
|
||||||
let hash = sha2::Digest::finalize((**hasher).clone());
|
let mut new_hasher = (**hasher).clone();
|
||||||
|
let hash = sha2::Digest::finalize_reset(&mut new_hasher);
|
||||||
|
|
||||||
|
*self = CryptoAlgo::Sha512(Box::new(new_hasher));
|
||||||
|
|
||||||
hash.to_vec()
|
hash.to_vec()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{:#?}", computed);
|
|
||||||
|
|
||||||
match encoding {
|
match encoding {
|
||||||
EncodingKind::Utf8 => String::from_utf8(computed).map_err(anyhow::Error::from),
|
EncodingKind::Utf8 => String::from_utf8(computed).map_err(anyhow::Error::from),
|
||||||
EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)),
|
EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)),
|
||||||
|
@ -173,25 +180,6 @@ impl Crypto {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl FromLua<'_> for Crypto {
|
|
||||||
// fn from_lua(value: LuaValue, _: &Lua) -> LuaResult<Self> {
|
|
||||||
// if !value.is_table() {
|
|
||||||
// return Err(LuaError::FromLuaConversionError {
|
|
||||||
// from: value.type_name(),
|
|
||||||
// to: "Crypto",
|
|
||||||
// message: Some("value must be a table".to_string()),
|
|
||||||
// });
|
|
||||||
// };
|
|
||||||
|
|
||||||
// let value = value.as_table().unwrap();
|
|
||||||
// let values = Self {
|
|
||||||
// algo: value.get("value")?,
|
|
||||||
// };
|
|
||||||
|
|
||||||
// Ok(values)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
impl LuaUserData for &'static Crypto {
|
impl LuaUserData for &'static Crypto {
|
||||||
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
methods.add_method(
|
methods.add_method(
|
||||||
|
|
|
@ -22,6 +22,12 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
|
||||||
.with_function("sha1", |_, content: Option<String>| {
|
.with_function("sha1", |_, content: Option<String>| {
|
||||||
Ok(Crypto::sha1(content))
|
Ok(Crypto::sha1(content))
|
||||||
})?
|
})?
|
||||||
|
.with_function("sha256", |_, content: Option<String>| {
|
||||||
|
Ok(Crypto::sha256(content))
|
||||||
|
})?
|
||||||
|
.with_function("sha512", |_, content: Option<String>| {
|
||||||
|
Ok(Crypto::sha512(content))
|
||||||
|
})?
|
||||||
.build()?,
|
.build()?,
|
||||||
)?
|
)?
|
||||||
.build_readonly()
|
.build_readonly()
|
||||||
|
|
Loading…
Add table
Reference in a new issue