feat: initial sea128 encrypt is working

This commit is contained in:
0xalivecow 2024-10-22 11:37:09 +02:00
parent c416547067
commit c21c11aed0
No known key found for this signature in database
5 changed files with 89 additions and 7 deletions

View file

@ -1,3 +1,4 @@
use anyhow::Result;
use base64::prelude::*;
use std::{fmt::format, str::FromStr, u128, u8};
@ -19,6 +20,16 @@ pub fn get_alpha_rep(num: u128) -> String {
alpha_rep
}
pub fn b64_2_num(string: &String) -> Result<u128> {
let decoded: Vec<u8> = BASE64_STANDARD.decode(string)?;
let mut bytes: [u8; 16] = [0u8; 16];
bytes.copy_from_slice(&decoded);
let number: u128 = <u128>::from_ne_bytes(bytes);
Ok(number)
}
pub fn get_coefficients(num: u128) -> Vec<u8> {
let mut powers: Vec<u8> = vec![];
for shift in 0..128 {
@ -54,6 +65,8 @@ pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128 {
#[cfg(test)]
mod tests {
use crate::utils::poly::b64_2_num;
use anyhow::Result;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
@ -88,4 +101,17 @@ mod tests {
"ARIAAAAAAAAAAAAAAAAAgA=="
);
}
#[test]
fn test_b64_2_num() -> Result<()> {
let b64_payload: String = String::from_str("juMqbhnlBwAAAAAAAAAAAA==")?;
assert_eq!(
b64_2_num(&b64_payload)?,
2222222222222222,
"Error: Value was: {}",
b64_2_num(&b64_payload)?
);
Ok(())
}
}