Struct rsa::RSAPublicKey[][src]

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

Represents the public part of an RSA key.

Implementations

Create a new key from its components.

Parse a PKCS1 encoded RSA Public Key.

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

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

Example

use rsa::RSAPublicKey;

let file_content = r#"
-----BEGIN RSA PUBLIC KEY-----
MEgCQQCuWe45NapeQ6rkb5T5hYMgQwr3T0NE9rItqDwFJjcNmf6m9kq/wRAaFqWo
BX/BpdSuD0YqSUrnQ5ejf1XW9gmJAgMBAAE=
-----END RSA PUBLIC 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 public_key = RSAPublicKey::from_pkcs1(&der_bytes).expect("failed to parse key");

Parse a PKCS8 encoded RSA Public Key.

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

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

Example

use rsa::RSAPublicKey;

let file_content = r#"
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK5Z7jk1ql5DquRvlPmFgyBDCvdPQ0T2
si2oPAUmNw2Z/qb2Sr/BEBoWpagFf8Gl1K4PRipJSudDl6N/Vdb2CYkCAwEAAQ==
-----END PUBLIC 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 public_key = RSAPublicKey::from_pkcs8(&der_bytes).expect("failed to parse key");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

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

This method tests for !=.

Encrypt the given message.

Verify a signed message. hashedmust be the result of hashing the input using the hashing function passed in through hash. If the message is valid Ok(()) is returned, otherwiese an Err indicating failure. Read more

Encrypt the given message.

Verify a signed message. hashedmust be the result of hashing the input using the hashing function passed in through hash. If the message is valid Ok(()) is returned, otherwiese an Err indicating failure. Read more

Encodes a Public key to into PKCS1 bytes. Read more

Encodes a Public key to into PKCS8 bytes. Read more

Returns the modulus of the key.

Returns the public exponent of the key.

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

Returns the modulus of the key.

Returns the public exponent of the key.

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

Converts a Public key into PKCS1 encoded bytes in pem format. Read more

Converts a Public key into PKCS1 encoded bytes in pem format with encoding config. Read more

Converts a Public key into PKCS8 encoded bytes in pem format. Read more

Converts a Public key into PKCS8 encoded bytes in pem format with encoding config. Read more

Parses a PKCS8 or PKCS1 encoded RSA Public Key.

Expects one of the following pem headers:

  • -----BEGIN PUBLIC KEY-----
  • -----BEGIN RSA PUBLIC KEY-----

Example

use std::convert::TryFrom;
use rsa::RSAPublicKey;

let file_content = r#"
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK5Z7jk1ql5DquRvlPmFgyBDCvdPQ0T2
si2oPAUmNw2Z/qb2Sr/BEBoWpagFf8Gl1K4PRipJSudDl6N/Vdb2CYkCAwEAAQ==
-----END PUBLIC KEY-----
"#;

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

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

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

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.