pub struct SecretKey { /* private fields */ }
Expand description

A type to represent the SecretKey that Chacha20, XChaCha20, ChaCha20-Poly1305 and XChaCha20-Poly1305 use.

Errors:

An error will be returned if:

  • slice is not 32 bytes.

Panics:

A panic will occur if:

  • Failure to generate random bytes securely.

Security:

  • Avoid using unprotected_as_bytes() whenever possible, as it breaks all protections that the type implements.

  • The trait PartialEq<&'_ [u8]> is implemented for this type so that users are not tempted to call unprotected_as_bytes to compare this sensitive value to a byte slice. The trait is implemented in such a way that the comparison happens in constant time. Thus, users should prefer SecretType == &[u8] over SecretType.unprotected_as_bytes() == &[u8]. Examples are shown below. The examples apply to any type that implements PartialEq<&'_ [u8]>.

use orion::hazardous::stream::chacha20::SecretKey;

// Initialize a secret key with random bytes.
let secret_key = SecretKey::generate();

// Secure, constant-time comparison with a byte slice
assert_ne!(secret_key, &[0; 32][..]);

// Secure, constant-time comparison with another SecretKey
assert_ne!(secret_key, SecretKey::generate());

Implementations§

Construct from a given byte slice.

Return the object as byte slice. Warning: Should not be used unless strictly needed. This breaks protections that the type implements.

Randomly generate using a CSPRNG. Not available in no_std context.

Return the length of the object.

Return true if this object does not hold any data, false otherwise.

NOTE: This method should always return false, since there shouldn’t be a way to create an empty instance of this object.

Trait Implementations§

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more

Make an object from a byte array.

This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.