Crate rsa[][src]

Expand description

RSA Implementation in pure Rust.

Usage

Using PKCS1v15.

use rsa::{PublicKey, RSAPrivateKey, RSAPublicKey, PaddingScheme};
use rand::rngs::OsRng;

let mut rng = OsRng;
let bits = 2048;
let private_key = RSAPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let public_key = RSAPublicKey::from(&private_key);

// Encrypt
let data = b"hello world";
let padding = PaddingScheme::new_pkcs1v15_encrypt();
let enc_data = public_key.encrypt(&mut rng, padding, &data[..]).expect("failed to encrypt");
assert_ne!(&data[..], &enc_data[..]);

// Decrypt
let padding = PaddingScheme::new_pkcs1v15_encrypt();
let dec_data = private_key.decrypt(padding, &enc_data).expect("failed to decrypt");
assert_eq!(&data[..], &dec_data[..]);

Using OAEP.

use rsa::{PublicKey, RSAPrivateKey, RSAPublicKey, PaddingScheme};
use rand::rngs::OsRng;

let mut rng = OsRng;
let bits = 2048;
let private_key = RSAPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let public_key = RSAPublicKey::from(&private_key);

// Encrypt
let data = b"hello world";
let padding = PaddingScheme::new_oaep::<sha2::Sha256>();
let enc_data = public_key.encrypt(&mut rng, padding, &data[..]).expect("failed to encrypt");
assert_ne!(&data[..], &enc_data[..]);

// Decrypt
let padding = PaddingScheme::new_oaep::<sha2::Sha256>();
let dec_data = private_key.decrypt(padding, &enc_data).expect("failed to decrypt");
assert_eq!(&data[..], &dec_data[..]);

Re-exports

pub use pem;
pub use self::hash::Hash;
pub use self::padding::PaddingScheme;

Modules

algorithms

Useful algorithms.

errors

Error types.

hash

Supported hash functions.

internals

Internal raw RSA functions.

padding

Supported padding schemes.

Structs

BigUint

A big unsigned integer type.

RSAPrivateKey

Represents a whole RSA key, public and private parts.

RSAPublicKey

Represents the public part of an RSA key.

Traits

PublicKey

Generic trait for operations on a public key.

PublicKeyParts