EcdsaP256Key

Struct EcdsaP256Key 

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

ECDSA P-256 key pair for digital signatures.

This key type uses the P-256 (secp256r1) elliptic curve for signing. ECDSA signatures are deterministic (RFC 6979) and provide strong security with 128-bit security level.

§Example

use rust_bottle::keys::EcdsaP256Key;
use rand::rngs::OsRng;

let rng = &mut OsRng;
let key = EcdsaP256Key::generate(rng);
let pub_key = key.public_key_bytes();
let priv_key = key.private_key_bytes();

Implementations§

Source§

impl EcdsaP256Key

Source

pub fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> Self

Generate a new ECDSA P-256 key pair.

This function generates a cryptographically secure key pair using the provided random number generator.

§Arguments
  • rng - A cryptographically secure random number generator
§Returns

A new EcdsaP256Key instance with a randomly generated key pair

§Example
use rust_bottle::keys::EcdsaP256Key;
use rand::rngs::OsRng;

let rng = &mut OsRng;
let key = EcdsaP256Key::generate(rng);
Source

pub fn public_key_bytes(&self) -> Vec<u8>

Get the public key in SEC1 uncompressed format.

The public key is returned as a 65-byte array in SEC1 uncompressed format (0x04 prefix + 32-byte x-coordinate + 32-byte y-coordinate).

§Returns

Public key bytes in SEC1 format

Source

pub fn private_key_bytes(&self) -> Vec<u8>

Get the private key bytes.

The private key is returned as a 32-byte array. This is sensitive data and should be handled securely.

§Returns

Private key bytes (32 bytes)

§Security Warning

Private keys are sensitive cryptographic material. They should be stored securely and cleared from memory when no longer needed.

Source

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

Create an ECDSA P-256 key pair from private key bytes.

This function reconstructs a key pair from a previously saved private key. The public key is automatically derived from the private key.

§Arguments
  • bytes - Private key bytes (32 bytes)
§Returns
  • Ok(EcdsaP256Key) - Reconstructed key pair
  • Err(BottleError::InvalidKeyType) - If the key format is invalid
§Example
use rust_bottle::keys::EcdsaP256Key;
use rand::rngs::OsRng;

let rng = &mut OsRng;
let original = EcdsaP256Key::generate(rng);
let priv_bytes = original.private_key_bytes();

let restored = EcdsaP256Key::from_private_key_bytes(&priv_bytes).unwrap();
assert_eq!(original.public_key_bytes(), restored.public_key_bytes());

Trait Implementations§

Source§

impl Sign for EcdsaP256Key

Source§

fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>>

Sign a message using ECDSA P-256.

The message is hashed with SHA-256 before signing. The signature is deterministic (RFC 6979), meaning the same message and key will always produce the same signature.

§Arguments
  • _rng - Random number generator (not used for deterministic signing)
  • message - The message to sign
§Returns
  • Ok(Vec<u8>) - Signature bytes (64 bytes: r + s values)
  • Err(BottleError::VerifyFailed) - If signing fails
Source§

impl SignerKey for EcdsaP256Key

Source§

fn fingerprint(&self) -> Vec<u8>

Get the public key fingerprint (SHA-256 hash).

The fingerprint is used to identify keys in keychains and IDCards.

§Returns

SHA-256 hash of the public key bytes

Source§

fn public_key(&self) -> Vec<u8>

Get the public key bytes.

§Returns

Public key bytes in SEC1 format

Source§

impl Verify for EcdsaP256Key

Source§

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

Verify an ECDSA P-256 signature.

The message is hashed with SHA-256 before verification. The signature must match the format produced by sign.

§Arguments
  • message - The original message
  • signature - The signature to verify (64 bytes: r + s values)
§Returns
  • Ok(()) - Signature is valid
  • Err(BottleError::VerifyFailed) - If signature verification fails
§Example
use rust_bottle::keys::EcdsaP256Key;
use rust_bottle::signing::{Sign, Verify};
use rand::rngs::OsRng;

let rng = &mut OsRng;
let key = EcdsaP256Key::generate(rng);
let message = b"Test message";

let signature = key.sign(rng, message).unwrap();
assert!(key.verify(message, &signature).is_ok());

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