Files
CipherStreamAPI/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherController.java
2024-04-07 19:41:57 -04:00

72 lines
2.9 KiB
Java

package com.mattrixwv.cipherstream.controller.polysubstitution;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Trifid;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/cipherStream/trifid")
public class TrifidCipherController{
@GetMapping("/encode")
public ObjectNode encodeTrifid(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Trifid");
log.info("Encoding Trifid");
CipherParameterUtil.verifyTrifidParams(cipherParams);
boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean();
boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean();
boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean();
String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText();
char fill = cipherParams.get(CipherParameterUtil.TRIFID_FILL).asText().charAt(0);
int groupLength = cipherParams.get(CipherParameterUtil.TRIFID_GROUP_LENGTH).asInt();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
Trifid trifid = new Trifid(preserveCapitals, preserveWhitespace, preserveSymbols, fill);
String outputString = trifid.encode(keyword, groupLength, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
@GetMapping("/decode")
public ObjectNode decodeTrifid(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Trifid");
log.info("Decoding Trifid");
CipherParameterUtil.verifyTrifidParams(cipherParams);
boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean();
boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean();
boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean();
String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText();
char fill = cipherParams.get(CipherParameterUtil.TRIFID_FILL).asText().charAt(0);
int groupLength = cipherParams.get(CipherParameterUtil.TRIFID_GROUP_LENGTH).asInt();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
Trifid trifid = new Trifid(preserveCapitals, preserveWhitespace, preserveSymbols, fill);
String outputString = trifid.decode(keyword, groupLength, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
}