java对String进行sha1加密
方法一:
1、使用apache的codec jar包对string进行加密,先下载并引入jar包:
http://commons.apache.org/proper/commons-codec/
2、生成:
String sign = DigestUtils.shaHex(str);
方法二:
/**
* 进行sha1加密
*
* @param strs
* @return
*/
private String getSha1(String strs) {
try {
MessageDigest md = MessageDigest.getInstance(“sha1”);
byte[] digest = md.digest(strs.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b :
digest) {
sb.append(chars[(b >> 4) & 15]);
sb.append(chars[b & 15]);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return “getSha1 failed”;
}
}