Struct onetimepad::OneTimePad

source ·
pub struct OneTimePad { /* private fields */ }
Expand description

A struct containing the state of a one time pad. It contains a buffer of pad characters which is used to encode and decode strings.

Encoding

To encode a string with the default alphabet:

use onetimepad::OneTimePad;

let mut otp = OneTimePad::new();
otp.push_to_pad("8t5l!Ok2v$q4e3/S3dOLztDY").unwrap();
let res = otp.encode("Never gonna give you up.").unwrap();
println!("{}", res.cipher_text);

Decoding

To decode a string with the default alphabet:

use onetimepad::OneTimePad;

let mut otp = OneTimePad::new();
otp.push_to_pad("kgx:?exP2B8").unwrap();
let res = otp.decode("g2Vt1~.UjTq").unwrap();
println!("{}", res);

Implementations§

source§

impl OneTimePad

source

pub fn new() -> Self

Create a new OneTimePad instance with the default alphabet, which covers ASCII, except control characters.

source

pub fn new_with_alphabet<S: AsRef<str>>(alphabet: S) -> Self

Create a new OneTimePad instance with a custom alphabet. The first character in the string will be numbered 0, and the numeric representation will increase with the character index.

source

pub fn push_to_pad<S: AsRef<str>>( &mut self, extra_pad_characters: S ) -> Result<(), OneTimePadError>

Push a string of characters to the end of the pad buffer. This will return a OneTimePadError::CharacterNotInAlphabet error if any of the characters are not in the alphabet for this one time pad.

source

pub fn clear_pad(&mut self)

Empty the pad buffer completely.

source

pub fn encode<S: AsRef<str>>( &mut self, plain_text: S ) -> Result<EncodingResult, OneTimePadError>

Encode a string to ciphertext.

The following requirements must be met for this to succeed:

In the event that an error is returned, the pad will not have been changed.

source

pub fn decode<S: AsRef<str>>( &mut self, cipher_text: S ) -> Result<String, OneTimePadError>

Encode ciphertext to plain text.

The following requirements must be met for this to succeed:

In the event that an error is returned, the pad will not have been changed.

Trait Implementations§

source§

impl Clone for OneTimePad

source§

fn clone(&self) -> OneTimePad

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

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

§

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 Twhere U: TryFrom<T>,

§

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.