-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 행사 비밀번호 암호화 추가 * refactor: 비밀번호 암호화 로직 수정 * fix: MessageDigest를 싱글톤으로 관리하지 않도록 수정 --------- Co-authored-by: 3juhwan <[email protected]>
- Loading branch information
Showing
6 changed files
with
103 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
server/src/main/java/server/haengdong/domain/event/Password.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package server.haengdong.domain.event; | ||
|
||
import jakarta.persistence.Embeddable; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Base64; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import server.haengdong.exception.HaengdongErrorCode; | ||
import server.haengdong.exception.HaengdongException; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@Embeddable | ||
public class Password { | ||
|
||
public static final int PASSWORD_LENGTH = 4; | ||
private static final Pattern PASSWORD_PATTERN = Pattern.compile(String.format("^\\d{%d}$", PASSWORD_LENGTH)); | ||
private static final String HASH_ALGORITHM = "SHA-256"; | ||
|
||
private String value; | ||
|
||
public Password(String password) { | ||
validatePassword(password); | ||
this.value = encode(password); | ||
} | ||
|
||
private void validatePassword(String password) { | ||
Matcher matcher = PASSWORD_PATTERN.matcher(password); | ||
if (!matcher.matches()) { | ||
throw new HaengdongException(HaengdongErrorCode.EVENT_PASSWORD_FORMAT_INVALID); | ||
} | ||
} | ||
|
||
private String encode(String rawPassword) { | ||
try { | ||
MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM); | ||
byte[] hashedPassword = digest.digest(rawPassword.getBytes()); | ||
return Base64.getEncoder().encodeToString(hashedPassword); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalArgumentException("해시 알고리즘이 존재하지 않습니다."); | ||
} | ||
} | ||
|
||
public boolean matches(String rawPassword) { | ||
String hashedPassword = encode(rawPassword); | ||
return value.equals(hashedPassword); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
server/src/test/java/server/haengdong/domain/event/PasswordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package server.haengdong.domain.event; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import server.haengdong.exception.HaengdongException; | ||
|
||
class PasswordTest { | ||
|
||
@DisplayName("비밀번호는 4자리 숫자 입니다.") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"1", "12", "123", "12345", "adgd"}) | ||
void validatePassword(String rawPassword) { | ||
assertThatCode(() -> new Password(rawPassword)) | ||
.isInstanceOf(HaengdongException.class); | ||
} | ||
} |