Struct rsa::RSAPrivateKey[][src]

pub struct RSAPrivateKey { /* fields omitted */ }
Expand description

Represents a whole RSA key, public and private parts.

Implementations

impl RSAPrivateKey[src]

pub fn new<R: Rng>(rng: &mut R, bit_size: usize) -> Result<RSAPrivateKey>[src]

Generate a new RSA key pair of the given bit size using the passed in rng.

pub fn from_components(
    n: BigUint,
    e: BigUint,
    d: BigUint,
    primes: Vec<BigUint>
) -> RSAPrivateKey
[src]

Constructs an RSA key pair from the individual components.

pub fn from_pkcs1(der: &[u8]) -> Result<RSAPrivateKey>[src]

Parse a PKCS1 encoded RSA Private Key.

The der data is expected to be the base64 decoded content following a -----BEGIN RSA PRIVATE KEY----- header.

https://tls.mbed.org/kb/cryptography/asn1-key-structures-in-der-and-pem

Example

use rsa::RSAPrivateKey;

let file_content = r#"
-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBAK5Z7jk1ql5DquRvlPmFgyBDCvdPQ0T2si2oPAUmNw2Z/qb2Sr/B
EBoWpagFf8Gl1K4PRipJSudDl6N/Vdb2CYkCAwEAAQJBAI3vWCfqsE8c9zoQPE8F
icHx0jOSq0ixLExO8M2gVqESq3SJpWbEbvPPbRb1sIqZHe5wV3Xmj09zvUzfdeB7
C6ECIQDjoB/kp7QlRiNhgudhQPct8XUf6Cgp7hBxL2K9Q9UzawIhAMQVvtH1TUOd
aSWiqrFx7w+54o58fIpkecI5Kl0TaWfbAiBrnye1Kn2IKhNMZWIUn2y+8izYeyGS
QZbQjQD4T3wcJQIgKGgWv2teNZ29ai0AIbrJuaLjhdsvStFzqctf6Hg0k1sCIQCj
JdwDGF7Kanex70KAacmOlw3vfx6XWT+2PH6Qh8tLug==
-----END RSA PRIVATE KEY-----
"#;

let der_encoded = file_content
    .lines()
    .filter(|line| !line.starts_with("-"))
    .fold(String::new(), |mut data, line| {
        data.push_str(&line);
        data
    });
let der_bytes = base64::decode(&der_encoded).expect("failed to decode base64 content");
let private_key = RSAPrivateKey::from_pkcs1(&der_bytes).expect("failed to parse key");

pub fn from_pkcs8(der: &[u8]) -> Result<RSAPrivateKey>[src]

Parse a PKCS8 encoded RSA Private Key.

The der data is expected to be the base64 decoded content following a -----BEGIN PRIVATE KEY----- header.

https://tls.mbed.org/kb/cryptography/asn1-key-structures-in-der-and-pem

Example

use rsa::RSAPrivateKey;

let file_content = r#"
-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEArlnuOTWqXkOq5G+U
+YWDIEMK909DRPayLag8BSY3DZn+pvZKv8EQGhalqAV/waXUrg9GKklK50OXo39V
1vYJiQIDAQABAkEAje9YJ+qwTxz3OhA8TwWJwfHSM5KrSLEsTE7wzaBWoRKrdIml
ZsRu889tFvWwipkd7nBXdeaPT3O9TN914HsLoQIhAOOgH+SntCVGI2GC52FA9y3x
dR/oKCnuEHEvYr1D1TNrAiEAxBW+0fVNQ51pJaKqsXHvD7nijnx8imR5wjkqXRNp
Z9sCIGufJ7UqfYgqE0xlYhSfbL7yLNh7IZJBltCNAPhPfBwlAiAoaBa/a141nb1q
LQAhusm5ouOF2y9K0XOpy1/oeDSTWwIhAKMl3AMYXspqd7HvQoBpyY6XDe9/HpdZ
P7Y8fpCHy0u6
-----END PRIVATE KEY-----
"#;

let der_encoded = file_content
    .lines()
    .filter(|line| !line.starts_with("-"))
    .fold(String::new(), |mut data, line| {
        data.push_str(&line);
        data
    });
let der_bytes = base64::decode(&der_encoded).expect("failed to decode base64 content");
let private_key = RSAPrivateKey::from_pkcs8(&der_bytes).expect("failed to parse key");

pub fn to_public_key(&self) -> RSAPublicKey[src]

Get the public key from the private key, cloning n and e.

Generally this is not needed since RSAPrivateKey implements the PublicKey trait, but it can occationally be useful to discard the private information entirely.

pub fn precompute(&mut self) -> Result<()>[src]

Performs some calculations to speed up private key operations.

pub fn d(&self) -> &BigUint[src]

Returns the private exponent of the key.

pub fn primes(&self) -> &[BigUint][src]

Returns the prime factors.

pub fn validate(&self) -> Result<()>[src]

Performs basic sanity checks on the key. Returns Ok(()) if everything is good, otherwise an approriate error.

pub fn decrypt(
    &self,
    padding: PaddingScheme,
    ciphertext: &[u8]
) -> Result<Vec<u8>>
[src]

Decrypt the given message.

pub fn decrypt_blinded<R: Rng>(
    &self,
    rng: &mut R,
    padding: PaddingScheme,
    ciphertext: &[u8]
) -> Result<Vec<u8>>
[src]

Decrypt the given message.

Uses rng to blind the decryption process.

pub fn sign(&self, padding: PaddingScheme, digest_in: &[u8]) -> Result<Vec<u8>>[src]

Sign the given digest.

pub fn sign_blinded<R: Rng>(
    &self,
    rng: &mut R,
    padding: PaddingScheme,
    digest_in: &[u8]
) -> Result<Vec<u8>>
[src]

Sign the given digest.

Use rng for blinding.

Methods from Deref<Target = RSAPublicKey>

Trait Implementations

impl Clone for RSAPrivateKey[src]

fn clone(&self) -> RSAPrivateKey[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for RSAPrivateKey[src]

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

Formats the value using the given formatter. Read more

impl Deref for RSAPrivateKey[src]

type Target = RSAPublicKey

The resulting type after dereferencing.

fn deref(&self) -> &RSAPublicKey[src]

Dereferences the value.

impl<'de> Deserialize<'de> for RSAPrivateKey[src]

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
    __D: Deserializer<'de>, 
[src]

Deserialize this value from the given Serde deserializer. Read more

impl Drop for RSAPrivateKey[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

impl From<&'_ RSAPrivateKey> for RSAPublicKey[src]

fn from(private_key: &RSAPrivateKey) -> Self[src]

Performs the conversion.

impl From<RSAPrivateKey> for RSAPublicKey[src]

fn from(private_key: RSAPrivateKey) -> Self[src]

Performs the conversion.

impl PartialEq<RSAPrivateKey> for RSAPrivateKey[src]

fn eq(&self, other: &RSAPrivateKey) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl PublicKeyParts for RSAPrivateKey[src]

fn n(&self) -> &BigUint[src]

Returns the modulus of the key.

fn e(&self) -> &BigUint[src]

Returns the public exponent of the key.

fn size(&self) -> usize[src]

Returns the modulus size in bytes. Raw signatures and ciphertexts for or by this public key will have the same size. Read more

impl<'a> PublicKeyParts for &'a RSAPrivateKey[src]

fn n(&self) -> &BigUint[src]

Returns the modulus of the key.

fn e(&self) -> &BigUint[src]

Returns the public exponent of the key.

fn size(&self) -> usize[src]

Returns the modulus size in bytes. Raw signatures and ciphertexts for or by this public key will have the same size. Read more

impl Serialize for RSAPrivateKey[src]

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
    __S: Serializer
[src]

Serialize this value into the given Serde serializer. Read more

impl TryFrom<Pem> for RSAPrivateKey[src]

fn try_from(pem: Pem) -> Result<RSAPrivateKey>[src]

Parses a PKCS8 or PKCS1 encoded RSA Private Key.

Expects one of the following pem headers:

  • -----BEGIN PRIVATE KEY-----
  • -----BEGIN RSA PRIVATE KEY-----

Example

use std::convert::TryFrom;
use rsa::RSAPrivateKey;

let file_content = r#"
-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBAK5Z7jk1ql5DquRvlPmFgyBDCvdPQ0T2si2oPAUmNw2Z/qb2Sr/B
EBoWpagFf8Gl1K4PRipJSudDl6N/Vdb2CYkCAwEAAQJBAI3vWCfqsE8c9zoQPE8F
icHx0jOSq0ixLExO8M2gVqESq3SJpWbEbvPPbRb1sIqZHe5wV3Xmj09zvUzfdeB7
C6ECIQDjoB/kp7QlRiNhgudhQPct8XUf6Cgp7hBxL2K9Q9UzawIhAMQVvtH1TUOd
aSWiqrFx7w+54o58fIpkecI5Kl0TaWfbAiBrnye1Kn2IKhNMZWIUn2y+8izYeyGS
QZbQjQD4T3wcJQIgKGgWv2teNZ29ai0AIbrJuaLjhdsvStFzqctf6Hg0k1sCIQCj
JdwDGF7Kanex70KAacmOlw3vfx6XWT+2PH6Qh8tLug==
-----END RSA PRIVATE KEY-----
"#;

let pem = rsa::pem::parse(file_content).expect("failed to parse pem file");
let private_key = RSAPrivateKey::try_from(pem).expect("failed to parse key");

type Error = Error

The type returned in the event of a conversion error.

impl Zeroize for RSAPrivateKey[src]

fn zeroize(&mut self)[src]

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler. Read more

impl Eq for RSAPrivateKey[src]

Auto Trait Implementations

impl RefUnwindSafe for RSAPrivateKey

impl Send for RSAPrivateKey

impl Sync for RSAPrivateKey

impl Unpin for RSAPrivateKey

impl UnwindSafe for RSAPrivateKey

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T[src]

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 
[src]

pub fn vzip(self) -> V[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]