generated from pure_sagacity/rust-starter
apparently the gen_or_get_key logic is for string only keys
changed that and also added an implementation for the trait
This commit is contained in:
@@ -9,7 +9,7 @@ pub type Key = chacha20poly1305::Key;
|
||||
pub type Nonce = chacha20poly1305::Nonce;
|
||||
|
||||
pub mod helper {
|
||||
use super::{Generate, Key, Nonce};
|
||||
use super::{Generate, Key, Nonce, Result};
|
||||
pub fn gen_nonce() -> Nonce {
|
||||
Nonce::generate()
|
||||
}
|
||||
@@ -17,6 +17,14 @@ pub mod helper {
|
||||
pub fn gen_key() -> Key {
|
||||
Key::generate()
|
||||
}
|
||||
|
||||
pub fn key_from(bytes: Vec<u8>) -> Result<Key> {
|
||||
let array: [u8; 32] = bytes
|
||||
.try_into()
|
||||
.map_err(|_| "key must be exactly 32 bytes".to_string())?;
|
||||
|
||||
Ok(Key::from(array))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encrypt(key: &Key, plaintext: Vec<u8>) -> Result<(Nonce, Vec<u8>)> {
|
||||
|
||||
Reference in New Issue
Block a user