Struct chacha20stream::key::Key [−][src]
A 32 byte key for the chacha20_poly1305 cipher
Generation
You can generate a random key with Key::new()
.
To create a key structure from bytes, you can use Key::from_bytes()
if the size of the buffer is exact, or you can write to an empty Key
as it implements Default
.
let mut key = Key::default(); key.as_mut().copy_from_slice(&key_bytes[..KEY_SIZE]);
You can also generate a random key/IV pair with chacha20stream::keygen()
.
Encoding
This type implements std::fmt::Display
, which prints the key as a base64 string.
Additionally, it implements std::str::FromStr
, which decodes a base64 string into a Key
instance.
If the input base64 string data decoded is shorter than KEY_SIZE
, the rest of the key instance is padded with 0s.
If it is longer, the rest is ignored.
The key can also be lazily formatted as a hex string, with the method to_hex_string()
.
let key = Key::new(); let key_encoded = key.to_string(); println!("Key base64: {}", key_encoded); println!("Key hex: {}", key.to_hex_string()); assert_eq!(key_encoded.parse::<Key>().unwrap(), key);
Implementations
impl Key
[src]
pub fn from_bytes(k: [u8; 32]) -> Self
[src]
Construct a Key
from an exact length (32 bytes) buffer.
pub fn new() -> Self
[src]
Create a new random 32 byte chacha20_poly1305 Key
.
pub fn to_hex_string(&self) -> impl Display + '_
[src]
Format this key as a hex string
Returns an opaque type that lazily formats the key into a hex string when written.
Example
fn print_key_info(key: &Key) { println!("Key base64: {}", key); println!("Key hex: {}", key.to_hex_string()); }
Formatting to String
let key_hex_string = key.to_hex_string().to_string();
Trait Implementations
impl AsMut<[u8]> for Key
[src]
impl AsRef<[u8]> for Key
[src]
impl AsRef<Key> for Key
[src]
impl Clone for Key
[src]
impl Copy for Key
[src]
impl Debug for Key
[src]
impl Default for Key
[src]
impl Display for Key
[src]
impl Eq for Key
[src]
impl From<[u8; 32]> for Key
[src]
impl FromStr for Key
[src]
type Err = DecodeError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<Self, Self::Err>
[src]
impl Hash for Key
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl PartialEq<Key> for Key
[src]
impl StructuralEq for Key
[src]
impl StructuralPartialEq for Key
[src]
Auto Trait Implementations
impl RefUnwindSafe for Key
impl Send for Key
impl Sync for Key
impl Unpin for Key
impl UnwindSafe for Key
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,