HashUtils.java 工具类
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HashUtils { private static String[] hexCode = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; private static final int TWO_HUNDERDS_AND_FIFTY_SIX = 256; private static final int SIXTEEN = 16; private static String byteToHexString(byte b) { int n = b; if (n < 0) { n = TWO_HUNDERDS_AND_FIFTY_SIX + n; } int d1 = n / SIXTEEN; int d2 = n % SIXTEEN; return hexCode[d1] + hexCode[d2]; } private static String byteArrayToHexString(byte[] b) { StringBuilder result = new StringBuilder(); for (int i = 0; i < b.length; i++) { result.append(byteToHexString(b[i])); } return result.toString(); } public static String hashSHA256(byte[] input){ MessageDigest md = null; try { md = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } byte[] digest = md.digest(input); return byteArrayToHexString(digest); } }