80 lines
3.2 KiB
Java
80 lines
3.2 KiB
Java
package com.mattrixwv.cipherstream.controller.polysubstitution;
|
|
|
|
|
|
import org.slf4j.MDC;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
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.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
|
|
import com.mattrixwv.cipherstream.polysubstitution.Hill;
|
|
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
|
|
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/cipherStream/hill")
|
|
public class HillCipherController{
|
|
@GetMapping
|
|
public ObjectNode getCipherInfo(){
|
|
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.HILL_CIPHER_NAME);
|
|
log.info("Getting info for {}", CipherInfoUtil.HILL_CIPHER_NAME);
|
|
|
|
|
|
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.HILL_CIPHER_NAME, CipherInfoUtil.HILL_CIPHER_DESCRIPTION);
|
|
}
|
|
|
|
@GetMapping("/encode")
|
|
public ObjectNode encodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{
|
|
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.HILL_CIPHER_NAME);
|
|
log.info("Encoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
|
|
|
|
|
|
CipherParameterUtil.verifyHillParams(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();
|
|
int[][] key = new ObjectMapper().treeToValue(cipherParams.get(CipherParameterUtil.HILL_KEY), int[][].class);
|
|
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
|
|
|
|
|
|
Hill hill = new Hill(preserveCapitals, preserveWhitespace, preserveSymbols);
|
|
String outputString = hill.encode(key, inputString);
|
|
|
|
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
|
|
|
|
|
|
return cipherParams;
|
|
}
|
|
|
|
@GetMapping("/decode")
|
|
public ObjectNode decodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{
|
|
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.HILL_CIPHER_NAME);
|
|
log.info("Decoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
|
|
|
|
|
|
CipherParameterUtil.verifyHillParams(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();
|
|
int[][] key = new ObjectMapper().treeToValue(cipherParams.get(CipherParameterUtil.HILL_KEY), int[][].class);
|
|
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
|
|
|
|
|
|
Hill hill = new Hill(preserveCapitals, preserveWhitespace, preserveSymbols);
|
|
String outputString = hill.decode(key, inputString);
|
|
|
|
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
|
|
|
|
|
|
return cipherParams;
|
|
}
|
|
}
|