mirror of
https://git.ethanthesleepy.one/ethanaobrien/ew
synced 2025-10-09 00:47:19 +08:00
Implement /api/start/assetHash
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use openssl::symm::{Cipher, Crypter, Mode};
|
||||
use openssl::error::ErrorStack;
|
||||
use base64::{Engine as _, engine::general_purpose};
|
||||
use rand::Rng;
|
||||
|
||||
const IV_LENGTH: usize = 16;
|
||||
const KEY: &str = "3559b435f24b297a79c68b9709ef2125";
|
||||
@@ -22,3 +23,29 @@ pub fn decrypt_packet(base64_input: &str) -> Result<String, ErrorStack> {
|
||||
|
||||
Ok(String::from_utf8(decrypted_data).unwrap())
|
||||
}
|
||||
|
||||
pub fn encrypt_packet(input: &str) -> Result<String, ErrorStack> {
|
||||
let cipher = Cipher::aes_256_cbc();
|
||||
let encryption_iv = generate_random_iv();
|
||||
|
||||
let mut encrypter = Crypter::new(cipher, Mode::Encrypt, KEY.as_bytes(), Some(&encryption_iv))?;
|
||||
|
||||
let mut encrypted_data = vec![0u8; input.len() + cipher.block_size()];
|
||||
let mut encrypted_len = encrypter.update(input.as_bytes(), &mut encrypted_data)?;
|
||||
|
||||
encrypted_len += encrypter.finalize(&mut encrypted_data[encrypted_len..])?;
|
||||
|
||||
encrypted_data.truncate(encrypted_len);
|
||||
|
||||
let mut result = encryption_iv.to_vec();
|
||||
result.extend_from_slice(&encrypted_data);
|
||||
|
||||
Ok(general_purpose::STANDARD.encode(&result))
|
||||
}
|
||||
|
||||
fn generate_random_iv() -> Vec<u8> {
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut bytes = vec![0u8; IV_LENGTH];
|
||||
rng.fill(&mut bytes[..]);
|
||||
bytes
|
||||
}
|
||||
|
Reference in New Issue
Block a user