• HMACSHA256加密


    package com.lh.micro.datasource.util;
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;

    public class HMACSHA256 {


    /**
    * 将加密后的字节数组转换成字符串
    *
    * @param b 字节数组
    * @return 字符串
    */
    public static String byteArrayToHexString(byte[] b) {
    StringBuilder hs = new StringBuilder();
    String stmp;
    for (int n = 0; b!=null && n < b.length; n++) {
    stmp = Integer.toHexString(b[n] & 0XFF);
    if (stmp.length() == 1)
    hs.append('0');
    hs.append(stmp);
    }
    return hs.toString().toLowerCase();
    }
    /**
    * sha256_HMAC加密
    * @param message 消息
    * @param secret 秘钥
    * @return 加密后字符串
    */
    public static String sha256_HMAC(String message, String secret) {
    String hash = "";
    try {
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);
    byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
    hash = byteArrayToHexString(bytes);
    } catch (Exception e) {
    System.out.println("Error HmacSHA256 ===========" + e.getMessage());
    }
    return hash;
    }


    }
  • 相关阅读:
    UVA 10131题解
    算法常见概念
    图算法概论
    POJ 1654 area 解题
    大数阶乘的位数和精确值计算
    printf()与 scanf()
    想编程之美竞赛
    所感所想
    Git 入门和常用命令详解
    使用crypto模块实现md5加密功能(解决中文加密前后端不一致的问题)
  • 原文地址:https://www.cnblogs.com/guokezhiren/p/9103382.html
Copyright © 2020-2023  润新知