Oaep

Struct Oaep 

Source
pub struct Oaep {
    pub digest: Box<dyn DynDigest + Send + Sync>,
    pub mgf_digest: Box<dyn DynDigest + Send + Sync>,
    pub label: Option<String>,
}
Expand description

Encryption and Decryption using OAEP padding.

  • digest is used to hash the label. The maximum possible plaintext length is m = k - 2 * h_len - 2, where k is the size of the RSA modulus.
  • mgf_digest specifies the hash function that is used in the MGF1.
  • label is optional data that can be associated with the message.

The two hash functions can, but don’t need to be the same.

A prominent example is the AndroidKeyStore. It uses SHA-1 for mgf_digest and a user-chosen SHA flavour for digest.

Fields§

§digest: Box<dyn DynDigest + Send + Sync>

Digest type to use.

§mgf_digest: Box<dyn DynDigest + Send + Sync>

Digest to use for Mask Generation Function (MGF).

§label: Option<String>

Optional label.

Implementations§

Source§

impl Oaep

Source

pub fn new<T: 'static + Digest + DynDigest + Send + Sync>() -> Self

Create a new OAEP PaddingScheme, using T as the hash function for both the default (empty) label and for MGF1.

§Example
use sha1::Sha1;
use sha2::Sha256;
use rsa::{BigUint, RsaPublicKey, Oaep, };
use base64ct::{Base64, Encoding};

let n = Base64::decode_vec("ALHgDoZmBQIx+jTmgeeHW6KsPOrj11f6CvWsiRleJlQpW77AwSZhd21ZDmlTKfaIHBSUxRUsuYNh7E2SHx8rkFVCQA2/gXkZ5GK2IUbzSTio9qXA25MWHvVxjMfKSL8ZAxZyKbrG94FLLszFAFOaiLLY8ECs7g+dXOriYtBwLUJK+lppbd+El+8ZA/zH0bk7vbqph5pIoiWggxwdq3mEz4LnrUln7r6dagSQzYErKewY8GADVpXcq5mfHC1xF2DFBub7bFjMVM5fHq7RK+pG5xjNDiYITbhLYrbVv3X0z75OvN0dY49ITWjM7xyvMWJXVJS7sJlgmCCL6RwWgP8PhcE=").unwrap();
let e = Base64::decode_vec("AQAB").unwrap();

let mut rng = rand::thread_rng(); // rand@0.8
let key = RsaPublicKey::new(BigUint::from_bytes_be(&n), BigUint::from_bytes_be(&e)).unwrap();
let padding = Oaep::new::<Sha256>();
let encrypted_data = key.encrypt(&mut rng, padding, b"secret").unwrap();
Source

pub fn new_with_label<T: 'static + Digest + DynDigest + Send + Sync, S: AsRef<str>>( label: S, ) -> Self

Create a new OAEP PaddingScheme with an associated label, using T as the hash function for both the label and for MGF1.

Source

pub fn new_with_mgf_hash<T: 'static + Digest + DynDigest + Send + Sync, U: 'static + Digest + DynDigest + Send + Sync>() -> Self

Create a new OAEP PaddingScheme, using T as the hash function for the default (empty) label, and U as the hash function for MGF1. If a label is needed use PaddingScheme::new_oaep_with_label or PaddingScheme::new_oaep_with_mgf_hash_with_label.

§Example
use sha1::Sha1;
use sha2::Sha256;
use rsa::{BigUint, RsaPublicKey, Oaep, };
use base64ct::{Base64, Encoding};

let n = Base64::decode_vec("ALHgDoZmBQIx+jTmgeeHW6KsPOrj11f6CvWsiRleJlQpW77AwSZhd21ZDmlTKfaIHBSUxRUsuYNh7E2SHx8rkFVCQA2/gXkZ5GK2IUbzSTio9qXA25MWHvVxjMfKSL8ZAxZyKbrG94FLLszFAFOaiLLY8ECs7g+dXOriYtBwLUJK+lppbd+El+8ZA/zH0bk7vbqph5pIoiWggxwdq3mEz4LnrUln7r6dagSQzYErKewY8GADVpXcq5mfHC1xF2DFBub7bFjMVM5fHq7RK+pG5xjNDiYITbhLYrbVv3X0z75OvN0dY49ITWjM7xyvMWJXVJS7sJlgmCCL6RwWgP8PhcE=").unwrap();
let e = Base64::decode_vec("AQAB").unwrap();

let mut rng = rand::thread_rng(); // rand@0.8
let key = RsaPublicKey::new(BigUint::from_bytes_be(&n), BigUint::from_bytes_be(&e)).unwrap();
let padding = Oaep::new_with_mgf_hash::<Sha256, Sha1>();
let encrypted_data = key.encrypt(&mut rng, padding, b"secret").unwrap();
Source

pub fn new_with_mgf_hash_and_label<T: 'static + Digest + DynDigest + Send + Sync, U: 'static + Digest + DynDigest + Send + Sync, S: AsRef<str>>( label: S, ) -> Self

Create a new OAEP PaddingScheme with an associated label, using T as the hash function for the label, and U as the hash function for MGF1.

Trait Implementations§

Source§

impl Debug for Oaep

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PaddingScheme for Oaep

Source§

fn decrypt<Rng: CryptoRngCore>( self, rng: Option<&mut Rng>, priv_key: &RsaPrivateKey, ciphertext: &[u8], ) -> Result<Vec<u8>>

Decrypt the given message using the given private key. Read more
Source§

fn encrypt<Rng: CryptoRngCore>( self, rng: &mut Rng, pub_key: &RsaPublicKey, msg: &[u8], ) -> Result<Vec<u8>>

Encrypt the given message using the given public key.

Auto Trait Implementations§

§

impl Freeze for Oaep

§

impl !RefUnwindSafe for Oaep

§

impl Send for Oaep

§

impl Sync for Oaep

§

impl Unpin for Oaep

§

impl !UnwindSafe for Oaep

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 56 bytes