Here you can find the source of sha256(String string)
Parameter | Description |
---|---|
string | to SHA256 |
public static String sha256(String string)
//package com.java2s; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /**//from w w w .j av a 2s . co m * Generate a SHA256 checksum of a string. * * @param string to SHA256 * @return A SHA256 string */ public static String sha256(String string) { MessageDigest digest = null; String hash = ""; try { digest = MessageDigest.getInstance("SHA-256"); digest.update(string.getBytes()); byte[] bytes = digest.digest(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { sb.append('0'); } sb.append(hex); } hash = sb.toString(); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } return hash; } }