mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
feat: mostly finish rust-side implementation
This commit is contained in:
parent
8539177d8e
commit
40f63c9bb0
3 changed files with 47 additions and 18 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -1136,7 +1136,7 @@ dependencies = [
|
||||||
"include_dir",
|
"include_dir",
|
||||||
"itertools",
|
"itertools",
|
||||||
"lz4_flex",
|
"lz4_flex",
|
||||||
"md-5",
|
"md5",
|
||||||
"mlua",
|
"mlua",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
@ -1205,14 +1205,10 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "md-5"
|
name = "md5"
|
||||||
version = "0.10.6"
|
version = "0.7.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
|
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"digest",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
|
|
|
@ -101,7 +101,7 @@ toml = { version = "0.8", features = ["preserve_order"] }
|
||||||
ring = "0.17.3"
|
ring = "0.17.3"
|
||||||
base64 = "0.21.4"
|
base64 = "0.21.4"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
md-5 = "0.10.6"
|
md5 = "0.7.0"
|
||||||
|
|
||||||
### NET
|
### NET
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
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 ring::digest::{self, digest, Digest};
|
use ring::digest::{self, digest, Digest as RingDigest};
|
||||||
|
|
||||||
// TODO: Proper error handling, remove unwraps
|
// TODO: Proper error handling, remove unwraps
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ pub enum CryptoAlgo {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Crypto {
|
impl Crypto {
|
||||||
pub fn sha1<T>(content: Option<T>) -> CryptoResult<String, Digest>
|
pub fn sha1<T>(content: Option<T>) -> CryptoResult<String, RingDigest>
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
|
@ -41,7 +41,7 @@ impl Crypto {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sha256<T>(content: Option<T>) -> CryptoResult<String, Digest>
|
pub fn sha256<T>(content: Option<T>) -> CryptoResult<String, RingDigest>
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
|
@ -54,7 +54,7 @@ impl Crypto {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sha512<T>(content: Option<T>) -> CryptoResult<String, Digest>
|
pub fn sha512<T>(content: Option<T>) -> CryptoResult<String, RingDigest>
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
|
@ -90,6 +90,7 @@ impl FromCryptoAlgo for ring::digest::Algorithm {
|
||||||
match &value {
|
match &value {
|
||||||
CryptoAlgo::Sha256 => &digest::SHA256,
|
CryptoAlgo::Sha256 => &digest::SHA256,
|
||||||
CryptoAlgo::Sha512 => &digest::SHA512,
|
CryptoAlgo::Sha512 => &digest::SHA512,
|
||||||
|
CryptoAlgo::Sha1 => &digest::SHA1_FOR_LEGACY_USE_ONLY,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,6 +122,40 @@ pub enum EncodingKind {
|
||||||
|
|
||||||
// Note that compute and digest declared here are identical to those of the below implementation
|
// Note that compute and digest declared here are identical to those of the below implementation
|
||||||
// Quite a bit of boilerplate, is there any way to avoid this without using derive macros?
|
// Quite a bit of boilerplate, is there any way to avoid this without using derive macros?
|
||||||
|
impl CryptoResult<String, Vec<u8>> {
|
||||||
|
pub fn update(&mut self, content: String) -> Self {
|
||||||
|
self.content = Some(content);
|
||||||
|
|
||||||
|
(*self).to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compute(&mut self) -> Self {
|
||||||
|
let content = match &self.content {
|
||||||
|
Some(inner) => inner.to_owned(),
|
||||||
|
None => "".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
match self.algo {
|
||||||
|
CryptoAlgo::Md5 => self.computed = Some(md5::compute(content).to_vec()),
|
||||||
|
_ => panic!("Invalid implementation"),
|
||||||
|
};
|
||||||
|
|
||||||
|
(*self).to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn digest(&self, encoding: EncodingKind) -> Result<String> {
|
||||||
|
let computed = self.computed.clone().ok_or(anyhow::Error::msg(
|
||||||
|
"compute the hash first before trying to obtain a digest",
|
||||||
|
))?;
|
||||||
|
|
||||||
|
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(computed.to_vec())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CryptoResult<String, ring::hmac::Tag> {
|
impl CryptoResult<String, ring::hmac::Tag> {
|
||||||
pub fn update(&mut self, content: String) -> Self {
|
pub fn update(&mut self, content: String) -> Self {
|
||||||
self.content = Some(content);
|
self.content = Some(content);
|
||||||
|
@ -166,7 +201,7 @@ impl CryptoResult<String, ring::hmac::Tag> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CryptoResult<String, Digest> {
|
impl CryptoResult<String, RingDigest> {
|
||||||
pub fn update(&mut self, content: String) -> Self {
|
pub fn update(&mut self, content: String) -> Self {
|
||||||
self.content = Some(content);
|
self.content = Some(content);
|
||||||
|
|
||||||
|
@ -180,15 +215,13 @@ impl CryptoResult<String, Digest> {
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.algo {
|
match self.algo {
|
||||||
CryptoAlgo::Sha256 | CryptoAlgo::Sha512 => {
|
CryptoAlgo::Sha256 | CryptoAlgo::Sha512 | CryptoAlgo::Sha1 => {
|
||||||
self.computed = Some(digest(
|
self.computed = Some(digest(
|
||||||
ring::digest::Algorithm::from_crypto_algo(self.algo.clone()),
|
ring::digest::Algorithm::from_crypto_algo(self.algo.clone()),
|
||||||
content.as_bytes(),
|
content.as_bytes(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
CryptoAlgo::Hmac(_) => unreachable!(),
|
_ => unreachable!(),
|
||||||
CryptoAlgo::Md5 => todo!(),
|
|
||||||
CryptoAlgo::Sha1 => todo!(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
(*self).to_owned()
|
(*self).to_owned()
|
||||||
|
|
Loading…
Add table
Reference in a new issue