package com.guozhi.bloodanalysis.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class EncryptUtils { public static String encryptSHA256(String str){ return sha("SHA-256",str); } private static String sha(String type,String str){ String result = null; try { MessageDigest md = MessageDigest.getInstance(type); md.update(str.getBytes()); byte[] byteBuffer = md.digest(); StringBuilder strHexString = new StringBuilder(); for (byte b : byteBuffer) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { strHexString.append('0'); } strHexString.append(hex); } result = strHexString.toString(); }catch (NoSuchAlgorithmException e){ e.printStackTrace(); } return result; } }