直接調用HashKit.sha1(String str)方法就能夠了,,返回的是16進制的字符串長度是40,算法
也就是用md.digest()方法解析出來的字節數是160字節長度。app
而MD5散列算法生成的字節數是128字節長度,返回的16進制的字符長度是32位ui
public class HashKit { private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); public static String sha1(String srcStr){ return hash("SHA-1", srcStr); } public static String hash(String algorithm, String srcStr) { try { MessageDigest md = MessageDigest.getInstance(algorithm); byte[] bytes = md.digest(srcStr.getBytes("utf-8")); return toHex(bytes); } catch (Exception e) { throw new RuntimeException(e); } } public static String toHex(byte[] bytes) { StringBuilder ret = new StringBuilder(bytes.length * 2); for (int i=0; i<bytes.length; i++) { ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]); ret.append(HEX_DIGITS[bytes[i] & 0x0f]); } return ret.toString(); } }