Keypair

Struct Keypair 

Source
pub struct Keypair<Kp: KeyPairSchema>(pub Kp);
Expand description

It represents an asymmetric private/public encryption key.

Tuple Fields§

§0: Kp

Implementations§

Source§

impl<Kp: KeyPairSchema> Keypair<Kp>

Source

pub fn random() -> Self

Generate a Keypair random.

§Example
use symbol_crypto_core::prelude::{Keypair, Sym};
let keypair = Keypair::<Sym>::random();
Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Construct a Keypair from the bytes of a PublicKey and PrivateKey.

§Inputs
  • bytes: an &[u8] representing the PublicKey and PrivateKey as bytes.
§Returns

A Result whose okay value is a Keypair or whose error value is an describing the error that occurred.

Source

pub fn from_hex_private_key<S: AsRef<str>>(hex: S) -> Result<Self>

Construct a Keypair from a hex encoded private key string.

§Inputs
  • hex: an S representing the hex private key (String or &str).
§Example
use symbol_crypto_core::prelude::{Keypair, Sym};
let private_key_hex: &str =
"7D3E959EB0CD66CC1DB6E9C62CB81EC52747AB56FA740CF18AACB5003429AD2E";
let keypair = Keypair::<Sym>::from_hex_private_key(private_key_hex);
§Returns

A Result whose okay value is an Keypair or whose error value is an failure::Error describing the error that occurred.

Source

pub fn from_private_key(private_key: PrivateKey) -> Self

Construct a Keypair PrivateKey type.

§Inputs
  • private_key: representing the PrivateKey type.
§Example
use symbol_crypto_core::prelude::{Keypair, PrivateKey, Sym};
let private_key_hex: &str = "7D3E959EB0CD66CC1DB6E9C62CB81EC52747AB56FA740CF18AACB5003429AD2E";
let private_key = PrivateKey::from_str(private_key_hex).unwrap();
let keypair = Keypair::<Sym>::from_private_key(private_key);
§Returns

A Keypair

Source

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

Convert this keypair to bytes.

§Returns

An array of bytes, [u8; KEYPAIR_LENGTH]. The first KEY_BYTES_SIZE of bytes is the PrivateKey, and the next KEY_BYTES_SIZE bytes is the PublicKey.

Source

pub fn sign(&self, data: &[u8]) -> Signature

Signs a data bytes with a Keypair.

§Inputs
  • data: an &[u8] representing the data to sign.
§Example
use symbol_crypto_core::prelude::{Keypair, Sym};
let keypair = Keypair::<Sym>::random();
let data = b"8ce03cd60514233b86789729102ea09e867fc6d964dea8c2018ef7d0a2e0e24bf7e348e917116690b9";

let signature = keypair.sign(data.as_ref());
§Returns

A Signature the signature hash.

Source

pub fn verify(&self, data: &[u8], signature: Signature) -> Result<()>

Verify a Signature on a data with this Keypair public key.

§Inputs
  • data: an &[u8] the data to verify.

  • signature: an Signature the signature hash.

§Returns

Returns Ok if the Signature was a valid signature created by this Keypair

Source

pub fn private_key(&self) -> PrivateKey

Source

pub fn public_key(&self) -> PublicKey

Source

pub fn encrypt_message( &self, receiver_pk: &[u8; 32], msg: &[u8], ) -> Result<Vec<u8>>

Encode a message text using the signer’s PrivateKey of this Keypair and receiver’s PublicKey.

§Inputs
  • receiver_pk: The receiver’s public key.

  • msg: Message to encrypt.

§Example
use symbol_crypto_core::prelude::{Keypair, Sym, PublicKey};


let message = b"Symbol is awesome from Rust!";

let encrypt_text = signer_kp.encrypt_message(receiver_pk.as_fixed_bytes(), message).unwrap();
§Returns

A Result whose okay value is an encrypt message Vec<u8> or whose error value is an failure::Error describing the error that occurred.

Source

pub fn decrypt_message( &self, signer_pk: &[u8; 32], enc_msg: &[u8], ) -> Result<Vec<u8>>

Decrypt a message text using the receiver’s the PrivateKey of this Keypair and signer’s PublicKey.

§Inputs
  • signer_pk: The signer’s public key.

  • enc_msg: Message encrypted.

§Example
use symbol_crypto_core::prelude::{Keypair, Sym, PublicKey};

let receiver_kp = Keypair::<Sym>::from_hex_private_key("A22A4BBF126A2D7D7ECE823174DFD184C5DE0FDE4CB2075D30CFA409F7EF8908").unwrap();
let signer_pk = PublicKey::from_str("3FD283D8543C12B81917C154CDF4EFD3D48E553E6D7BC77E29CB168138CED17D").unwrap();

let encrypt_text_vec = [
    125, 59, 126, 248, 124, 129, 157, 100, 111, 84, 49, 163, 111, 68, 22, 137, 75, 132, 135,
    217, 251, 158, 115, 74, 226, 172, 200, 208, 33, 183, 110, 103, 107, 170, 52, 174, 192, 110,
    164, 44, 77, 69, 203, 48, 43, 17, 206, 143, 154, 155, 231, 72, 28, 24, 20, 241, 234, 202,
    184, 66,
];

let decrypted_text = receiver_kp.decrypt_message( signer_pk.as_fixed_bytes(), &encrypt_text_vec).unwrap();
§Returns

A Result whose okay value is an decrypted message Vec<u8> or whose error value is an failure::Error describing the error that occurred.

Trait Implementations§

Source§

impl<C: KeyPairSchema> AsRef<C> for Keypair<C>

Source§

fn as_ref(&self) -> &C

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<Kp: Clone + KeyPairSchema> Clone for Keypair<Kp>

Source§

fn clone(&self) -> Keypair<Kp>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Kp: Debug + KeyPairSchema> Debug for Keypair<Kp>

Source§

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

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

impl<C: KeyPairSchema> Display for Keypair<C>

Source§

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

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

impl<Kp: Hash + KeyPairSchema> Hash for Keypair<Kp>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Kp: PartialEq + KeyPairSchema> PartialEq for Keypair<Kp>

Source§

fn eq(&self, other: &Keypair<Kp>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<Kp: Copy + KeyPairSchema> Copy for Keypair<Kp>

Source§

impl<Kp: KeyPairSchema> StructuralPartialEq for Keypair<Kp>

Auto Trait Implementations§

§

impl<Kp> Freeze for Keypair<Kp>
where Kp: Freeze,

§

impl<Kp> RefUnwindSafe for Keypair<Kp>
where Kp: RefUnwindSafe,

§

impl<Kp> Send for Keypair<Kp>
where Kp: Send,

§

impl<Kp> Sync for Keypair<Kp>
where Kp: Sync,

§

impl<Kp> Unpin for Keypair<Kp>
where Kp: Unpin,

§

impl<Kp> UnwindSafe for Keypair<Kp>
where Kp: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V