package com.mattrixwv.cipherstream.controller.polysubstitution; 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.polysubstitution.Trifid; 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("/trifid") @PropertySource("classpath:ciphers.properties") public class TrifidCipherController{ @Value("${cipher.poly.trifid.name}") private String trifidName; @Value("${cipher.poly.trifid.description}") private String trifidDescription; @Value("#{${cipher.poly.trifid.explanation}}") private List trifidExplanation; @Value("#{${cipher.poly.trifid.facts}}") private List trifidFacts; @GetMapping public ObjectNode getCipherInfo(){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, trifidName); log.info("Getting info for {}", trifidName); return CipherInfoUtil.buildInfoNode(trifidName, trifidDescription, trifidExplanation, trifidFacts); } @PostMapping("/encode") public ObjectNode encodeTrifid(@RequestBody ObjectNode cipherParams){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, trifidName); log.info("Encoding {}", trifidName); 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).asString(); char fill = cipherParams.get(CipherParameterUtil.TRIFID_FILL).asString().charAt(0); int groupLength = cipherParams.get(CipherParameterUtil.TRIFID_GROUP_LENGTH).asInt(); String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString(); Trifid trifid = new Trifid(preserveCapitals, preserveWhitespace, preserveSymbols, fill); String outputString = trifid.encode(keyword, groupLength, inputString); cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); return cipherParams; } @PostMapping("/decode") public ObjectNode decodeTrifid(@RequestBody ObjectNode cipherParams){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, trifidName); log.info("Decoding {}", trifidName); 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).asString(); char fill = cipherParams.get(CipherParameterUtil.TRIFID_FILL).asString().charAt(0); int groupLength = cipherParams.get(CipherParameterUtil.TRIFID_GROUP_LENGTH).asInt(); String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString(); Trifid trifid = new Trifid(preserveCapitals, preserveWhitespace, preserveSymbols, fill); String outputString = trifid.decode(keyword, groupLength, inputString); cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); return cipherParams; } }