pub struct Key { /* private fields */ }Expand description
Contains a valid AES key. Can be instantiated with a random key, or built from a slice
of bytes that is 16, 24, or 32 bytes long.
A key object is required to instantiate a Cipher.
§Examples
use aesp::Key;
// Instantiate random keys:
let rk_128 = Key::rand_key_128()?;
let rk_192 = Key::rand_key_192()?;
let rk_256 = Key::rand_key_256()?;
// Instantiate keys from slice:
let key_bytes: [u8; 32] = [0xBA, 0x32, 0x82, 0x9A, 0x43, 0x8A, 0x48, 0xED,
0xC2, 0xEA, 0x10, 0x73, 0x26, 0xF8, 0xA9, 0x62,
0xDE, 0x82, 0x06, 0xBA, 0x53, 0xC2, 0xC7, 0x55,
0x2C, 0x72, 0xC5, 0x37, 0xBF, 0xD4, 0xDB, 0x5E];
let my_key_128 = Key::try_from_slice(&key_bytes[..16])?;
let my_key_192 = Key::try_from_slice(&key_bytes[..24])?;
let my_key_256 = Key::try_from_slice(&key_bytes[..32])?;
// Internal bytes of Key objects are accessible and match the original key:
assert_eq!(my_key_128.as_bytes(), &key_bytes[..16]);
assert_eq!(my_key_192.as_bytes(), &key_bytes[..24]);
assert_eq!(my_key_256.as_bytes(), &key_bytes[..32]);
// Attempting to instantiate with an invalid key size (not 16, 24, or 32 bytes)
// returns an InvalidKeyLength error:
assert!(Key::try_from_slice(&key_bytes[..20]).is_err());Implementations§
Source§impl Key
impl Key
Sourcepub fn rand_key_128() -> Result<Self>
pub fn rand_key_128() -> Result<Self>
Generate a random 128-bit key. Returns Error if OsRng fails.
Sourcepub fn rand_key_192() -> Result<Self>
pub fn rand_key_192() -> Result<Self>
Generate a random 192-bit key. Returns Error if OsRng fails.
Sourcepub fn rand_key_256() -> Result<Self>
pub fn rand_key_256() -> Result<Self>
Generate a random 256-bit key. Returns Error if OsRng fails.
Sourcepub fn try_from_slice(bytes: &[u8]) -> Result<Self>
pub fn try_from_slice(bytes: &[u8]) -> Result<Self>
Attempts to build a key from a slice of bytes. Will return an InvalidKeyLength error if the input slice is anything other than 16, 24, or 32 bytes long.
Trait Implementations§
impl Eq for Key
impl StructuralPartialEq for Key
Auto Trait Implementations§
impl Freeze for Key
impl RefUnwindSafe for Key
impl Send for Key
impl Sync for Key
impl Unpin for Key
impl UnsafeUnpin for Key
impl UnwindSafe for Key
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more