[JAVA]SHA-256 HashCode

개발/JAVA2014. 3. 14. 09:26
728x90

SHA-256(Sechre Hash Standard) 
 현재 일반적으로 암호틀 통해 저장을 주로 한다.

public static String sha256(String base) {
    try{
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(base.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);
        }

        return hexString.toString();
    } catch(Exception ex){
       throw new RuntimeException(ex);
    }
}

  apache-commons를 활용할 경우 좀 더 간단하게 할 수 있다.

 String sha256hex = org.apache.commons.codec.digest.DigestUtils.sha256Hex("codezip.net");



결과는 64자리의 결과값을 리턴하게 된다. 

728x90

작성자

Posted by 일퍼센트

관련 글

댓글 영역