Skip to content

Commit

Permalink
Add more tests & make inverse output a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Killarexe committed Jan 10, 2024
1 parent ee487d8 commit 60b1034
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ I Know this is maybe a stupid idea but i made a encryptor that do shift carry op

## Usage:

| Input | Direction | Nb of iterations | Output file *(optional)* |
|-------|-----------|------------------|--------------------------|
|**File path** or **String value**| *"left"* or *"right"* | 1-7 | A file path |
| Input | Direction | Nb of iterations | Output file *(optional)* | Inverse output *(optional, ***false by default***)* |
|-------|-----------|------------------|--------------------------|-----------------------------------------------------|
|**File path** or **String value**| *"left"* or *"right"* | 1-7 | A file path | *"true"* or "false" |

**Warning:**

Expand All @@ -30,5 +30,5 @@ To decrypt...:

The **S**hift **C**arry **E**ncryptor*(SCE in short)* have 2 steps of bit manipulation per bytes in the input:

1. Invert the byte *(e.g: `0110` -> `1001`)*
1. Invert the byte **If `Inverse Output` is set to `true` in the arguments!** *(e.g: `0110` -> `1001`)*
2. Shift Carry depending in the direction given *(e.g: Shift carry left 1 -> `1001` -> `0011` | Shift carry right 1 -> `1101` -> `1110`)*
8 changes: 7 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct Args {
/// Number of iterations.
pub iterations: u8,
/// Output file.
pub output_path: Option<PathBuf>
pub output_path: Option<PathBuf>,
// Invert the output.
invert_output: Option<bool>
}

impl Args {
Expand All @@ -37,4 +39,8 @@ impl Args {
None
}
}

pub fn get_invert_output(&self) -> bool {
self.invert_output.unwrap_or(false)
}
}
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ pub fn shift_carry_right(value: u8, iterations: u8) -> u8 {
result
}

pub fn process_input(input: Vec<u8>, iterations: u8, direction: bool) -> Vec<u8>{
pub fn process_input(input: Vec<u8>, iterations: u8, direction: bool, invert: bool) -> Vec<u8>{
if direction {
return input.iter().map(|&value| !shift_carry_left(value, iterations)).collect();
return input.iter().map(|&value| if invert {
!shift_carry_left(value, iterations)
} else {
shift_carry_left(value, iterations)
}).collect();
}
input.iter().map(|&value| shift_carry_right(!value, iterations)).collect()
input.iter().map(|&value| if invert {
shift_carry_right(!value, iterations)
} else {
shift_carry_right(value, iterations)
}).collect()
}

pub fn process_output(output: Vec<u8>, output_path: Option<PathBuf>) {
Expand All @@ -57,7 +65,7 @@ fn main() {
let mut args: Args = Args::parse();
match args.get_direction() {
Some(direction) => {
let output: Vec<u8> = process_input(args.get_input(), args.iterations, direction);
let output: Vec<u8> = process_input(args.get_input(), args.iterations, direction, args.get_invert_output());
process_output(output, args.output_path);
},
None => {
Expand Down
22 changes: 20 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,29 @@ fn single_input() {
assert_eq!(input, shift_carry_right(output, iterations));
}

#[test]
fn single_input_with_inversion() {
let input: u8 = 0b01101110;
let iterations: u8 = 2;
let output: u8 = !shift_carry_left(input, iterations);
assert_eq!(output, !0b10111001);
assert_eq!(input, shift_carry_right(!output, iterations));
}

#[test]
fn multiple_inputs() {
let inputs: Vec<u8> = vec![0b10010101, 0b11101001, 0b11101011, 0b11000010];
let iterations: u8 = 2;
let outputs: Vec<u8> = process_input(inputs.clone(), iterations, true, false);
assert_eq!(outputs, vec![0b01010110, 0b10100111, 0b10101111, 0b00001011]);
assert_eq!(inputs, process_input(outputs, iterations, false, false));
}

#[test]
fn multiple_inputs_with_inversion() {
let inputs: Vec<u8> = vec![0b10010101, 0b11101001, 0b11101011, 0b11000010];
let iterations: u8 = 2;
let outputs: Vec<u8> = process_input(inputs.clone(), iterations, true);
let outputs: Vec<u8> = process_input(inputs.clone(), iterations, true, true);
assert_eq!(outputs, vec![!0b01010110, !0b10100111, !0b10101111, !0b00001011]);
assert_eq!(inputs, process_input(outputs, iterations, false));
assert_eq!(inputs, process_input(outputs, iterations, false, true));
}

0 comments on commit 60b1034

Please sign in to comment.