Programming/JAVA

JAVA SHA-256 암호화 방법

빈쿵바라기 2023. 5. 10. 16:58
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA256Util {

	public static String SHA256Encrypt(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		StringBuffer hexString = new StringBuffer();
		
		MessageDigest digest = MessageDigest.getInstance("SHA-256");
    	byte[] hash = digest.digest(token.getBytes("UTF-8"));
    	
    	
    	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();
	}
	
}