java hmacMD5加密笔记

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.SecrerKeySpec;

public class TestHmacMd5{
  public static void main(String[] args)throws Exception{
    SecretKey sk = new SecretKeySpec("TEST".getBytes("UTF-8"),"HmacMD5");
    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(sk);
    byte[] result = mac.doFinal("12345TEST".getBytes("UTF-8"));
    for(byte b : result){
       if(Integer.toHexString(0xFF & b).length()==1)
      System.out.print("0"+Integer.toHexString(OxFF & b));
   else
      System.out.print(Integer.toHexString(OxFF & b));
    }
  }

}

你可能感兴趣的:(java hmacMD5加密笔记)