更新时间:2022-04-14 03:28:04下载pdf
您在向云开发平台发送调用请求时,都需要提供签名(Sign)进行身份验证。文本介绍如何构建云开发平台 API 签名。
涂鸦采用 HMAC-SHA256 方法创建摘要。针对不同的 API,当前提供两套签名算法。
适用范围:获取令牌和刷新令牌 API。
签名算法: sign = HMAC-SHA256(client_id + t, secret).toUpperCase()
算法逻辑:
参数名 | 参数值 |
---|---|
client_id | 1KAD46OrT9HafiKdsXeg |
secret | 4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC |
t | 1588925778000 |
access_token | 3f4eda2bdec17232f67c0b188af3eec1 |
拼接待签名字符串。
1KAD46OrT9HafiKdsXeg1588925778000
通过哈希摘要获得新字符。
HMAC-SHA256(1KAD46OrT9HafiKdsXeg1588925778000,4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC)
ceaafb5ccdc2f723a9fd3e91d3d2238ee0dd9a6d7c3c365deb50fc2af277aa83
转换为大写。
CEAAFB5CCDC2F723A9FD3E91D3D2238EE0DD9A6D7C3C365DEB50FC2AF277AA83
拼接待签名字符串:
1KAD46OrT9HafiKdsXeg3f4eda2bdec17232f67c0b188af3eec11588925778000
通过哈希摘要获得新字符。
哈希摘要:HMAC-SHA256(1KAD46OrT9HafiKdsXeg3f4eda2bdec17232f67c0b188af3eec11588925778000,4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC)
新字符串: 36c30e300f226b68add014dd1ef56a81edb7b7a817840485769b9d6c96d0faa1
转换为大写。
36C30E300F226B68ADD014DD1EF56A81EDB7B7A817840485769B9D6C96D0FAA1
/**
Run the code online with this jsfiddle. Dependent upon an open source js library calledhttp://code.google.com/p/crypto-js/.
**/
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-base64.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/hmac-sha256.min.js"></script>
<script>
var hash = CryptoJS.HmacSHA256("Message", "secret");
var hashInBase64 = hash.toString().toUpperCase();
document.write(hashInBase64);
</script>
/**
PHP has built in methods for hash_hmac (PHP 5) and base64_encode (PHP 4, PHP 5) resulting in no outside dependencies. Say what you want about PHP but they have the cleanest code for this example.
**/
$s = strtoupper(hash_hmac("sha256", "Message", 'secret'));
echo var_dump($s);
/**
Dependent on Apache Commons Codec to encode in base64.
**/
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class ApiSecurityExample {
public static void main(String[] args) {
try {
String secret = "secret";
String message = "Message";
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
String hash = new HexBinaryAdapter().marshal(bytes).toUpperCase();
System.out.println(hash);
}
catch (Exception e){
System.out.println("Error");
}
}
}
using System;
using System.Security.Cryptography;
namespace Test
{
public class MyHmac
{
public static string Encrypt(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < hashmessage.Length; i++)
{
builder.Append(hashmessage[i].ToString("x2"));
}
return builder.ToString().ToUpper();
}
}
}
}
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈