Struct PrivateKey

pub struct PrivateKey(/* private fields */);
Expand description

This represent a private key. Must be kept secret.

Could be used to generate a new one or restore an older already saved.

Implementations§

§

impl PrivateKey

pub fn new<T>(rnd: &mut T) -> Self
where T: CryptoRng + RngCore,

Generate a random key.

§Example
use rand_core::OsRng;
use ed448_rust::PrivateKey;
let private_key = PrivateKey::new(&mut OsRng);

pub const fn as_bytes(&self) -> &[u8; 57]

Convert the private key to a format exportable.

§Example
let exportable_pkey = private_key.as_bytes();

pub fn sign(&self, msg: &[u8], ctx: Option<&[u8]>) -> Result<[u8; 114]>

Sign with key pair.

It’s possible to indicate a context. More information in RFC8032 8.3 Use of contexts.

§Examples

Without any context.

use ed448_rust::PrivateKey;
let pkey = PrivateKey::from([0xcd, 0x23, 0xd2, 0x4f, 0x71, 0x42, 0x74, 0xe7, 0x44, 0x34, 0x32, 0x37, 0xb9,
            0x32, 0x90, 0xf5, 0x11, 0xf6, 0x42, 0x5f, 0x98, 0xe6, 0x44, 0x59, 0xff, 0x20, 0x3e, 0x89, 0x85,
            0x08, 0x3f, 0xfd, 0xf6, 0x05, 0x00, 0x55, 0x3a, 0xbc, 0x0e, 0x05, 0xcd, 0x02, 0x18, 0x4b, 0xdb,
            0x89, 0xc4, 0xcc, 0xd6, 0x7e, 0x18, 0x79, 0x51, 0x26, 0x7e, 0xb3, 0x28]);
let msg = &[0x0c, 0x3e, 0x54, 0x40, 0x74, 0xec, 0x63, 0xb0, 0x26, 0x5e, 0x0c];
let sig = pkey.sign(msg, None).unwrap();

assert_eq!(
    sig.iter().map(|b| format!("{:02x}", b)).collect::<Vec<String>>().concat(),
    "1f0a8888ce25e8d458a21130879b840a9089d999aaba039eaf3e3afa090a09d389dba82c4ff2ae8a\
        c5cdfb7c55e94d5d961a29fe0109941e00b8dbdeea6d3b051068df7254c0cdc129cbe62db2dc9\
        57dbb47b51fd3f213fb8698f064774250a5028961c9bf8ffd973fe5d5c206492b140e00"
);

With a context.

use ed448_rust::PrivateKey;
let pkey = PrivateKey::from([0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6, 0x32, 0xf3, 0xdb, 0xb4, 0x84,
            0x89, 0x92, 0x4d, 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d, 0x4a, 0x1f, 0x00, 0xac, 0xda,
            0x2c, 0x46, 0x3a, 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c, 0x5e, 0x3b, 0xc3, 0x97, 0xa6,
            0x59, 0x94, 0x9e, 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27, 0x4e]);
let msg = &[03];
let sig = pkey.sign(msg, Some(&[0x66, 0x6f, 0x6f])).unwrap();

assert_eq!(
    sig.iter().map(|b| format!("{:02x}", b)).collect::<Vec<String>>().concat(),
    "d4f8f6131770dd46f40867d6fd5d5055de43541f8c5e35abbcd001b32a89f7d2151f7647f11d8ca2\
        ae279fb842d607217fce6e042f6815ea000c85741de5c8da1144a6a1aba7f96de42505d7a7298\
        524fda538fccbbb754f578c1cad10d54d0d5428407e85dcbc98a49155c13764e66c3c00"
);
§Errors

pub fn sign_ph(&self, msg: &[u8], ctx: Option<&[u8]>) -> Result<[u8; 114]>

Sign with key pair. Message is pre-hashed before signed.

The message is hashed before being signed. The size of the signed message in this case is always 64 bytes length.

See PrivateKey::sign.

§Errors

Trait Implementations§

§

impl Clone for PrivateKey

§

fn clone(&self) -> PrivateKey

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
§

impl Debug for PrivateKey

§

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

Formats the value using the given formatter. Read more
§

impl From<&[u8; 57]> for PrivateKey

§

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

Converts to this type from the input type.
§

impl From<&PrivateKey> for PublicKey

Instantiate a PublicKey from the PrivateKey.

§

fn from(private_key: &PrivateKey) -> Self

Converts to this type from the input type.
§

impl From<[u8; 57]> for PrivateKey

Restore the private key from the slice.

§

fn from(array: [u8; 57]) -> Self

Converts to this type from the input type.
§

impl TryFrom<&[u8]> for PrivateKey

Restore the private key from an array.

§Error

Could return Ed448Error::WrongKeyLength if the array’s length is not KEY_LENGTH.

§

type Error = Ed448Error

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

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

Performs the conversion.
§

impl Copy for PrivateKey

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