Files
CipherStreamAPI/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherController.java
2026-01-06 23:48:37 -05:00

96 lines
3.7 KiB
Java

package com.mattrixwv.cipherstream.controller.combination;
import java.util.List;
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.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.combination.ADFGVX;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j;
import tools.jackson.databind.node.ObjectNode;
@Slf4j
@RestController
@RequestMapping("/adfgvx")
@PropertySource("classpath:ciphers.properties")
public class AdfgvxCipherController{
@Value("${cipher.combination.adfgvx.name}")
private String adfgvxName;
@Value("${cipher.combination.adfgvx.description}")
private String adfgvxDescription;
@Value("#{${cipher.combination.adfgvx.explanation}}")
private List<String> adfgvxExplanation;
@Value("#{${cipher.combination.adfgvx.facts}}")
private List<String> adfgvxFacts;
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, adfgvxName);
log.info("Getting info for {}", adfgvxName);
return CipherInfoUtil.buildInfoNode(adfgvxName, adfgvxDescription, adfgvxExplanation, adfgvxFacts);
}
@PostMapping("/encode")
public ObjectNode encodeAdfgvx(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, adfgvxName);
log.info("Encoding {}", adfgvxName);
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).asString();
String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asString();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString();
ADFGVX adfgvx = new ADFGVX(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = adfgvx.encode(squareKeyword, keyword, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
@PostMapping("/decode")
public ObjectNode decodeAdfgvx(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, adfgvxName);
log.info("Decoding {}", adfgvxName);
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).asString();
String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asString();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString();
ADFGVX adfgvx = new ADFGVX(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = adfgvx.decode(squareKeyword, keyword, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
}