文档中心 > 签名Signature生成规则说明
签名Signature生成规则说明

最近更新时间:2023-05-18

签名生成规则

第一步:将Signature外其他的所有请求参数封装成一个json,参数名作为json的key,参数值作为该key的value,比如要访问的路径为(Signature的值已经给出):

http://api.unicloud.com/product?Action=GetRegion&ProductCode=VM&AccessKeyId=0E9vFY4xL4PvaDil&Signature=dKvipmrF27bAkLldtHjx1bQFl1c%3D,

Signature外的请求参数有三个:

1.Action=GetRegion,

2.ProductCode=VM,

3.AccessKeyId=0E9vFY4xL4PvaDil,则封装json的伪代码为:

json.put("Action","GetRegion");

json.put("ProductCode","VM");

json.put("AccessKeyId","0E9vFY4xL4PvaDil");


第二步:以json和您的access_secret作为参数,使用签名生成工具类生成签名:Signature=dKvipmrF27bAkLldtHjx1bQFl1c%3D,拼接上访问即可。

签名生成工具类

package com.unicloud.framework.transaction.utils;


import com.alibaba.fastjson.JSONObject;

import com.google.common.base.Joiner;

import org.apache.commons.codec.binary.Base64;


import javax.crypto.Mac;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.List;

import java.util.stream.Collectors;


/**

 * @Desc 签名生成工具类

 * @Version 1.0

 */

public class GenerateSignatureTool {


    private static String ak = "Xnt0niaAeiz1t1Ly";

    private static String sk = "rngdawvIVJBOu0g2FI08LbJayvnO3j";


    public static void main(String[] args) throws Exception{

        // 查询计费方式

        JSONObject params = new JSONObject();

        params.put("Action","GetProductPaymentList");

        params.put("RegionId","cn-beijing");

        params.put("ProductCode","VM");

        params.put("AccessKeyId", ak);

        System.out.println(generateSignature(params, sk));

    }


    /**

     * 生成签名

     *

     * @param params

     * @param sk

     * @return

     * @throws Exception

     */

    public static String generateSignature(JSONObject params, String sk) throws Exception{

        ListkeyValueList = params.keySet().stream().map(key -> key + "=" + encode(params.get(key))).map(GenerateSignatureTool::encode).sorted().collect(Collectors.toList());

        String formatQueryString = Joiner.on("%26").join(keyValueList);

        String stringToSign = Joiner.on("&").join("GET", URLEncoder.encode("/", "utf-8"), formatQueryString);

        String hmac = HmacSHA1Encrypt(stringToSign, sk + "&");

        return new StringBuilder("Signature=").append(encode(hmac)).toString();

    }


    private static String HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {

        SecretKey secretKey = new SecretKeySpec(encryptKey.getBytes(), "HmacSHA1");

        Mac mac = Mac.getInstance("HmacSHA1");

        mac.init(secretKey);

        byte[] rawHmac = mac.doFinal(encryptText.getBytes());

        return new String(Base64.encodeBase64(rawHmac));

    }


    private static String encode(Object s) {

        try {

            return s != null ? URLEncoder.encode(String.valueOf(s), "UTF-8").replace("+", "%20")

                    .replace("*", "%2A").replace("%7E", "~") : null;

        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

            return "";

        }

    }

}