1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
use crate::prelude::*;
use crate::Method;
use std::sync::Arc;
use std::str::FromStr;
use async_trait::async_trait;
use super::ALPHABET;
#[derive(Clone, Debug, Default)]
#[cfg_attr(featue = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(test, derive(PartialEq))]
pub struct Vigenere;
impl FromStr for Vigenere {
type Err = Error;
fn from_str(input: &str) -> Result<Self> {
if input.to_uppercase() == "VIGENERE" {
Ok(Self)
} else {
Err(Error::InvalidMethodError(input.to_string()))
}
}
}
#[async_trait]
impl Method for Vigenere {
async fn encrypt(&self, uw: Arc<str>, vw: Arc<str>) -> Result<String> {
// Getting the unique variable pass
let unique = uw.to_lowercase();
let variable = vw.to_lowercase();
// Creating the new pass and initializing it empity
let mut new_pass = String::new();
// Counter to control valid characters on the alphabet
let mut i = 0;
// Loop to set the new characters to the new password
for c in unique.chars() {
// Get the index of the current character on variable pass.
// This formula is to get the value even when the unique pass length is larger than variable
// pass length.
let variable_index = if i < variable.len() {
i
} else {
i - (variable.len() * (i / variable.len()))
};
// Just an alias for the alphabet lenght as a i8
let alphabet_len = ALPHABET.len() as i8;
// Get the index of the current unique pass character from the alphabet.
// If there is some character that is not in the alphabet, like a special character or number, just append
// it to the new pass.
let pos_u = match ALPHABET.iter().position(|&s| s == c) {
Some(u) => u as i8,
None => {
new_pass.push(c);
continue;
}
};
// Get the index of the current variable pass character from the alphabet. If the character
// is not on the defined alphabet it returns a error since every character here should
// match a valid character on the unique pass.
let pos_v: i8 = ALPHABET
.iter()
.position(|&s| s == variable.as_bytes()[variable_index] as char)
.ok_or(Error::InvalidCharacterError)?
.try_into()
.unwrap();
// Get the index of the new charater on the alphabet based on the unique and variable pass.
let mut position = pos_u - alphabet_len + pos_v;
if position < 0 {
position += alphabet_len;
}
// Get the new character on the alphabet.
let new_character = ALPHABET[position as usize];
i += 1;
new_pass.push(new_character);
}
Ok(new_pass)
}
}