-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: 사용자 암호는 안전한 해시 함수로 일방향 암호화한다.
- Loading branch information
Showing
5 changed files
with
131 additions
and
21 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
19 changes: 0 additions & 19 deletions
19
src/main/java/com/seong/shoutlink/domain/auth/SimplePasswordEncoder.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
src/main/java/com/seong/shoutlink/global/auth/crypto/BCryptPasswordEncoder.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,22 @@ | ||
package com.seong.shoutlink.global.auth.crypto; | ||
|
||
import com.seong.shoutlink.domain.auth.PasswordEncoder; | ||
import org.mindrot.jbcrypt.BCrypt; | ||
|
||
public class BCryptPasswordEncoder implements PasswordEncoder { | ||
|
||
@Override | ||
public String encode(String rawPassword) { | ||
return BCrypt.hashpw(rawPassword, BCrypt.gensalt()); | ||
} | ||
|
||
@Override | ||
public boolean isMatches(String rawPassword, String encodedPassword) { | ||
return BCrypt.checkpw(rawPassword, encodedPassword); | ||
} | ||
|
||
@Override | ||
public boolean isNotMatches(String rawPassword, String encodedPassword) { | ||
return !isMatches(rawPassword, encodedPassword); | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
src/test/java/com/seong/shoutlink/global/auth/crypto/BCryptPasswordEncoderTest.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,104 @@ | ||
package com.seong.shoutlink.global.auth.crypto; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BCryptPasswordEncoderTest { | ||
|
||
BCryptPasswordEncoder bCryptPasswordEncoder; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
bCryptPasswordEncoder = new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Nested | ||
@DisplayName("encode 호출 시") | ||
class EncodeTest { | ||
|
||
@Test | ||
@DisplayName("성공: 일방향 해싱됨") | ||
void encode() { | ||
//given | ||
String password = "asdf1234!"; | ||
|
||
//when | ||
String encoded = bCryptPasswordEncoder.encode(password); | ||
|
||
//then | ||
assertThat(encoded).isNotEqualTo(password); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("matches 호출 시") | ||
class MatchesTest { | ||
|
||
@Test | ||
@DisplayName("성공: 플레인 텍스트와 해시값이 동일하면 true") | ||
void matches_ThenTrue() { | ||
//given | ||
String password = "asdf1234!"; | ||
String encoded = bCryptPasswordEncoder.encode(password); | ||
|
||
//when | ||
boolean result = bCryptPasswordEncoder.isMatches(password, encoded); | ||
|
||
//then | ||
assertThat(result).isTrue(); | ||
} | ||
|
||
@Test | ||
@DisplayName("성공: 플레인 텍스트와 해시값이 동일하지 않으면 false") | ||
void noneMatches_ThenFalse() { | ||
//given | ||
String password = "asdf1234!"; | ||
String encoded = bCryptPasswordEncoder.encode(password); | ||
String noneMatches = password + "a"; | ||
|
||
//when | ||
boolean result = bCryptPasswordEncoder.isMatches(noneMatches, encoded); | ||
|
||
//then | ||
assertThat(result).isFalse(); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("noneMatches 호출 시") | ||
class NoneMatchesTest { | ||
|
||
@Test | ||
@DisplayName("성공: 플레인 텍스트와 해시값이 동일하지 않으면 true") | ||
void noneMatches_ThenTrue() { | ||
//given | ||
String password = "asdf1234!"; | ||
String encoded = bCryptPasswordEncoder.encode(password); | ||
String noneMatch = password + "a"; | ||
|
||
//when | ||
boolean result = bCryptPasswordEncoder.isNotMatches(noneMatch, encoded); | ||
|
||
//then | ||
assertThat(result).isTrue(); | ||
} | ||
|
||
@Test | ||
@DisplayName("성공: 플레인 텍스트와 해시값이 동일하면 false") | ||
void matches_ThenFalse() { | ||
//given | ||
String matches = "asdf1234!"; | ||
String encoded = bCryptPasswordEncoder.encode(matches); | ||
|
||
//when | ||
boolean result = bCryptPasswordEncoder.isNotMatches(matches, encoded); | ||
|
||
//then | ||
assertThat(result).isFalse(); | ||
} | ||
} | ||
} |