package com.mattrixwv.cipherstream.controller.monosubstitution; 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.monosubstitution.OneTimePad; 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("/oneTimePad") @PropertySource("classpath:ciphers.properties") public class OneTimePadCipherController{ @Value("${cipher.mono.onetimepad.name}") private String oneTimePadName; @Value("${cipher.mono.onetimepad.description}") private String oneTimePadDescription; @Value("#{${cipher.mono.onetimepad.explanation}}") private List oneTimePadExplanation; @Value("#{${cipher.mono.onetimepad.facts}}") private List oneTimePadFacts; @GetMapping public ObjectNode getCipherInfo(){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, oneTimePadName); log.info("Getting info for {}", oneTimePadName); return CipherInfoUtil.buildInfoNode(oneTimePadName, oneTimePadDescription, oneTimePadExplanation, oneTimePadFacts); } @PostMapping("/encode") public ObjectNode encodeOneTimePad(@RequestBody ObjectNode cipherParams){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, oneTimePadName); log.info("Encoding {}", oneTimePadName); CipherParameterUtil.verifyParamsWithKeyword(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 inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString(); OneTimePad oneTimePad = new OneTimePad(preserveCapitals, preserveWhitespace, preserveSymbols); String outputString = oneTimePad.encode(keyword, inputString); cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); return cipherParams; } @PostMapping("/decode") public ObjectNode decodeOneTimePad(@RequestBody ObjectNode cipherParams){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, oneTimePadName); log.info("Decoding {}", oneTimePadName); CipherParameterUtil.verifyParamsWithKeyword(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 inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString(); OneTimePad oneTimePad = new OneTimePad(preserveCapitals, preserveWhitespace, preserveSymbols); String outputString = oneTimePad.decode(keyword, inputString); cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); return cipherParams; } }