pub struct SecretKey { /* private fields */ }
safe_api
only.Expand description
A type to represent a secret key.
As default it will randomly generate a SecretKey
of 32 bytes.
Errors:
An error will be returned if:
slice
is empty.length
is 0.length
is not less thanisize::MAX
.
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 callunprotected_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 preferSecretType == &[u8]
overSecretType.unprotected_as_bytes() == &[u8]
. Examples are shown below. The examples apply to any type that implementsPartialEq<&'_ [u8]>
.
use orion::pwhash::Password;
// Initialize a password with 32 random bytes.
let password = Password::generate(32)?;
// Secure, constant-time comparison with a byte slice
assert_ne!(password, &[0; 32][..]);
// Secure, constant-time comparison with another Password
assert_ne!(password, Password::generate(32)?);
Implementations
sourceimpl SecretKey
impl SecretKey
sourcepub fn from_slice(slice: &[u8]) -> Result<SecretKey, UnknownCryptoError>
pub fn from_slice(slice: &[u8]) -> Result<SecretKey, UnknownCryptoError>
Construct from a given byte slice.
sourcepub fn unprotected_as_bytes(&self) -> &[u8]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
pub fn unprotected_as_bytes(&self) -> &[u8]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
Return the object as byte slice. Warning: Should not be used unless strictly needed. This breaks protections that the type implements.
Trait Implementations
impl Eq for SecretKey
Auto Trait Implementations
impl RefUnwindSafe for SecretKey
impl Send for SecretKey
impl Sync for SecretKey
impl Unpin for SecretKey
impl UnwindSafe for SecretKey
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more