Updating tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Trifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 03-03-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -21,10 +21,10 @@ public class Trifid{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Trifid.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
protected class CharLocation{
|
||||
protected int x;
|
||||
protected int y;
|
||||
protected int z;
|
||||
public CharLocation(int x, int y, int z){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
@@ -45,18 +45,19 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Fields
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
private int groupSize; //The size of the groups used to break up the input
|
||||
private char[][][] grid; //The grid used to encode/decode the message
|
||||
private char fillIn; //The character added to the alphabet to meet the 27 character requirement
|
||||
private boolean preserveCapitals; //Persist capitals in the output string
|
||||
private boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
private boolean preserveSymbols; //Persist symbols in the output string
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
protected int groupSize; //The size of the groups used to break up the input
|
||||
protected char[][][] grid; //The grid used to encode/decode the message
|
||||
protected char fillIn; //The character added to the alphabet to meet the 27 character requirement
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
|
||||
//Makes sure the fillIn is a valid character
|
||||
private void setFillIn(char fillIn) throws InvalidCharacterException{
|
||||
protected void setFillIn(char fillIn) throws InvalidCharacterException{
|
||||
//Make sure the character is a printing character
|
||||
if((fillIn < ' ') || (fillIn > '~')){
|
||||
throw new InvalidCharacterException("Fill in character must be a printing character");
|
||||
@@ -72,7 +73,7 @@ public class Trifid{
|
||||
this.fillIn = fillIn;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
@@ -93,7 +94,7 @@ public class Trifid{
|
||||
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn;
|
||||
|
||||
//Remove all duplicate characters
|
||||
logger.debug("Removing duplicated characters");
|
||||
logger.debug("Removing duplicate characters");
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
@@ -104,7 +105,7 @@ public class Trifid{
|
||||
createGrid();
|
||||
}
|
||||
//Creates the grid from the keyword
|
||||
private void createGrid(){
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){
|
||||
@@ -120,7 +121,7 @@ public class Trifid{
|
||||
logger.debug("Completed grid\n{}", getGrid());
|
||||
}
|
||||
//Ensures groupSize constraints
|
||||
private void setGroupSize(int groupSize) throws InvalidBaseException{
|
||||
protected void setGroupSize(int groupSize) throws InvalidBaseException{
|
||||
if(groupSize <= 0){
|
||||
throw new InvalidBaseException("Group size must be > 0");
|
||||
}
|
||||
@@ -130,7 +131,7 @@ public class Trifid{
|
||||
this.groupSize = groupSize;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -165,12 +166,12 @@ public class Trifid{
|
||||
}
|
||||
}
|
||||
//Returns the inputString with only letters
|
||||
private String getCleanInputString(){
|
||||
protected String getCleanInputString(){
|
||||
logger.debug("Cleaning input string for encoding");
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws InvalidCharacterException{
|
||||
protected CharLocation findChar(char letter) throws InvalidCharacterException{
|
||||
logger.debug("Finding character {} in grid", letter);
|
||||
|
||||
for(int layer = 0;layer < grid.length;++layer){
|
||||
@@ -189,7 +190,7 @@ public class Trifid{
|
||||
throw new InvalidCharacterException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Return the character from the location provided
|
||||
private char getChar(CharLocation location) throws InvalidCharacterException{
|
||||
protected char getChar(CharLocation location) throws InvalidCharacterException{
|
||||
if(location.getX() > 2){
|
||||
throw new InvalidCharacterException("x cannot be larget than 2");
|
||||
}
|
||||
@@ -205,7 +206,7 @@ public class Trifid{
|
||||
return grid[location.getZ()][location.getX()][location.getY()];
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
private void formatOutput(String outputString){
|
||||
protected void formatOutput(String outputString){
|
||||
logger.debug("Formatting output");
|
||||
|
||||
//Keep track of where you are in the output
|
||||
@@ -222,6 +223,15 @@ public class Trifid{
|
||||
logger.debug("Formatting lowercase");
|
||||
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else if(ch == fillIn){
|
||||
logger.debug("Adding fillIn");
|
||||
if(preserveCapitals){
|
||||
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
}
|
||||
else{
|
||||
logger.debug("Appending symbol");
|
||||
output.append(ch);
|
||||
@@ -229,15 +239,15 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Save the output
|
||||
logger.debug("Formatted output '{}'", output);
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Formatted output '{}'", this.outputString);
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
private void encode() throws InvalidCharacterException{
|
||||
protected void encode() throws InvalidCharacterException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Step through every element in the sanitized inputString encoding the letters
|
||||
logger.debug("Conveting letters to coordinates");
|
||||
logger.debug("Converting letters to coordinates");
|
||||
ArrayList<CharLocation> locations = new ArrayList<>();
|
||||
for(char ch : getCleanInputString().toCharArray()){
|
||||
//Get the location of the char in the grid
|
||||
@@ -247,10 +257,7 @@ public class Trifid{
|
||||
|
||||
//Split the locations up by group
|
||||
logger.debug("Splitting locations into groups");
|
||||
int numGroups = inputString.length() / groupSize;
|
||||
if(numGroups == 0){
|
||||
numGroups = 1;
|
||||
}
|
||||
int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize);
|
||||
ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups);
|
||||
for(int cnt = 0;cnt < numGroups;++cnt){
|
||||
groups.add(new ArrayList<>());
|
||||
@@ -302,7 +309,7 @@ public class Trifid{
|
||||
formatOutput(output.toString());
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
private void decode() throws InvalidCharacterException{
|
||||
protected void decode() throws InvalidCharacterException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Step through every element in the sanitized inputString encoding the letters
|
||||
@@ -316,10 +323,7 @@ public class Trifid{
|
||||
|
||||
//Split the locations up by group
|
||||
logger.debug("Splitting locations into groups");
|
||||
int numGroups = inputString.length() / groupSize;
|
||||
if(numGroups == 0){
|
||||
numGroups = 1;
|
||||
}
|
||||
int numGroups = (int)Math.ceil((double)inputString.length() / (double)groupSize);
|
||||
ArrayList<ArrayList<CharLocation>> groups = new ArrayList<>(numGroups);
|
||||
for(int cnt = 0;cnt < numGroups;++cnt){
|
||||
groups.add(new ArrayList<>());
|
||||
@@ -382,18 +386,21 @@ public class Trifid{
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
setFillIn('+');
|
||||
reset();
|
||||
}
|
||||
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
setFillIn('+');
|
||||
reset();
|
||||
}
|
||||
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char fillIn) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
setFillIn(fillIn);
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and groupSize and returns the result
|
||||
@@ -437,7 +444,8 @@ public class Trifid{
|
||||
groupSize = Integer.MAX_VALUE;
|
||||
grid = new char[3][3][3];
|
||||
}
|
||||
//Gets
|
||||
|
||||
//Getters
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user