String bs= "逆向有你a";
SecretKeySpec key = new SecretKeySpec("12345".getBytes(StandardCharsets.UTF_8), "HmacSHA256");//申请一个密钥,长度内容随便写
Mac mac = Mac.getInstance("HmacSHA256");//告诉系统我用的加密码方式是HmacSHA256
mac.init(key);
byte[] macres = mac.doFinal(bs.getBytes(StandardCharsets.UTF_8));//开始加密
System.out.println("HmacSHA1加密(字节):"+Arrays.toString(macres));
System.out.println("HmacSHA1加密(字符串):"+bytes2HexString(macres));
SecretKeySpec key1 = new SecretKeySpec("12345".getBytes(StandardCharsets.UTF_8), "HmacSHA256");//申请一个密钥,长度内容随便写
Mac mac1 = Mac.getInstance("HmacSHA256");//告诉系统我用的加密码方式是HmacSHA256
mac1.init(key1);
mac1.update("逆向".getBytes(StandardCharsets.UTF_8));
mac1.update("有你".getBytes(StandardCharsets.UTF_8));
byte[] macres1 = mac1.doFinal("a".getBytes(StandardCharsets.UTF_8));
System.out.println(bytes2HexString(macres1));
运行结果:
HmacSHA1加密(字节):[9, 12, 15, -104, -34, 47, 127, -100, 1, 11, -92, 24, -101, 103, -45, -123, -45, 92, -2, -65, -29, 119, 111, 32, -1, 55, 10, -69, 66, -112, -68, -104]
HmacSHA1加密(字符串):090C0F98DE2F7F9C010BA4189B67D385D35CFEBFE3776F20FF370ABB4290BC98
090C0F98DE2F7F9C010BA4189B67D385D35CFEBFE3776F20FF370ABB4290BC98