pub struct SymmetricKey(/* private fields */);
Expand description
A symmetric encryption key used for both encryption and decryption.
SymmetricKey
is a 32-byte cryptographic key used with ChaCha20-Poly1305
AEAD (Authenticated Encryption with Associated Data) encryption. This
implementation follows the IETF ChaCha20-Poly1305 specification as defined in RFC-8439.
Symmetric encryption uses the same key for both encryption and decryption, unlike asymmetric encryption where different keys are used for each operation.
SymmetricKey
can be used to encrypt plaintext into an EncryptedMessage
that includes:
- Ciphertext (the encrypted data)
- Nonce (a unique number used once for each encryption)
- Authentication tag (to verify message integrity)
- Optional additional authenticated data (AAD)
Implementations§
Source§impl SymmetricKey
impl SymmetricKey
pub const SYMMETRIC_KEY_SIZE: usize = 32usize
Sourcepub fn new_using(rng: &mut impl RandomNumberGenerator) -> Self
pub fn new_using(rng: &mut impl RandomNumberGenerator) -> Self
Create a new random symmetric key using the given random number generator.
Sourcepub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
Create a new symmetric key from data.
Sourcepub fn from_hex(hex: impl AsRef<str>) -> Result<Self>
pub fn from_hex(hex: impl AsRef<str>) -> Result<Self>
Create a new symmetric key from the given hexadecimal string.
§Panics
Panics if the string is not exactly 24 hexadecimal digits.
Sourcepub fn encrypt(
&self,
plaintext: impl AsRef<[u8]>,
aad: Option<impl AsRef<[u8]>>,
nonce: Option<impl AsRef<Nonce>>,
) -> EncryptedMessage
pub fn encrypt( &self, plaintext: impl AsRef<[u8]>, aad: Option<impl AsRef<[u8]>>, nonce: Option<impl AsRef<Nonce>>, ) -> EncryptedMessage
Encrypt the given plaintext with this key, and the given additional authenticated data and nonce.
Sourcepub fn encrypt_with_digest(
&self,
plaintext: impl AsRef<[u8]>,
digest: impl AsRef<Digest>,
nonce: Option<impl AsRef<Nonce>>,
) -> EncryptedMessage
pub fn encrypt_with_digest( &self, plaintext: impl AsRef<[u8]>, digest: impl AsRef<Digest>, nonce: Option<impl AsRef<Nonce>>, ) -> EncryptedMessage
Encrypt the given plaintext with this key, and the given digest of the plaintext, and nonce.
Trait Implementations§
Source§impl AsRef<[u8]> for SymmetricKey
impl AsRef<[u8]> for SymmetricKey
Source§impl AsRef<SymmetricKey> for SymmetricKey
Implements AsRef<SymmetricKey>
to allow self-reference.
impl AsRef<SymmetricKey> for SymmetricKey
Implements AsRef<SymmetricKey>
to allow self-reference.
Source§fn as_ref(&self) -> &SymmetricKey
fn as_ref(&self) -> &SymmetricKey
Source§impl CBORTagged for SymmetricKey
Implements CBORTagged to provide the CBOR tag for the SymmetricKey.
impl CBORTagged for SymmetricKey
Implements CBORTagged to provide the CBOR tag for the SymmetricKey.
Source§impl CBORTaggedDecodable for SymmetricKey
Implements CBORTaggedDecodable to provide CBOR decoding functionality.
impl CBORTaggedDecodable for SymmetricKey
Implements CBORTaggedDecodable to provide CBOR decoding functionality.
Source§fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for SymmetricKey
Implements CBORTaggedEncodable to provide CBOR encoding functionality.
impl CBORTaggedEncodable for SymmetricKey
Implements CBORTaggedEncodable to provide CBOR encoding functionality.
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl Clone for SymmetricKey
impl Clone for SymmetricKey
Source§fn clone(&self) -> SymmetricKey
fn clone(&self) -> SymmetricKey
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for SymmetricKey
Implements Debug formatting to display the key in hexadecimal format.
impl Debug for SymmetricKey
Implements Debug formatting to display the key in hexadecimal format.
Source§impl Default for SymmetricKey
Implements Default to create a new random symmetric key.
impl Default for SymmetricKey
Implements Default to create a new random symmetric key.
Source§impl<'a> From<&'a SymmetricKey> for &'a [u8; 32]
Implements conversion from a SymmetricKey reference to a byte array
reference.
impl<'a> From<&'a SymmetricKey> for &'a [u8; 32]
Implements conversion from a SymmetricKey reference to a byte array reference.
Source§fn from(key: &'a SymmetricKey) -> Self
fn from(key: &'a SymmetricKey) -> Self
Source§impl From<&SymmetricKey> for SymmetricKey
Implements conversion from a SymmetricKey reference to a SymmetricKey.
impl From<&SymmetricKey> for SymmetricKey
Implements conversion from a SymmetricKey reference to a SymmetricKey.
Source§fn from(key: &SymmetricKey) -> Self
fn from(key: &SymmetricKey) -> Self
Source§impl From<&SymmetricKey> for Vec<u8>
Implements conversion from a SymmetricKey reference to a Vec<u8>
.
impl From<&SymmetricKey> for Vec<u8>
Implements conversion from a SymmetricKey reference to a Vec<u8>
.
Source§fn from(key: &SymmetricKey) -> Self
fn from(key: &SymmetricKey) -> Self
Source§impl From<SymmetricKey> for CBOR
Implements conversion from SymmetricKey to CBOR for serialization.
impl From<SymmetricKey> for CBOR
Implements conversion from SymmetricKey to CBOR for serialization.
Source§fn from(value: SymmetricKey) -> Self
fn from(value: SymmetricKey) -> Self
Source§impl From<SymmetricKey> for Vec<u8>
Implements conversion from a SymmetricKey to a Vec<u8>
.
impl From<SymmetricKey> for Vec<u8>
Implements conversion from a SymmetricKey to a Vec<u8>
.
Source§fn from(key: SymmetricKey) -> Self
fn from(key: SymmetricKey) -> Self
Source§impl Hash for SymmetricKey
impl Hash for SymmetricKey
Source§impl PartialEq for SymmetricKey
impl PartialEq for SymmetricKey
Source§impl TryFrom<CBOR> for SymmetricKey
Implements TryFrom<CBOR>
for SymmetricKey to support conversion from CBOR
data.
impl TryFrom<CBOR> for SymmetricKey
Implements TryFrom<CBOR>
for SymmetricKey to support conversion from CBOR
data.
Source§impl TryFrom<Vec<u8>> for SymmetricKey
Implements conversion from a Vec<u8>
to a SymmetricKey.
impl TryFrom<Vec<u8>> for SymmetricKey
Implements conversion from a Vec<u8>
to a SymmetricKey.
impl Eq for SymmetricKey
impl StructuralPartialEq for SymmetricKey
Auto Trait Implementations§
impl Freeze for SymmetricKey
impl RefUnwindSafe for SymmetricKey
impl Send for SymmetricKey
impl Sync for SymmetricKey
impl Unpin for SymmetricKey
impl UnwindSafe for SymmetricKey
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
Source§impl<T> CBORDecodable for T
impl<T> CBORDecodable for T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Lower case
letters are used (e.g. f9b4ca
)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Upper case
letters are used (e.g. F9B4CA
)