Files
CipherStreamAPI/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherController.java
2024-04-22 23:25:43 -04:00

90 lines
3.4 KiB
Java

package com.mattrixwv.cipherstream.controller.combination;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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.combination.ADFGX;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/adfgx")
@PropertySource("classpath:ciphers.properties")
public class AdfgxCipherController{
@Value("${cipher.combination.adfgx.name}")
private String adfgxName;
@Value("${cipher.combination.adfgx.description}")
private String adfgxDescription;
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, adfgxName);
log.info("Getting info for {}", adfgxName);
return CipherInfoUtil.buildInfoNode(adfgxName, adfgxDescription);
}
@PostMapping("/encode")
public ObjectNode encodeAdfgx(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, adfgxName);
log.info("Encoding {}", adfgxName);
CipherParameterUtil.verifySquareKeyword(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();
String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asText();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
ADFGX adfgx = new ADFGX(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = adfgx.encode(squareKeyword, keyword, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
@PostMapping("/decode")
public ObjectNode decodeAdfgx(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, adfgxName);
log.info("Decoding {}", adfgxName);
CipherParameterUtil.verifySquareKeyword(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();
String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asText();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
ADFGX adfgx = new ADFGX(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = adfgx.decode(squareKeyword, keyword, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
}