import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; public class EncodeTest { public static void main(String[] args) throws Exception { SecretKey key = new SecretKeySpec("secretkey".getBytes(), "HmacMD5"); Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); byte[] code = mac.doFinal("the message need sign".getBytes()); System.out.println(Hex.encodeHex(code)); } }
新博客地址