chore: Merge newest dev to get most recent changes
This commit is contained in:
commit
a6571e43af
1769 changed files with 398681 additions and 20 deletions
59
src/utils/ciphers.rs
Normal file
59
src/utils/ciphers.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use anyhow::Result;
|
||||
use openssl::symm::{Cipher, Crypter, Mode};
|
||||
|
||||
use super::math::xor_bytes;
|
||||
|
||||
pub fn aes_128_encrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let mut encrypter = Crypter::new(Cipher::aes_128_ecb(), Mode::Encrypt, &key, None)?;
|
||||
encrypter.pad(false);
|
||||
|
||||
let mut ciphertext = [0; 32].to_vec();
|
||||
|
||||
let mut count = encrypter.update(input, &mut ciphertext)?;
|
||||
count += encrypter.finalize(&mut ciphertext)?;
|
||||
ciphertext.truncate(count);
|
||||
|
||||
//eprintln!("{:?}", &ciphertext[..]);
|
||||
|
||||
Ok(ciphertext)
|
||||
}
|
||||
|
||||
pub fn aes_128_decrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let mut decrypter = Crypter::new(Cipher::aes_128_ecb(), Mode::Decrypt, key, None)?;
|
||||
decrypter.pad(false);
|
||||
|
||||
let mut plaintext = [0; 32].to_vec();
|
||||
|
||||
let mut count = decrypter.update(input, &mut plaintext)?;
|
||||
count += decrypter.finalize(&mut plaintext)?;
|
||||
plaintext.truncate(count);
|
||||
|
||||
let mut bytes: [u8; 16] = [0u8; 16];
|
||||
bytes.copy_from_slice(&plaintext);
|
||||
let number: u128 = <u128>::from_be_bytes(bytes);
|
||||
|
||||
Ok(plaintext)
|
||||
}
|
||||
|
||||
pub fn sea_128_encrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let xor_val: u128 = 0xc0ffeec0ffeec0ffeec0ffeec0ffee11;
|
||||
let sea128_out = xor_bytes(
|
||||
&aes_128_encrypt(key, input)?,
|
||||
xor_val.to_be_bytes().to_vec(),
|
||||
)?;
|
||||
Ok(sea128_out)
|
||||
}
|
||||
|
||||
pub fn sea_128_decrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let xor_val: u128 = 0xc0ffeec0ffeec0ffeec0ffeec0ffee11;
|
||||
|
||||
let intermediate = xor_bytes(input, xor_val.to_be_bytes().to_vec())?;
|
||||
Ok(aes_128_decrypt(&key, &intermediate)?)
|
||||
}
|
||||
|
||||
/*
|
||||
* let mut bytes: [u8; 16] = [0u8; 16];
|
||||
bytes.copy_from_slice(&ciphertext);
|
||||
let number: u128 = <u128>::from_be_bytes(bytes);
|
||||
|
||||
* */
|
||||
9
src/utils/math.rs
Normal file
9
src/utils/math.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use anyhow::Result;
|
||||
|
||||
pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
|
||||
for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) {
|
||||
*byte2 ^= byte1;
|
||||
}
|
||||
|
||||
Ok(vec2)
|
||||
}
|
||||
|
|
@ -1,2 +1,4 @@
|
|||
pub mod ciphers;
|
||||
pub mod math;
|
||||
pub mod parse;
|
||||
pub mod poly;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use anyhow::Result;
|
||||
use base64::prelude::*;
|
||||
use serde_json::Value;
|
||||
use std::{fmt::format, str::FromStr, u128, u8};
|
||||
|
||||
use std::{str::FromStr, u128, u8};
|
||||
|
||||
pub fn get_alpha_rep(num: u128) -> String {
|
||||
let powers: Vec<u8> = get_coefficients(num);
|
||||
|
|
@ -21,8 +21,7 @@ pub fn get_alpha_rep(num: u128) -> String {
|
|||
alpha_rep
|
||||
}
|
||||
|
||||
pub fn block_2_number(string: String) -> Result<u128> {
|
||||
//let string: String = serde_json::from_value(val["block"].clone())?;
|
||||
pub fn b64_2_num(string: &String) -> Result<u128> {
|
||||
let decoded: Vec<u8> = BASE64_STANDARD.decode(string)?;
|
||||
|
||||
let mut bytes: [u8; 16] = [0u8; 16];
|
||||
|
|
@ -67,6 +66,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::*;
|
||||
|
||||
|
|
@ -101,4 +102,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(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue