JDK自带加密算法sha-1

package com.common.util;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
//import org.apache.commons.codec.digest.DigestUtils;
 
///
 // JDK自带加密算法
 // 
 // @author iPan
 // @version 2014-4-25
 //
public class MessageDigestUtils {
 
public static String sha1(String text) {
MessageDigest md = null;
String outStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(text.getBytes());
outStr = byteToString(digest);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return outStr;
}
 
private static String byteToString(byte[] digest) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
String tempStr = Integer.toHexString(digest[i] & 0xff);
if (tempStr.length() == 1) {
buf.append("0").append(tempStr);
} else {
buf.append(tempStr);
}
}
return buf.toString().toLowerCase();
}
public static void main(String[] args) {
// System.out.println(DigestUtils.sha1Hex("123456"));
System.out.println(MessageDigestUtils.sha1("123456"));
}
}

转载于:https://www.cnblogs.com/zhangminghan/p/6269562.html

你可能感兴趣的:(JDK自带加密算法sha-1)