Crate rsa_export

Source
Expand description

rsa_export allows you to export your RSA key, generated via the rsa crate, with PKCS#1 or PKCS#8 encoding

Note: Multi-prime keys are not supported

The keys can also be exported into the PEM format by enabling the feature pem

Example:

use rsa::RSAPrivateKey;
use rand::rngs::OsRng;
use rsa_export::Encode;

let mut rng = OsRng;
let private_key = RSAPrivateKey::new(&mut rng, 2048).unwrap();
let public_key = private_key.to_public_key();

let pkcs1_encoded_private = private_key.as_pkcs1().unwrap();
let pkcs1_encoded_public = public_key.as_pkcs1().unwrap();

let pkcs8_encoded_private = private_key.as_pkcs8().unwrap();
let pkcs8_encoded_public = public_key.as_pkcs8().unwrap();

Encode PKCS#1 or PKCS#8 encoded keys into the PEM format

use rsa::RSAPrivateKey;
use rand::rngs::OsRng;
use rsa_export::PemEncode;

let mut rng = OsRng;
let private_key = RSAPrivateKey::new(&mut rng, 2048).unwrap();
let public_key = private_key.to_public_key();

let pkcs1_encoded_private_pem = private_key.as_pkcs1_pem().unwrap();
let pkcs1_encoded_public_pem = public_key.as_pkcs1_pem().unwrap();

let pkcs8_encoded_private_pem = private_key.as_pkcs8_pem().unwrap();
let pkcs8_encoded_public_pem = public_key.as_pkcs8_pem().unwrap();

Encode PKCS#1 or PKCS#8 encoded keys into the PEM format with a custom line ending

use rsa::RSAPrivateKey;
use rsa_export::{PemEncode, LineEnding};
use rand::rngs::OsRng;

let mut rng = OsRng;
let private_key = RSAPrivateKey::new(&mut rng, 2048).unwrap();
let public_key = private_key.to_public_key();

let pkcs1_encoded_private_pem = private_key
    .as_pkcs1_pem_custom_ending(LineEnding::CRLF)
    .unwrap();
let pkcs1_encoded_public_pem = public_key
    .as_pkcs1_pem_custom_ending(LineEnding::CRLF)
    .unwrap();

let pkcs8_encoded_private_pem = private_key
    .as_pkcs1_pem_custom_ending(LineEnding::CRLF)
    .unwrap();
let pkcs8_encoded_public_pem = public_key
   .as_pkcs1_pem_custom_ending(LineEnding::CRLF)
   .unwrap();

Modules§

Enums§

Traits§

  • Trait for encoding the keys
  • Trait for encoding the keys and wrapping them in the PEM format