반응형
이전 포스팅에서 SHA 256에 대해서 설명한 글이 있다.
개념을 잘 이해하지 못했다면 참고포스팅을 참고 바란다.
Java 에서 사용은 간단하다.
security 안에 MessageDigest 클래스를 사용하면 된다.
예제 코드를 바로 확인하면 아래와 같다.
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SHA256 { public String encrypt(String text) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(text.getBytes()); return bytesToHex(md.digest()); } private String bytesToHex(byte[] bytes) { StringBuilder builder = new StringBuilder(); for (byte b : bytes) { builder.append(String.format("%02x", b)); } return builder.toString(); } } |
그리고 사용하는 메서드에서는 아래와 같이 표현이 된다.
import java.security.NoSuchAlgorithmException; public class Main { public static void main(String[] args) throws NoSuchAlgorithmException { SHA256 sha256 = new SHA256(); String password = "thenicesj1234"; String cryptogram = sha256.encrypt(password); System.out.println(cryptogram); System.out.println(cryptogram.equals(sha256.encrypt(password))); } } |
참고 포스팅
https://thenicesj.tistory.com/721
[암호화] AES-128과 SHA-256
암호화란? 데이터를 있는 그대로 저장할 경우 해킹을 당하면 개인 정보가 그대로 노출이 된다. 이럴 경우를 대비하기 위해서 개발자들은 암호화 라는 기법을 사용하기 시작하였다. 암호화를 하
thenicesj.tistory.com
반응형
'IT > Java' 카테고리의 다른 글
SQL Format Style (25) | 2024.06.06 |
---|---|
Java 에서 Bcrypt 사용 (7) | 2024.06.05 |
Spring Batch (part 4. 프로젝트 적용 - Tasklet 방식) (21) | 2024.06.03 |
H2 다룰때 초기화할 데이터들 설정 (25) | 2024.06.02 |
H2 DB 3가지 모드 사용(Embedded, In-Memory, Server) (15) | 2024.06.01 |
댓글