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
impl EcdsaP256Key
Sourcepub fn generate<R: RngCore + CryptoRng>(rng: &mut R) -> Self
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);Sourcepub fn public_key_bytes(&self) -> Vec<u8> ⓘ
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
Sourcepub fn private_key_bytes(&self) -> Vec<u8> ⓘ
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.
Sourcepub fn from_private_key_bytes(bytes: &[u8]) -> Result<Self>
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 pairErr(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
impl Sign for EcdsaP256Key
Source§fn sign(&self, _rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>>
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
impl SignerKey for EcdsaP256Key
Source§impl Verify for EcdsaP256Key
impl Verify for EcdsaP256Key
Source§fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()>
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 messagesignature- The signature to verify (64 bytes: r + s values)
§Returns
Ok(())- Signature is validErr(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());