leehyogum의 트러블슈팅

순수 Java + JDBC + IntelliJ 환경에서 BCrypt 사용하기 본문

CS/Database Systems

순수 Java + JDBC + IntelliJ 환경에서 BCrypt 사용하기

leehyogum 2024. 11. 24. 21:35

Spring, Spring Boot 등의 프레임워크 없는 순수 자바에서 jBCrypt를 사용하는 방법이다.

 

해당 링크에 접속한다.

https://mvnrepository.com/artifact/org.mindrot/jbcrypt/0.4

 

1. Files > jar을 클릭하여 다운로드 한다.

 

2. 외부 라이브러리 목록에 추가한다.

 

IntelliJ > 프로젝트 우클릭 > 모듈 설정 열기

 

라이브러리 > + 버튼 > 추가 > 다운받은 bcrypt파일 추가

 

외부 라이브러리 등록에 성공하였다.

왜 mysql-connector-j 밑으로 들어가졌는지는 모르겠으나.. 잘 되긴 한다.

 

 

import 성공

 


 

사용 예시

import org.mindrot.jbcrypt.BCrypt;

public class BcryptExample {
    public static void main(String[] args) {
        // 원래 비밀번호
        String plainPassword = "mySecurePassword";

        // 비밀번호 해싱
        String hashedPassword = BCrypt.hashpw(plainPassword, BCrypt.gensalt());
        System.out.println("Hashed Password: " + hashedPassword);

        // 비밀번호 검증 (사용자가 입력한 비밀번호와 해시 비교)
        boolean isPasswordMatch = BCrypt.checkpw(plainPassword, hashedPassword);
        if (isPasswordMatch) {
            System.out.println("Password matches!");
        } else {
            System.out.println("Password does not match!");
        }
    }
}

 

실행 결과