본문 바로가기

Java

SHA-256 예제

import java.security.MessageDigest;
 
public class CommonUtils {
    
    public static String SHA256Encoder(String password) {
        
        try{
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(password.getBytes("UTF-8"));
            StringBuffer hexString = new StringBuffer();
 
            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if(hex.length() == 1) hexString.append('0');
                hexString.append(hex.toLowerCase());
            }
 
            return hexString.toString();
        } catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
       
    }
}
cs

참고

SHA-256 Hash Generator