feat: add aes/sea encrypt/decrypt in gcm and add test cases
This commit is contained in:
parent
6bef350301
commit
6b2775cde1
6 changed files with 373 additions and 5 deletions
|
|
@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
|
|||
use base64::prelude::*;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::utils::ciphers::gcm_encrypt_aes;
|
||||
use crate::utils::ciphers::{gcm_decrypt_aes, gcm_decrypt_sea, gcm_encrypt_aes, gcm_encrypt_sea};
|
||||
|
||||
pub fn gcm_encrypt(args: &Value) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)> {
|
||||
let nonce_text: String = serde_json::from_value(args["nonce"].clone())?;
|
||||
|
|
@ -21,6 +21,32 @@ pub fn gcm_encrypt(args: &Value) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>
|
|||
|
||||
match alg_text.as_str() {
|
||||
"aes128" => Ok(gcm_encrypt_aes(nonce, key, plaintext, ad)?),
|
||||
"sea128" => Ok(gcm_encrypt_sea(nonce, key, plaintext, ad)?),
|
||||
_ => Err(anyhow!("No compatible algorithm found")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gcm_decrypt(args: &Value) -> Result<(Vec<u8>, bool)> {
|
||||
let nonce_text: String = serde_json::from_value(args["nonce"].clone())?;
|
||||
let nonce = BASE64_STANDARD.decode(nonce_text)?;
|
||||
|
||||
let key_text: String = serde_json::from_value(args["key"].clone())?;
|
||||
let key = BASE64_STANDARD.decode(key_text)?;
|
||||
|
||||
let plaintext_text: String = serde_json::from_value(args["ciphertext"].clone())?;
|
||||
let plaintext = BASE64_STANDARD.decode(plaintext_text)?;
|
||||
|
||||
let ad_text: String = serde_json::from_value(args["ad"].clone())?;
|
||||
let ad = BASE64_STANDARD.decode(ad_text)?;
|
||||
|
||||
let tag_text: String = serde_json::from_value(args["tag"].clone())?;
|
||||
let tag = BASE64_STANDARD.decode(tag_text)?;
|
||||
|
||||
let alg_text: String = serde_json::from_value(args["algorithm"].clone())?;
|
||||
|
||||
match alg_text.as_str() {
|
||||
"aes128" => Ok(gcm_decrypt_aes(nonce, key, plaintext, ad, tag)?),
|
||||
"sea128" => Ok(gcm_decrypt_sea(nonce, key, plaintext, ad, tag)?),
|
||||
_ => Err(anyhow!("No compatible algorithm found")),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue