Files
CipherStreamAPI/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherController.java

94 lines
3.5 KiB
Java

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.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/columnar")
@PropertySource("classpath:ciphers.properties")
public class ColumnarCipherController{
@Value("${cipher.poly.columnar.name}")
private String columnarName;
@Value("${cipher.poly.columnar.description}")
private String columnarDescription;
@Value("${cipher.poly.columnar.explanation}")
private List<String> columnarExplanation;
@Value("${cipher.poly.columnar.facts}")
private List<String> columnarFacts;
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, columnarName);
log.info("Getting info for {}", columnarName);
return CipherInfoUtil.buildInfoNode(columnarName, columnarDescription, columnarExplanation, columnarFacts);
}
@PostMapping("/encode")
public ObjectNode encodeColumnar(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, columnarName);
log.info("Encoding {}", columnarName);
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).asText();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
Columnar columnar = new Columnar(preserveCapitals, preserveWhitespace, preserveSymbols, true);
String outputString = columnar.encode(keyword, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
@PostMapping("/decode")
public ObjectNode decodeColumnar(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, columnarName);
log.info("Decoding {}", columnarName);
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).asText();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
Columnar columnar = new Columnar(preserveCapitals, preserveWhitespace, preserveSymbols, true);
String outputString = columnar.decode(keyword, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
}