Struct elliptic_curve::SecretKey
source · pub struct SecretKey<C: Curve> { /* private fields */ }
Expand description
Elliptic curve secret keys.
This type wraps a secret scalar value, helping to prevent accidental exposure and securely erasing the value from memory when dropped.
Parsing PKCS#8 Keys
PKCS#8 is a commonly used format for encoding secret keys (especially ones generated by OpenSSL).
Keys in PKCS#8 format are either binary (ASN.1 BER/DER), or PEM encoded (ASCII) and begin with the following:
-----BEGIN PRIVATE KEY-----
To decode an elliptic curve private key from PKCS#8, enable the pkcs8
feature of this crate (or the pkcs8
feature of a specific RustCrypto
elliptic curve crate) and use the [DecodePrivateKey
] trait to parse it.
When the pem
feature of this crate (or a specific RustCrypto elliptic
curve crate) is enabled, a [FromStr
] impl is also available.
Implementations§
source§impl<C> SecretKey<C>where
C: Curve,
impl<C> SecretKey<C>where C: Curve,
sourcepub fn random(rng: impl CryptoRng + RngCore) -> Selfwhere
C: ProjectiveArithmetic,
pub fn random(rng: impl CryptoRng + RngCore) -> Selfwhere C: ProjectiveArithmetic,
Generate a random SecretKey
.
sourcepub fn new(scalar: ScalarCore<C>) -> Self
pub fn new(scalar: ScalarCore<C>) -> Self
Create a new secret key from a scalar value.
sourcepub fn as_scalar_core(&self) -> &ScalarCore<C>
pub fn as_scalar_core(&self) -> &ScalarCore<C>
Borrow the inner secret ScalarCore
value.
⚠️ Warning
This value is key material.
Please treat it with the care it deserves!
sourcepub fn to_nonzero_scalar(&self) -> NonZeroScalar<C>where
C: Curve + ProjectiveArithmetic,
pub fn to_nonzero_scalar(&self) -> NonZeroScalar<C>where C: Curve + ProjectiveArithmetic,
Get the secret NonZeroScalar
value for this key.
⚠️ Warning
This value is key material.
Please treat it with the care it deserves!
sourcepub fn public_key(&self) -> PublicKey<C>where
C: Curve + ProjectiveArithmetic,
pub fn public_key(&self) -> PublicKey<C>where C: Curve + ProjectiveArithmetic,
Get the PublicKey
which corresponds to this secret key
sourcepub fn from_be_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_be_bytes(bytes: &[u8]) -> Result<Self>
Deserialize raw secret scalar as a big endian integer.
sourcepub fn to_be_bytes(&self) -> FieldBytes<C>
pub fn to_be_bytes(&self) -> FieldBytes<C>
Serialize raw secret scalar as a big endian integer.
sourcepub fn from_sec1_der(der_bytes: &[u8]) -> Result<Self>where
C: Curve + ValidatePublicKey,
FieldSize<C>: ModulusSize,
pub fn from_sec1_der(der_bytes: &[u8]) -> Result<Self>where C: Curve + ValidatePublicKey, FieldSize<C>: ModulusSize,
Deserialize secret key encoded in the SEC1 ASN.1 DER ECPrivateKey
format.
sourcepub fn to_sec1_der(&self) -> Result<Zeroizing<Vec<u8>>>where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
pub fn to_sec1_der(&self) -> Result<Zeroizing<Vec<u8>>>where C: Curve + ProjectiveArithmetic, AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>, FieldSize<C>: ModulusSize,
Serialize secret key in the SEC1 ASN.1 DER ECPrivateKey
format.