java中常用接口对接加密方式

hmac生成码

/**
*
* @param message 加密的内容
* @param secret 第三方接口提供的密钥
* @return
*/
private static String sha256_HMAC(String message, byte[] secret) {
  String hash = "";
  try {
      Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
      SecretKeySpec secret_key = new SecretKeySpec(secret, "HmacSHA256");
      sha256_HMAC.init(secret_key);
      byte[] bytes = sha256_HMAC.doFinal(message.getBytes(StandardCharsets.UTF_8));
      hash = new String(java.util.Base64.getEncoder().encode(bytes), StandardCharsets.UTF_8);
      System.out.println(hash);
  } catch (Exception e) {
      System.out.println("Error HmacSHA256 ===========" + e.getMessage());
  }
  return hash;
}

md5加密

 @Test
 public void genreateMd5() {
     String message = "chenrui"; //要加密的内容
     String salt = "chenrui";    //随机的盐
     String key = "key";
     String sign = DigestUtils.md5Hex(message+salt+key);
     System.out.println(sign);
 }

猜你喜欢

转载自blog.csdn.net/Hello_Ray/article/details/83055869