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 Into<Vec<u8>>,
aad: Option<impl Into<Vec<u8>>>,
nonce: Option<impl AsRef<Nonce>>,
) -> EncryptedMessage
pub fn encrypt( &self, plaintext: impl Into<Vec<u8>>, aad: Option<impl Into<Vec<u8>>>, nonce: Option<impl AsRef<Nonce>>, ) -> EncryptedMessage
Encrypt the given plaintext with this key, and the given additional authenticated data and nonce.
Trait Implementations§
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.