pub struct PrivateKey { /* private fields */ }Expand description
An owned private key.
Private key bytes are zeroized on drop to prevent key material from
lingering in heap memory. Clone is deliberately not derived —
cloning private keys should be an explicit operation via from_bytes.
Implementations§
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn new(algorithm: Algorithm, bytes: Vec<u8>) -> Result<Self>
pub fn new(algorithm: Algorithm, bytes: Vec<u8>) -> Result<Self>
Creates a new PrivateKey from an owned Vec<u8> after validating key size.
Sourcepub fn from_bytes(algorithm: Algorithm, bytes: &[u8]) -> Result<Self>
pub fn from_bytes(algorithm: Algorithm, bytes: &[u8]) -> Result<Self>
Creates a new PrivateKey by copying the provided bytes after validation.
Sourcepub fn into_bytes(self) -> Zeroizing<Vec<u8>>
pub fn into_bytes(self) -> Zeroizing<Vec<u8>>
Extracts the inner byte vector wrapped in Zeroizing so the
key material is automatically zeroized when the returned value
is dropped.
Sourcepub fn from_pkcs8(der: &[u8]) -> Result<Self>
pub fn from_pkcs8(der: &[u8]) -> Result<Self>
Decode a PKCS8 DER-encoded private key into an owned PrivateKey.
Sourcepub fn encode_pkcs8_to(&self, out: &mut Vec<u8>)
pub fn encode_pkcs8_to(&self, out: &mut Vec<u8>)
Encode this private key as PKCS8 DER into the given buffer.
Sourcepub fn encode_der_to(&self, out: &mut Vec<u8>)
pub fn encode_der_to(&self, out: &mut Vec<u8>)
Encode this private key as DER into the given buffer.
Sourcepub fn to_pkcs8(&self) -> Zeroizing<Vec<u8>>
pub fn to_pkcs8(&self) -> Zeroizing<Vec<u8>>
Encode this private key as PKCS8 DER, returning a Zeroizing<Vec<u8>>.
The returned wrapper automatically zeroizes the DER bytes on drop.
Sourcepub fn to_der(&self) -> Zeroizing<Vec<u8>>
pub fn to_der(&self) -> Zeroizing<Vec<u8>>
Encode this private key as DER, returning a Zeroizing<Vec<u8>>.
The returned wrapper automatically zeroizes the DER bytes on drop.
Sourcepub fn from_pem(pem: &str) -> Result<Self>
pub fn from_pem(pem: &str) -> Result<Self>
Decode a PEM-encoded private key into an owned PrivateKey.
Sourcepub fn to_pem(&self) -> Zeroizing<String>
pub fn to_pem(&self) -> Zeroizing<String>
Encode this private key as PEM, returning a Zeroizing<String>.
The returned wrapper automatically zeroizes the PEM string on drop.
Sourcepub fn as_key_ref(&self) -> PrivateKeyRef<'_>
pub fn as_key_ref(&self) -> PrivateKeyRef<'_>
Returns a borrowed PrivateKeyRef.