MessageDigest 消息摘要
例子:
MD5加密:
try{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update("abc".getBytes());
System.out.println("md5(abc)=" + byte2str(md5.digest()));
}catch (NoSuchAlgorithmException e){ }
操作过程:
1、getInstance得到实例
2、传入key和算法参数进行初始化
3、update添加数据
4、doFinal得到结果
例子:
public static String signString(String source, String accessSecret) throws InvalidKeyException, IllegalStateException {
try {
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1"));
byte[] signData = mac.doFinal(source.getBytes("UTF-8"));
return Base64Helper.encode(signData);
} catch (NoSuchAlgorithmException var5) {
throw new RuntimeException("HMAC-SHA1 not supported.");
} catch (UnsupportedEncodingException var6) {
throw new RuntimeException("UTF-8 not supported.");
}
}