Struct rsa::RSAPrivateKey [−][src]
pub struct RSAPrivateKey { /* fields omitted */ }
Expand description
Represents a whole RSA key, public and private parts.
Implementations
Generate a new RSA key pair of the given bit size using the passed in rng
.
pub fn new_with_exp<R: Rng>(
rng: &mut R,
bit_size: usize,
exp: &BigUint
) -> Result<RSAPrivateKey>
pub fn new_with_exp<R: Rng>(
rng: &mut R,
bit_size: usize,
exp: &BigUint
) -> Result<RSAPrivateKey>
Generate a new RSA key pair of the given bit size and the public exponent
using the passed in rng
.
Unless you have specific needs, you should use RSAPrivateKey::new
instead.
pub fn from_components(
n: BigUint,
e: BigUint,
d: BigUint,
primes: Vec<BigUint>
) -> RSAPrivateKey
pub fn from_components(
n: BigUint,
e: BigUint,
d: BigUint,
primes: Vec<BigUint>
) -> RSAPrivateKey
Constructs an RSA key pair from the individual components.
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");
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");
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.
Performs some calculations to speed up private key operations.
Performs basic sanity checks on the key.
Returns Ok(())
if everything is good, otherwise an approriate error.
Decrypt the given message.
Decrypt the given message.
Uses rng
to blind the decryption process.
Sign the given digest.
Methods from Deref<Target = RSAPublicKey>
Trait Implementations
type Target = RSAPublicKey
type Target = RSAPublicKey
The resulting type after dereferencing.
Dereferences the value.
Performs the conversion.
Performs the conversion.
Converts a Private key into PKCS1
encoded bytes in pem format. Read more
Converts a Private key into PKCS1
encoded bytes in pem format with encoding config. Read more
Converts a Private key into PKCS8
encoded bytes in pem format. Read more
Converts a Private key into PKCS8
encoded bytes in pem format with encoding config. Read more
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");
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
Mutably borrows from an owned value. Read more
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V