use nostr_sdk::prelude::nip44::v2::ConversationKey;
use rand::RngCore;
pub fn encrypt_with_random_nonce(
ck: &ConversationKey,
plaintext: &[u8],
) -> Result<Vec<u8>, String> {
let mut nonce = [0u8; 32];
rand::rngs::OsRng.fill_bytes(&mut nonce);
crate::crypto::nip44::encrypt_with_nonce(ck, plaintext, nonce).map_err(|e| e.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use nostr_sdk::prelude::nip44::v2::decrypt_to_bytes;
#[test]
fn round_trips_and_never_repeats_a_nonce() {
let ck = ConversationKey::new([0x5au8; 32]);
let a = encrypt_with_random_nonce(&ck, b"hello community").unwrap();
let b = encrypt_with_random_nonce(&ck, b"hello community").unwrap();
assert_ne!(a, b);
assert_eq!(decrypt_to_bytes(&ck, &a).unwrap(), b"hello community");
assert!(decrypt_to_bytes(&ConversationKey::new([1u8; 32]), &a).is_err());
}
}