Skip to main content

Key

Struct Key 

Source
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

Source

pub fn rand_key_128() -> Result<Self>

Generate a random 128-bit key. Returns Error if OsRng fails.

Source

pub fn rand_key_192() -> Result<Self>

Generate a random 192-bit key. Returns Error if OsRng fails.

Source

pub fn rand_key_256() -> Result<Self>

Generate a random 256-bit key. Returns Error if OsRng fails.

Source

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.

Source

pub fn as_bytes(&self) -> &[u8]

Returns a reference to the internal key as an array of bytes.

Trait Implementations§

Source§

impl Clone for Key

Source§

fn clone(&self) -> Key

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Key

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Key

Source§

fn eq(&self, other: &Key) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Key

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V