美文网首页
java使用HMAC-SHA256算法实现接口认证

java使用HMAC-SHA256算法实现接口认证

作者: haiyong6 | 来源:发表于2022-09-03 14:10 被阅读0次

    HMAC-SHA256

    HMAC算法利用哈希运算,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。其安全性是建立在Hash加密算法基础上的。它要求通信双方共享密钥、约定算法、对报文进行Hash运算,形成固定长度的认证码。通信双方通过认证码的校验来确定报文的合法性。HMAC算法可以用来作加密、数字签名、报文验证等。

    简单说来,HMAC-SHA256在sha256的基础上加了密钥加密,比如要给第三方提供一个接口,可以用HMAC-SHA256来约定固定的算法,生成一个密钥,提供给对方,对方可以根据约定的算法利用密匙对请求加密,我方收到请求后,用同样的算法利用密钥加密,之后比对是否一致,如果一致则达到校验身份的目的。
    比如我方提供一个接口,先给对方生成一个acountName和密钥key,我方提供的接口里的请求头有header1,header2,header3等等属性,那么就可以约定一个算法:StringToSign = header1+"\n" + header2 +"\n"+ header3+"\n" + acountName+"\n" + timestamp时间戳;
    然后对StringToSign字符串进行HMAC-SHA256加密,密钥是我方提供的key,得到一个signed字符串,然后往请求头里增加一个属性,比如是authorization:acountName:signed
    如此,接收到请求的时候我方根据此算法计算出signed的值,比对是否一致,如果一致则通过校验处理请求,如果没有通过校验则返回校验身份不通过,从而实现身份校验功能。

    贴上java实现HMAC-SHA256代码

    package com.ly.mp.project.utils;
    
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    
    public class HmacSHA256Util {
         /**
         * HmacSHA256算法,返回的结果始终是32位
         * @param key 加密的键,可以是任何数据
         * @param content 待加密的内容
         * @return 加密后的内容
         * @throws Exception
         */
        public static byte[] hmacSHA256(byte[] key,byte[] content) throws Exception {
            Mac hmacSha256 = Mac.getInstance("HmacSHA256");
            hmacSha256.init(new SecretKeySpec(key, 0, key.length, "HmacSHA256"));
            byte[] hmacSha256Bytes = hmacSha256.doFinal(content);
            return hmacSha256Bytes;
        }
    
        /**
         * 将加密后的字节数组转换成字符串
         *
         * @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 hmacSHA256(String secret, String message) throws Exception {
            String hash = "";
            Mac hmacSha256 = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            hmacSha256.init(secret_key);
            byte[] bytes = hmacSha256.doFinal(message.getBytes());
            hash = byteArrayToHexString(bytes);
            return hash;
        }
    
        public static void main(String[] args) {
            String appSecret = "b06c75b58d1701ff470119a4114f8b45";
            String appId = "10000001";
            String timestamp = "1529853639000";
    
    //        HMAC-SHA256(appSecret,appId + timestamp+去除节点间的空格及换行符的请求体)
            String message = appId + timestamp + "<req><orderId>91303183862452</orderId><notifyUrl></notifyUrl><refundTime>2013-03-22 16:25:26</refundTime><refundFee>24.5</refundFee><refundNo>14252414245</refundNo></req>";
            System.out.println("加密前 =========>"+message);
            String str = "";
            try{
                str = HmacSHA256Util.hmacSHA256(appSecret,message);
                System.out.println("加密后 =========>"+str);
            }catch (Exception e){
                System.out.println("Error HmacSHA256 ===========>" + e.getMessage());
            }
    
        }
    }
    

    运行结果如下:

    加密前 =========>100000011529853639000<req><orderId>91303183862452</orderId><notifyUrl></notifyUrl><refundTime>2013-03-22 16:25:26</refundTime><refundFee>24.5</refundFee><refundNo>14252414245</refundNo></req>
    加密后 =========>856bf51d4a3c2f0f9d8a794688291ba6f37d5551d50f61c23f8cdc7df0900c1f
    

    参考:
    https://blog.csdn.net/sdnyqfyqf/article/details/105534376
    https://blog.csdn.net/lvshu_yuan/article/details/112789111

    相关文章

      网友评论

          本文标题:java使用HMAC-SHA256算法实现接口认证

          本文链接:https://www.haomeiwen.com/subject/ipjdbrtx.html