• 快速入门
  • 未来社区底座API
  • 未来社区客户端API
  • 智能硬件接入
  • 附录
  • 工具与资源
  • 附录
    • 里约调用签名参考示例

里约调用签名参考示例

基于postman

postman签名脚本如下:

var token = "你的paasId对应的token";
var arr = new Array(4)
var timestamp = Math.round(new Date().getTime() / 1000)
pm.environment.set("timestamp", timestamp);
console.log(timestamp)
arr[0] = timestamp
arr[1] = token
arr[2] = 'XSP34bIoyc'
arr[3] = timestamp
var str = arr.join("")
var sha256= CryptoJS.SHA256(str).toString();
pm.environment.set('x-rio-signature',sha256)
pm.environment.set('x-rio-timestamp',timestamp)
pm.environment.set('x-rio-nonce', 'XSP34bIoyc')

请求头示意: img.png

Go

/* genRioSign : 生成调用里约网关发布的API时所需要的签名字段x-rio-signature
 * @param {string} timestamp - 时间戳,需要和发起请求时的header:x-rio-timestamp一致
 * @param {string} token - rio网关应用token, 必须和发起请求是的header:x-rio-paasid是一对
 * @param {string} nonce - 随机数,需要和发起请求时的header:x-rio-nonce一致
 */
func genRioSign(timestamp, token, nonce string) string {
    textToSign := timestamp + token + nonce + timestamp
    hashInBytes := sha256.Sum256([]byte(textToSign))
    hashStr := hex.EncodeToString(hashInBytes[:])
    sign := strings.ToUpper(hashStr)
    return sign
}

Java

package cmusdk.rio;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class RioUtil {
    String rioPaasId;
    String rioToken;

    public RioUtil(String paasId, String token) {
        rioPaasId = paasId;
        rioToken = token;
    }

    // 生成x-rio-signature :
    // x-rio-signature = sha256(x-rio-timestamp + Token + x-rio-nonce + x-rio-timestamp)
    public String generateRioSignature(String timestamp, String nonce) {
        String strToSign, sign;
        strToSign = timestamp + this.rioToken + nonce + timestamp;
        sign = getSHA256StrJava(strToSign).toUpperCase(Locale.ROOT);
        return sign;
    }

     // 利用java原生的摘要实现SHA256加密
    private static String getSHA256StrJava(String str) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

    // 将byte转为16进制
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                // 1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}

nodejs

import crypto from "crypto"


/* Generating a signature for a request to a Rio server.
 * @param {string} token - rio网关应用token
 * @param {string} timestamp - 时间戳,需要和发起请求时的header:x-rio-timestamp一致
 * @param {string} nonce - 随机数,需要和发起请求时的header:x-rio-nonce一致
 */
const generateRioSign = function (token, timestamp, nonce) {
    let textToSign =  timestamp + token + nonce + timestamp
    let sign = crypto.createHash("sha256").update(textToSign).digest("hex").toUpperCase()
    return sign
}

export {generateRioSign}