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."); } }

