본문 바로가기

Java/Java 기초

SHA256 encoding

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA256 {
    final static String ALGORITHM = "SHA-256";
    
    public String encode(byte[] param) {
        MessageDigest md;
        String endcodedData = null;
        try {
            md = MessageDigest.getInstance(ALGORITHM);

            byte[] hash = md.digest(param);
            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);
            }
            endcodedData = hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.toString());
        }
        return endcodedData;
    }
}


'Java > Java 기초' 카테고리의 다른 글

Autoboxing과 AutoUnboxing  (0) 2019.01.14
Properties Class 사용  (0) 2018.06.12
Base62 encoding decoding  (0) 2018.05.29
Java Queue 구현  (0) 2018.05.24
Java Stack 구현  (0) 2018.05.24