请求签名值计算方法

更新时间:2020-10-13 15:24:57

说明

将请求参数的JSON字符串作为待加密明文信息用projectSecret作为密钥通过HmacSHA256哈希算法进行请求签名值(X-timevale-signature)的计算。

如果是POST请求方式时,是对请求Body体内的JSON字符串进行加签。

如:

请求Body体内JSON字符串为

{"sceneName":"房屋租赁行业子目录证据链创建","sceneTemplateId":"64b9c14c-a7fd-4141-93b6-d62a238b8fb2","linkIds":[]}


如果是“拼接用于查看存证证明的跳转URL”时GET请求方式时,是对参数进行加签。

如:

id=C918397270204903425&projectId=1111563517&timestamp=1510987348003&reverse=false&type=CODE_USC&number=591101009HEC6BFG3T

获取示例

图解示例如下:

参考网址:http://tool.oschina.net/encrypt?type=2

如果是“拼接用于查看存证证明的跳转URL”时GET请求方式时,是对拼接的字符串进行加签

JAVA获取示例

    public static void main(String[] args) {

        String PROJECT_ID = "项目id";
        String ALGORITHM = "HmacSHA256";
        String ENCODING = "UTF-8";
        String PROJECT_SECRET = "密钥";
        // 请求签名值
        String signature = getSignature(param_json.toString(), PROJECT_SECRET, ALGORITHM, ENCODING);
        System.out.println("请求签名值 = " + signature);
        
    }

    /***
     * 获取请求签名值
     * 
     * @param data      加密前数据
     * @param key       密钥
     * @param algorithm HmacMD5 HmacSHA1 HmacSHA256 HmacSHA384 HmacSHA512
     * @param encoding  编码格式
     * @return HMAC加密后16进制字符串
     * @throws Exception
     */
    public static String getSignature(String data, String key, String algorithm, String encoding) {
        Mac mac = null;
        try {
            mac = Mac.getInstance(algorithm);
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(encoding), algorithm);
            mac.init(secretKey);
            mac.update(data.getBytes(encoding));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            return null;
        }
        return byte2hex(mac.doFinal());
    }

    /***
     * 将byte[]转成16进制字符串
     * 
     * @param data
     * 
     * @return 16进制字符串
     */
    public static String byte2hex(byte[] data) {
    StringBuilder hash = new StringBuilder();
    String stmp;
    for (int n = 0; data != null && n < data.length; n++) {
        stmp = Integer.toHexString(data[n] & 0XFF);
        if (stmp.length() == 1)
        hash.append('0');
        hash.append(stmp);
    }
    return hash.toString();
    }


JS获取示例

//请求数据data
var data = JSON.stringify(request.data);
data = data.substr(1, data.length - 2).replace(/\\n/g, "\n").replace(/\\t/g, "\t").replace(/\\r/g, "\r").replace(/\\"/g, "\"");
//获取签名值
var signature = CryptoJS.HmacSHA256(data, "密钥").toString();


PHP获取示例


//对于请求数据data 转为json字符串
$pa=json_encode($param);
$signature =  getSignature($pa,PROJECT_SECRET(密钥));
/计算请求签名值
function getSignature($message, $projectSecret) {
    $signature = hash_hmac('sha256', $message, $projectSecret, FALSE);
    return $signature;
}


.NET获取示例

// 请求参数-JSON字符串
  string data = jsonObject.ToString();
            // 请求签名值
  string signature =GetSignature(data, SceneConfig.PROJECTSECRET);
具体方法:
/// <summary>
        ///  HmacSHA256 加密
        /// </summary>
        /// <param name="secret">projectSecret</param>
        /// <param name="data">请求的JSON参数</param>
        /// <returns></returns>
        public static string GetSignature(string data, string secret)
        {
            byte[] keyByte = Encoding.UTF8.GetBytes(secret);
            byte[] messageBytes = Encoding.UTF8.GetBytes(data);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                StringBuilder sb = new StringBuilder();
                foreach (byte test in hashmessage)
                {
                    sb.Append(test.ToString("X2"));
                }
                return sb.ToString();
            }
        }
    }



我要纠错