prople_crypto/aead/key.rs
1//! `key` module provides a simple object struct to maintain
2//! generated key and nonce used to encrypt and decrypt a message
3
4use crate::{errors::CommonError, types::Value};
5
6use super::{KeyEncryption, KeyNonce};
7
8/// `Key` used to store `key` and `nonce`
9///
10/// The `key` field used to store bytes that defined by
11/// `chacha20poly1305` key properties which is an alias of
12/// `GenericArray<u8, U32>`
13///
14/// The `nonce` field used when encrypt and decrypt given message
15/// which is an alias of `GenericArray<u8, <A as AeadCore>::NonceSize>`
16/// taken from `aead` crate
17///
18/// Both of these properties grouped into this single object to simplify
19/// the API library
20#[derive(Clone, Debug)]
21pub struct Key {
22 key: KeyEncryption,
23 nonce: KeyNonce,
24}
25
26impl Key {
27 pub fn new(key: KeyEncryption, nonce: KeyNonce) -> Self {
28 Self { key, nonce }
29 }
30
31 pub fn get_key_bytes(&self) -> Result<[u8; 32], CommonError> {
32 self.key.get()
33 }
34
35 pub fn get_nonce_bytes(&self) -> Result<[u8; 24], CommonError> {
36 self.nonce.get()
37 }
38}