【工具】加密方式
程序员文章站
2024-01-14 11:05:46
...
private static String md5(String tmp) {
String result=null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buf = md.digest(tmp.getBytes());
if (buf != null && buf.length != 0) {
StringBuilder out = new StringBuilder();
for (byte b : buf) {
out.append(HEX[b >> 4 & 15]).append(HEX[b & 15]);
}
result=out.toString();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return result;
}
private static String hmacSHA1(String var1, String var2, String charset) {
String result = null;
try {
SecretKeySpec signingKey = new SecretKeySpec(var2.getBytes(charset), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] buf = mac.doFinal(var1.getBytes(charset));
if (buf != null && buf.length != 0) {
StringBuilder out = new StringBuilder();
for (byte b : buf) {
out.append(HEX[b >> 4 & 15]).append(HEX[b & 15]);
}
result = out.toString();
}
} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
上一篇: Nginx 模块自主开发一:开发前的了解
下一篇: 实例详解PHP中页面跳转的三种方法