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
impl OneTimePad
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new OneTimePad
instance with the default alphabet, which
covers ASCII, except control characters.
sourcepub fn new_with_alphabet<S: AsRef<str>>(alphabet: S) -> Self
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.
sourcepub fn push_to_pad<S: AsRef<str>>(
&mut self,
extra_pad_characters: S
) -> Result<(), OneTimePadError>
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.
sourcepub fn encode<S: AsRef<str>>(
&mut self,
plain_text: S
) -> Result<EncodingResult, OneTimePadError>
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:
- The pad must contain at least the same number of characters as the
input string, otherwise a
OneTimePadError::PadBufferNotLongEnough
will be returned. - The input text must not contain any characters not in the alphabet,
otherwise a
OneTimePadError::CharacterNotInAlphabet
will be returned.
In the event that an error is returned, the pad will not have been changed.
sourcepub fn decode<S: AsRef<str>>(
&mut self,
cipher_text: S
) -> Result<String, OneTimePadError>
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:
- The pad must contain at least the same number of characters as the
input string, otherwise a
OneTimePadError::PadBufferNotLongEnough
will be returned. - The input text must not contain any characters not in the alphabet,
otherwise a
OneTimePadError::CharacterNotInAlphabet
will be returned.
In the event that an error is returned, the pad will not have been changed.
Trait Implementations§
source§impl Clone for OneTimePad
impl Clone for OneTimePad
source§fn clone(&self) -> OneTimePad
fn clone(&self) -> OneTimePad
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more