#[cfg(feature = "shift_cipher")]
pub mod shift_cipher;
use super::{
CryptoError,
CryptographicAtom,
};
pub trait Cipher: CryptographicAtom + Decrypt + Encrypt + Key {}
pub trait Decrypt {
fn decrypt(
&mut self,
ciphertext: &Vec<u8>,
) -> Result<Vec<u8>, CryptoError>;
}
pub trait Encrypt {
fn encrypt(
&mut self,
plaintext: &Vec<u8>,
) -> Result<Vec<u8>, CryptoError>;
}
pub trait Key {
fn get_key(
&self
) -> Option<&Vec<u8>>;
fn set_key(
&mut self,
key: &Vec<u8>,
) -> Result<(), CryptoError>;
}
#[cfg(any(feature = "shift_cipher", test))]
fn shift_byte(
value: u8,
distance: u8,
shift_left: bool,
modulo: u8,
) -> u8 {
let value = value % modulo;
let distance = distance % modulo;
if shift_left {
(value + (modulo - distance)) % modulo
} else {
(value + distance) % modulo
}
}
#[cfg(any(test, feature = "doc_tests"))]
mod tests {
use super::*;
#[cfg_attr(not(feature = "doc_tests"), test)]
fn test_01() {
assert_eq![shift_byte(7, 3, true, 5), 4];
}
#[cfg_attr(not(feature = "doc_tests"), test)]
fn test_02() {
assert_eq![shift_byte(3, 4, true, 255), 254];
}
#[cfg_attr(not(feature = "doc_tests"), test)]
fn test_03() {
assert_eq![shift_byte(7, 37, true, 13), 9]
}
#[cfg_attr(not(feature = "doc_tests"), test)]
fn test_04() {
assert_eq![shift_byte(1, 100, false, 255), 101];
}
#[cfg_attr(not(feature = "doc_tests"), test)]
fn test_05() {
assert_eq![shift_byte(200, 55, false, 201), 54];
}
#[cfg_attr(not(feature = "doc_tests"), test)]
fn test_06() {
assert_eq![shift_byte(255, 105, false, 100), 60];
}
}