Struct secp256k1::ecdh::SharedSecret
source · pub struct SharedSecret(_);
Expand description
Enables two parties to create a shared secret without revealing their own secrets.
Examples
let s = Secp256k1::new();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
let sec1 = SharedSecret::new(&pk2, &sk1);
let sec2 = SharedSecret::new(&pk1, &sk2);
assert_eq!(sec1, sec2);
Implementations§
sourcepub fn display_secret(&self) -> DisplaySecret
pub fn display_secret(&self) -> DisplaySecret
Formats the explicit byte value of the shared secret kept inside the type as a little-endian hexadecimal string using the provided formatter.
This is the only method that outputs the actual shared secret value, and, thus, should be used with extreme caution.
Examples
use secp256k1::ecdh::SharedSecret;
let secret = SharedSecret::new(&pk, &sk);
// Here we explicitly display the secret value:
assert_eq!(
format!("{}", secret.display_secret()),
"cf05ae7da039ddce6d56dd57d3000c6dd91c6f1695eae47e05389f11e2467043"
);
// Also, we can explicitly display with `Debug`:
assert_eq!(
format!("{:?}", secret.display_secret()),
format!("DisplaySecret(\"{}\")", secret.display_secret())
);
sourcepub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret
Creates a new shared secret from a pubkey and secret key.
sourcepub fn secret_bytes(&self) -> [u8; 32]
pub fn secret_bytes(&self) -> [u8; 32]
Returns the shared secret as a byte value.
sourcepub fn from_bytes(bytes: [u8; 32]) -> SharedSecret
pub fn from_bytes(bytes: [u8; 32]) -> SharedSecret
Creates a shared secret from bytes
array.
sourcepub fn from_slice(bytes: &[u8]) -> Result<SharedSecret, Error>
pub fn from_slice(bytes: &[u8]) -> Result<SharedSecret, Error>
Creates a shared secret from bytes
slice.
Trait Implementations§
source§fn clone(&self) -> SharedSecret
fn clone(&self) -> SharedSecret
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§fn cmp(&self, other: &SharedSecret) -> Ordering
fn cmp(&self, other: &SharedSecret) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§fn eq(&self, other: &SharedSecret) -> bool
fn eq(&self, other: &SharedSecret) -> bool
source§fn partial_cmp(&self, other: &SharedSecret) -> Option<Ordering>
fn partial_cmp(&self, other: &SharedSecret) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more