Skip to content

Commit

Permalink
update (salt extender): create a salt and string extender
Browse files Browse the repository at this point in the history
  • Loading branch information
lumbrjx committed Jan 19, 2024
1 parent 8666528 commit 61e11ab
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/encpt/mapping/mapper.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::error::Error;
use std::fmt;

use crate::maps::chars::*;
use crate::maps::salt::*;

Expand All @@ -7,6 +10,7 @@ pub enum MpType {
SaltMap,
}

// map strings to vectors
pub fn chr_to_mp(vc: Vec<&str>, mpt: MpType) -> Result<Vec<&str>, &str> {
let mut result: Vec<&str> = vec![];
let mpp: [[&str; 3]; 85];
Expand All @@ -29,6 +33,59 @@ pub fn chr_to_mp(vc: Vec<&str>, mpt: MpType) -> Result<Vec<&str>, &str> {
}
}

// extend salt based on string length
#[derive(Debug)]
struct EmptyValueError;

impl fmt::Display for EmptyValueError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "EmptyValueError: can't accept empty values")
}
}

impl Error for EmptyValueError {}

struct ExtValue {
longer: String,
shorter: String,
}
impl ExtValue {
fn ext_data(&self) -> String {
let extend_size = &self.longer.len() - &self.shorter.len();
let mut chunk: String = String::from(&self.shorter);
while extend_size > chunk.len() {
chunk = chunk + &self.shorter;
}
let trimmed = &chunk[..extend_size];
let concated = self.shorter.clone() + trimmed;
concated
}
}

pub fn salt_extender(salt: String, password: String) -> Result<String, Box<dyn Error>> {
if salt.is_empty() || password.is_empty() {
return Err(Box::new(EmptyValueError));
}

if salt.len() > password.len() {
let res = ExtValue {
longer: salt,
shorter: password,
};
return Ok(res.ext_data());
}

if salt.len() < password.len() {
let res = ExtValue {
longer: password,
shorter: salt,
};
return Ok(res.ext_data());
}

Ok(salt)
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -38,4 +95,55 @@ mod tests {
let res = chr_to_mp(vec!["A", "B", "C"], MpType::CharMap);
assert_eq!(res, Ok(vec!["Av", "bQ", "TG"]))
}
#[test]
fn salt_extender_longer() {
let longer = String::from("abc");
let shorter = String::from("dsdfsqdfsqdff");
match salt_extender(longer.clone(), shorter.clone()) {
Ok(result) => {
let expected = String::from("abcabcabcabca");
assert_eq!(result, expected);
}
Err(err) => panic!("Unexpected error: {}", err),
}
}

#[test]
fn salt_extender_shorter() {
let longer = String::from("abc");
let shorter = String::from("dsdfsqdfsqdff");
match salt_extender(shorter.clone(), longer.clone()) {
Ok(result) => {
let expected = String::from("abcabcabcabca");
assert_eq!(result, expected);
}
Err(err) => panic!("Unexpected error: {}", err),
}
}
#[test]
fn salt_extender_even() {
let longer = String::from("dsdfsqdfsqdff");
let shorter = String::from("dsdfsqdfsqdff");
match salt_extender(shorter.clone(), longer.clone()) {
Ok(result) => {
let expected = String::from("dsdfsqdfsqdff");
assert_eq!(result, expected);
}
Err(err) => panic!("Unexpected error: {}", err),
}
}

#[test]
#[should_panic]
fn salt_extender_empty() {
let longer = String::from("");
let shorter = String::from("");
match salt_extender(shorter.clone(), longer.clone()) {
Ok(result) => {
let expected = String::from("dsdfsqdfsqdff");
assert_eq!(result, expected);
}
Err(err) => panic!("Unexpected error: {}", err),
}
}
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ mod encpt {
pub mod mapper;
}
}

fn main() {
let _ = chr_to_mp(vec!["A", "B", "C"], MpType::CharMap);
match salt_extender(String::from("abc"), String::from("dsdfsqdfsqdff")) {
Ok(result) => println!("Result: {}", result),
Err(err) => eprintln!("Error: {}", err),
}
}

0 comments on commit 61e11ab

Please sign in to comment.