Skip to main content

SecretKey

Struct SecretKey 

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

X25519 Diffie-Hellman key exchange private key (RFC 7748).

§Generating a key

use crypto::curve25519::x25519::SecretKey;

let alice = SecretKey::generate();
let bob = SecretKey::generate();

§Diffie-Hellman key exchange

use crypto::curve25519::x25519::SecretKey;

let alice = SecretKey::generate();
let bob = SecretKey::generate();
let alice_shared = alice.ecdh(&bob.public_key());
let bob_shared = bob.ecdh(&alice.public_key());
assert_eq!(alice_shared, bob_shared);

§Conversion from Ed25519

An Ed25519 SecretKey can be converted to an X25519 key via From<&ed25519::SecretKey>.

§Security

The shared secret produced by ecdh must not be used directly as an encryption key. Apply a KDF (e.g. HKDF) first.

Implementations§

Source§

impl SecretKey

Source

pub fn generate() -> SecretKey

Source

pub fn from_bytes(bytes: &[u8; 32]) -> SecretKey

Source

pub fn to_bytes(&self) -> [u8; 32]

Source

pub fn public_key(&self) -> PublicKey

Source

pub fn ecdh(&self, peer: &PublicKey) -> [u8; 32]

Perform a Diffie-Hellman key exchange to derive a shared secret. The shared secret IS NOT SAFE to use directly as an encryption key and an additional key derivation operation must be applied to it.

Trait Implementations§

Source§

impl Clone for SecretKey

Source§

fn clone(&self) -> SecretKey

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SecretKey

Source§

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

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

impl Eq for SecretKey

Source§

impl From<&SecretKey> for SecretKey

Source§

fn from(ed25519_key: &SecretKey) -> Self

Converts to this type from the input type.
Source§

impl From<&[u8; 32]> for SecretKey

Source§

fn from(bytes: &[u8; 32]) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for SecretKey

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 StructuralPartialEq for SecretKey

Source§

impl TryFrom<&[u8]> for SecretKey

Source§

type Error = EllipticCurveError

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

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

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> 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.