Encryptor, KeyFactory 제거

master
mjkhan21 7 months ago
parent 60a87e8d35
commit 442df0ed6e

@ -23,9 +23,7 @@ import cokr.xit.foundation.Access;
* @author mjkhan * @author mjkhan
*/ */
@Component("applicationAccess") @Component("applicationAccess")
public class ApplicationAccess public class ApplicationAccess extends AbstractComponent implements AccessDecisionVoter<Object>, AuthorizationManager<RequestAuthorizationContext> {
extends AbstractComponent
implements AccessDecisionVoter<Object>, AuthorizationManager<RequestAuthorizationContext> {
/** 권한별 가용 기능(URL) */ /** 권한별 가용 기능(URL) */
@Resource(name="accessContext") @Resource(name="accessContext")
protected AccessContext accessContext; protected AccessContext accessContext;

@ -1,80 +0,0 @@
package cokr.xit.base.security.crypto;
import java.security.Key;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import cokr.xit.foundation.AbstractComponent;
public class Encryptor extends AbstractComponent {
private String algorithm;
private Key key;
private IvParameterSpec iv;
private Cipher cipher;
/** .
* @return
*/
public String getAlgorithm() {
return algorithm;
}
/** .
* @param algorithm
* @return Encryptor
*/
public Encryptor setAlgorithm(String algorithm) {
this.algorithm = algorithm;
return this;
}
/**SecretKey .
* @return SecretKey
*/
public Key getKey() {
return key;
}
/**SecretKey .
* @param key SecretKey
* @return Encryptor
*/
public Encryptor setKey(Key key) {
this.key = key;
return this;
}
/**iv .
* @return iv
*/
public IvParameterSpec getIv() {
return iv;
}
/**iv .
* @param iv iv
* @return Encryptor
*/
public Encryptor setIv(IvParameterSpec iv) {
this.iv = iv;
return this;
}
public String encrypt(String str) {
if (isEmpty(str)) return "";
try {
if (cipher == null) {
cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
}
byte[] cipherText = cipher.doFinal(str.getBytes());
return Base64.getEncoder()
.encodeToString(cipherText);
} catch (Exception e) {
throw applicationException(e);
}
}
}

@ -1,85 +0,0 @@
package cokr.xit.base.security.crypto;
import java.security.Key;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.util.List;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import cokr.xit.foundation.AbstractComponent;
public class KeyFactory extends AbstractComponent {
private int keySize;
private String algorithm;
private KeyGenerator keyGen;
/** .
* @return
*/
public int getKeySize() {
return Math.max(128, keySize);
}
/** .
* @param keySize (128, 192, 256 )
* @return KeyFactory
*/
public KeyFactory setKeySize(int keySize) {
if (!List.of(128, 192, 256).contains(keySize))
throw new IllegalArgumentException("keySize must be either of 128, 192, 256");
this.keySize = keySize;
return this;
}
/** .
* @return
*/
public String getAlgorithm() {
return ifEmpty(algorithm, "AES");
}
/** .
* @param algorithm
* @return KeyFactory
*/
public KeyFactory setAlgorithm(String algorithm) {
this.algorithm = algorithm;
return this;
}
public static IvParameterSpec generateIv(int size) {
byte[] iv = new byte[size];
new SecureRandom().nextBytes(iv);
return new IvParameterSpec(iv);
}
public Key create() {
try {
if (keyGen == null) {
keyGen = KeyGenerator.getInstance(getAlgorithm());
keyGen.init(getKeySize());
}
return keyGen.generateKey();
} catch (Exception e) {
throw applicationException(e);
}
}
public Key create(String str, String salt) {
if (isEmpty(str) || isEmpty(salt)) return null;
try {
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(str.toCharArray(), salt.getBytes(), 65536, getKeySize());
return new SecretKeySpec(skf.generateSecret(spec).getEncoded(), getAlgorithm());
} catch (Exception e) {
throw applicationException(e);
}
}
}
Loading…
Cancel
Save