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

95 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.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;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.node.ObjectNode;
@Slf4j
@RestController
@RequestMapping("/hill")
@PropertySource("classpath:ciphers.properties")
public class HillCipherController{
@Value("${cipher.poly.hill.name}")
private String hillName;
@Value("${cipher.poly.hill.description}")
private String hillDescription;
@Value("#{${cipher.poly.hill.explanation}}")
private List<String> hillExplanation;
@Value("#{${cipher.poly.hill.facts}}")
private List<String> hillFacts;
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
log.info("Getting info for {}", hillName);
return CipherInfoUtil.buildInfoNode(hillName, hillDescription, hillExplanation, hillFacts);
}
@PostMapping("/encode")
public ObjectNode encodeHill(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
log.info("Encoding {}", hillName);
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).asString();
Hill hill = new Hill(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = hill.encode(key, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
@PostMapping("/decode")
public ObjectNode decodeHill(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, hillName);
log.info("Decoding {}", hillName);
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).asString();
Hill hill = new Hill(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = hill.decode(key, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
}