Skip to content

Commit

Permalink
Merge pull request #102 from Seasoning-Today/crypto
Browse files Browse the repository at this point in the history
기록장 암호화 적용
  • Loading branch information
csct3434 authored Mar 27, 2024
2 parents 8cdf2c9 + ae60c88 commit 31546f5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
Expand All @@ -20,6 +21,7 @@
import org.hibernate.annotations.OnDeleteAction;
import today.seasoning.seasoning.article.dto.RegisterArticleCommand;
import today.seasoning.seasoning.common.BaseTimeEntity;
import today.seasoning.seasoning.common.cipher.CryptoConverter;
import today.seasoning.seasoning.common.util.TsidUtil;
import today.seasoning.seasoning.solarterm.domain.SolarTerm;
import today.seasoning.seasoning.user.domain.User;
Expand Down Expand Up @@ -49,6 +51,7 @@ public class Article extends BaseTimeEntity {
private int createdTerm;

@Lob
@Convert(converter = CryptoConverter.class)
private String contents;

@OneToMany(mappedBy = "article", cascade = {CascadeType.REMOVE, CascadeType.PERSIST}, orphanRemoval = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package today.seasoning.seasoning.common.cipher;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "cipher")
public class CipherProperties {

private String secretKey;
private String algorithm;
private String transformation;
private String iv;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package today.seasoning.seasoning.common.cipher;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CipherUtil {

private final Cipher cipher;
private final SecretKeySpec secretKeySpec;
private final IvParameterSpec ivParameterSpec;

@Autowired
public CipherUtil(CipherProperties properties) throws NoSuchPaddingException, NoSuchAlgorithmException {
this.secretKeySpec = new SecretKeySpec(properties.getSecretKey().getBytes(UTF_8), properties.getAlgorithm());
this.ivParameterSpec = new IvParameterSpec(properties.getIv().getBytes());
this.cipher = Cipher.getInstance(properties.getTransformation());
}

public String encode(String plainText) {
if (plainText == null) {
return null;
}

try {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(plainText.getBytes(UTF_8));
return Base64.encodeBase64String(encrypted);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public String decode(String encodedText) {
if (encodedText == null) {
return null;
}

try {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decoded = cipher.doFinal(Base64.decodeBase64(encodedText));
return new String(decoded, UTF_8);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package today.seasoning.seasoning.common.cipher;

import javax.persistence.AttributeConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CryptoConverter implements AttributeConverter<String, String> {

private final CipherUtil cipherUtil;

@Override
public String convertToDatabaseColumn(String attribute) {
return cipherUtil.encode(attribute);
}

@Override
public String convertToEntityAttribute(String dbData) {
return cipherUtil.decode(dbData);
}
}

0 comments on commit 31546f5

Please sign in to comment.