shuffle

Function shuffle 

Source
pub fn shuffle(key: &str, alphabet: &str) -> String
Expand description

Shuffles an alphabet using a keyword to create a mixed alphabet for cipher use.

§Algorithm

  1. Condense the concatenation of key and alphabet to remove duplicates
  2. Calculate dimensions: length = condensed key length, height = alphabet length / length
  3. Extract characters in a specific pattern based on these dimensions, working backwards through columns and forwards through rows

§Arguments

  • key - The keyword used to shuffle the alphabet (will be condensed to remove duplicates)
  • alphabet - The alphabet string to be shuffled

§Returns

A new String containing the shuffled alphabet

§Examples

Regular rectangle (key length divides alphabet evenly):

use old_crypto_rs::helpers::shuffle;

let key = "ARABESQUE";  // Condenses to "ARBESQU" (length = 7)
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ/-";  // 28 chars, height = 4
let result = shuffle(key, alphabet);
assert_eq!(result, "ACKVRDLWBFMXEGNYSHOZQIP/UJT-");

Irregular rectangle (key length does not divide alphabet evenly):

use old_crypto_rs::helpers::shuffle;

let key = "SUBWAY";  // Condenses to "SUBWAY" (length = 6)
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ/-";  // 28 chars, height = 5
let result = shuffle(key, alphabet);
assert_eq!(result, "SCIOXUDJPZBEKQ/WFLR-AGMTYHNV");

§Performance

Time complexity: O(n * m) where n is the key length and m is the height Space complexity: O(n + m) for the working vector and result string