Struct secp256k1::ecdh::SharedSecret
source · pub struct SharedSecret(/* private fields */);
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 rand::thread_rng());
let (sk2, pk2) = s.generate_keypair(&mut rand::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::{SecretKey, PublicKey};
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 non_secure_erase(&mut self)
pub fn non_secure_erase(&mut self)
Attempts to erase the contents of the underlying array.
Note, however, that the compiler is allowed to freely copy or move the
contents of this array to other places in memory. Preventing this behavior
is very subtle. For more discussion on this, please see the documentation
of the zeroize
crate.
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
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
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,
source§fn eq(&self, other: &SharedSecret) -> bool
fn eq(&self, other: &SharedSecret) -> bool
self
and other
values to be equal, and is used
by ==
.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
self
and other
) and is used by the <=
operator. Read moreAuto Trait Implementations§
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)