Skip to main content

MessageEncryption

Struct MessageEncryption 

Source
pub struct MessageEncryption { /* private fields */ }
Expand description

A storage container that represents a message you want to encrypt/decrypt. In order for both operations to work, you also need to store the encryption key and additional authenticated data (plaintext).

§Examples

You can create a MessageEncryption using the following code:

use street_cred::MessageEncryption;

let message = b"secret message".to_vec();
let key = "425D76994EE6101105DDDA2EE2604AA0";
let aad = "additional authenticated data";
let encryptor = MessageEncryption::new(message, key, aad);

Implementations§

Source§

impl MessageEncryption

Source

pub fn new(message: Vec<u8>, key: &str, aad: &str) -> Self

Create a new instance of MessageEncryption

§Arguments
  • message - Message to be encrypted
  • key - Key to use for encryption/decryption
  • aad - Additional authenticated data
§Examples
use street_cred::MessageEncryption;

let message = b"secret message".to_vec();
let key = "425D76994EE6101105DDDA2EE2604AA0";
let aad = "additional authenticated data";
let encryptor = MessageEncryption::new(message, key, aad);
Source

pub fn decrypt(&self, iv: &str, tag: &str) -> Result<String>

Decrypts the contents of the MessageEncryption and returns them as a String

§Arguments
  • iv - Initialization vector used when initially encrypting the message
  • tag - Additional Authenticated data resulting from encrypting the message
§Examples
use street_cred::MessageEncryption;

let encrypted_message = b"".to_vec();
let key = "425D76994EE6101105DDDA2EE2604AA0";
let plaintext_aad = "";
let iv = "fWoW3cyLE2/JfiiF";
let tag = "DyMEJPXzmksJGb+QumM2Rd6X";

let decryptor = MessageEncryption::new(encrypted_message, key, plaintext_aad);
let decrypted_contents = decryptor.decrypt(iv, tag);

match decrypted_contents {
  Ok(contents) => println!("Decrypted Contents: {}", contents),
  Err(why) => println!("Error: {}", why),
}
Source

pub fn encrypt(&self) -> Result<String>

Encrypts the contents of the MessageEncryption and returns them as a String

§Examples
use street_cred::MessageEncryption;

let plaintext_message = b"super secret message".to_vec();
let key = "16 byte key line";
let plaintext_aad = "";
let encryptor = MessageEncryption::new(plaintext_message, key, plaintext_aad);
let encrypted_contents = encryptor.encrypt();

match encrypted_contents {
  Ok(contents) => println!("Encrypted Contents: {}", contents),
  Err(why) => println!("Error: {}", why),
}
Source

pub fn split_encrypted_contents(contents: &str) -> Result<Vec<&str>>

Split contents of an encrypted file into a Vec with a length of 3. The first index is the encrypted contents, the second index is the initialization vector, and the third index is the additional authenticated data.

§Arguments
  • contents - The entire encrypted file as one long string. Encrypted contents should be formatted like this: “message–iv–aad”
§Examples
use street_cred::MessageEncryption;

let encrypted_contents = "HPxd1UcM3cH+Rt0HaIOFzdHqIPWIc3yR--/EoLW7ichWLzLh3V--7L1L8uPH7LoQYLkEfIckgA==";
let split_parts = MessageEncryption::split_encrypted_contents(encrypted_contents);

Auto Trait Implementations§

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.