From 5e331b873f7f30c609260db92df408e7f2e7eedb Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Fri, 1 Dec 2023 14:24:33 +0900 Subject: [PATCH] =?UTF-8?q?ARIA=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cokr/xit/foundation/data/ARIA.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/main/java/cokr/xit/foundation/data/ARIA.java diff --git a/src/main/java/cokr/xit/foundation/data/ARIA.java b/src/main/java/cokr/xit/foundation/data/ARIA.java new file mode 100644 index 0000000..f291da9 --- /dev/null +++ b/src/main/java/cokr/xit/foundation/data/ARIA.java @@ -0,0 +1,120 @@ +package cokr.xit.foundation.data; + +import java.util.Base64; + +import org.egovframe.rte.fdl.cryptography.EgovPasswordEncoder; +import org.egovframe.rte.fdl.cryptography.impl.EgovARIACryptoServiceImpl; + +import cokr.xit.foundation.AbstractComponent; + +/**ARIA 기반의 암/복호화 서비스를 제공하는 컴포넌트 + * @author mjkhan + */ +public class ARIA extends AbstractComponent { + private String + key, + algorithm; + private int blockSize; + + private EgovPasswordEncoder passwordEncoder; + private EgovARIACryptoServiceImpl aria; + + /**키를 반환한다. + * @return 키 + */ + public String getKey() { + return key; + } + + /**키를 설정한다. + * @param key 키 + * @return 현재 ARIA + */ + public ARIA setKey(String key) { + this.key = key; + return this; + } + + /**알고리즘을 반환한다. 디폴트는 SHA-256. + * @return 알고리즘 + */ + public String getAlgorithm() { + return algorithm; + } + + /**알고리즘을 설정한다. 디폴트는 SHA-256. + * @param algorithm 알고리즘 + * @return 현재 ARIA + */ + public ARIA setAlgorithm(String algorithm) { + this.algorithm = algorithm; + return this; + } + + /**블록사이즈를 반환한다. 디폴트는 1024. + * @return 블록사이즈 + */ + public int getBlockSize() { + return blockSize < 1 ? 1024 : blockSize; + } + + /**블록사이즈를 설정한다. 디폴트는 1024. + * @param blockSize 블록사이즈 + * @return 현재 ARIA + */ + public ARIA setBlockSize(int blockSize) { + this.blockSize = blockSize; + return this; + } + + /**주어진 문자열(평문)을 암호화하여 반환한다. + * @param plain 문자열(평문) + * @return 암호화한 문자열 + */ + public String encrypt(String plain) { + if (isEmpty(plain)) return ""; + + byte[] bytes = service().encrypt(plain.getBytes(), key); + return Base64.getEncoder().encodeToString(bytes); + } + + private EgovPasswordEncoder passwordEncoder() { + if (passwordEncoder == null) { + passwordEncoder = new EgovPasswordEncoder(); + String hashed = passwordEncoder.encryptPassword(notEmpty(key, "key")); + passwordEncoder.setHashedPassword(hashed); + if (!isEmpty(algorithm)) + passwordEncoder.setAlgorithm(algorithm); + } + return passwordEncoder; + } + + private EgovARIACryptoServiceImpl service() { + if (aria == null) { + aria = new EgovARIACryptoServiceImpl(); + aria.setPasswordEncoder(passwordEncoder()); + aria.setBlockSize(getBlockSize()); + } + return aria; + } + + /**주어진 문자열(암호화)을 복호화하여 반환한다. + * @param encrypted 문자열(암호화) + * @return 복호화한 문자열 + */ + public String decrypt(String encrypted) { + byte[] bytes = Base64.getDecoder().decode(encrypted); + return new String(service().decrypt(bytes, key)); + } + + /**현재 설정상태를 비운다. + * @return 현재 ARIA + */ + public ARIA clear() { + key = algorithm = null; + blockSize = 0; + passwordEncoder = null; + aria = null; + return this; + } +} \ No newline at end of file