Add crypto generate_key function

This commit is contained in:
NicolasCantu 2025-03-12 10:20:30 +01:00 committed by Nicolas Cantu
parent f20a95ad56
commit 525d7f14a8

View File

@ -1,4 +1,4 @@
use anyhow::{Error, Result};
use anyhow::Result;
use sp_client::silentpayments::{
bitcoin_hashes::{sha256t_hash_newtype, Hash, HashEngine},
secp256k1::PublicKey,
@ -6,7 +6,7 @@ use sp_client::silentpayments::{
use aes_gcm::aead::{Aead, Payload};
pub use aes_gcm::{AeadCore, Aes256Gcm, KeyInit};
use rand::thread_rng;
use rand::{thread_rng, CryptoRng, RngCore};
pub const AAD: &[u8] = "4nk".as_bytes();
@ -25,6 +25,11 @@ impl AnkSharedSecretHash {
}
}
pub fn generate_key(rng: &mut (impl CryptoRng + RngCore)) -> [u8; 32] {
let key = Aes256Gcm::generate_key(rng);
key.into()
}
pub fn encrypt_with_key(key: &[u8; 32], plaintext: &[u8]) -> Result<Vec<u8>> {
let encryption_eng = Aes256Gcm::new(key.into());
let nonce = Aes256Gcm::generate_nonce(&mut thread_rng());