生产签名进行 Base64 编码

第1步:构造签名内容,签名内容为 head 所有内容和 body 所有内容拼接起来。考虑到XML节点的无序性,在拼装时按照节点名称字母进行正序排列(即从a到z),举例:首字母相同的,按照处于第2位的进行排序;以此类推。且组成的签名内容中不能包含空格、换行等字符。

第2步:生产签名值sign,使用 HMAC-SHA1 加密算法,将第1步中的内容进行加密

第3步:对加密后的内容进行 Base64 编码。

 public static String genHMAC(String data) {
        String key = "9fKmCAhF6FGIXSE8ZG46gsVBA5bIHuvl";
        byte[] result = null;
        try {
            SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signinKey);
            byte[] rawHmac = mac.doFinal(data.getBytes());
            result = Base64.encodeBase64(rawHmac);
        } catch (NoSuchAlgorithmException e) {
            System.err.println(e.getMessage());
        } catch (InvalidKeyException e) {
            System.err.println(e.getMessage());
        }
        if (null != result) {
            return new String(result);
        } else {
            return null;
        }
    }

猜你喜欢

转载自blog.csdn.net/guanttt/article/details/81114693