相关DEMO
更新时间:2023-05-05
相关DEMO
(一)签名类DEMO
golang
/**
package main
import (
"github.com/google/uuid"
"time"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
)**/
//AK,SK为接入时分配的APPID及秘钥,接入方根据实际情况改变取值
//重要:不要在客户端暴露SK,否则有信息泄露等一系列安全风险
ak := "lf2a69d4dff7dc9f3a462719da8bb943";
sk := "yf4xqjv0bspsrlzh2hq6yxibqauvaciq";
//nonce跟据业务实际情况指定
nonce := uuid.New().String()
//时间戳为精确到秒,故JAVA需要转换
timestamp := time.Now().Unix()
signType := "HMAC-SHA256";
contentMd5 := "57e37568a871d537d25cd19a9dc10cb7";
//此种拼接效率效低,这里为了更好的展示,故采用字符串相加方式,业务式可根据自己的需求调整算法
joinedString := ak + "&" + timestamp + "&" + nonce + "&" + signType + "&" + contentMd5
//以sk为密钥进行hmac_sha256加密,业务方可自行选择类库,这里只列出一种方式
keys := []byte(sk)
h := hmac.New(sha256.New,keys)
h.Write([]byte(joinedString))
sign := hex.EncodeToString(h.Sum(nil))
java
//AK,SK为接入时分配的APPID及秘钥,接入方根据实际情况改变取值
//重要:不要在客户端暴露SK,否则有信息泄露等一系列安全风险
String ak = "lf2a69d4dff7dc9f3a462719da8bb943";
String sk = "yf4xqjv0bspsrlzh2hq6yxibqauvaciq";
//nonce跟据业务实际情况指定
String nonce = UUIDGenerator.getUUID();
String timestamp = String.valueOf(date.getTime()/1000);
String signType = "HMAC-SHA256";
String contentMd5 = "57e37568a871d537d25cd19a9dc10cb7";
//此种拼接效率效低,这里为了更好的展示,故采用字符串相加方式,业务式可根据自己的需求调整算法
String joinedString = ak + "&" + timestamp + "&" + nonce + "&" + signType + "&" + contentMd5
//以sk为密钥进行hmac_sha256加密,业务方可自行选择类库,这里只列出一种方式
ac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256_HMAC.init(sk);
byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
String sign = sb.toString();
php
//AK,SK为接入时分配的APPID及秘钥,接入方根据实际情况改变取值
//重要:不要在客户端暴露SK,否则有信息泄露等一系列安全风险
$ak = "lf2a69d4dff7dc9f3a462719da8bb943";
$sk = "yf4xqjv0bspsrlzh2hq6yxibqauvaciq";
//nonce跟据业务实际情况指定
$nonce = com_create_guid();
$timestamp = time();
$signType = "HMAC-SHA256";
$contentMd5 = "57e37568a871d537d25cd19a9dc10cb7";
$joinedString = $ak ."&" . $timestamp . "&" . $nonce . "&" . $signType . "&" . $contentMd5
$sign = hash_hmac('sha256',$joinedString, $sk);
(二)BODY参数拼接及content-md5算法
假设当前接口需求参数为 a为"abc", b为123, c为"中文示例 ",则拼接口body,及计算header中的content-md5各语言demo如下 (Content-Type为application/json的情况下,其他类型根据接口具体要求调整)
golang
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
)
type Content struct {
A string `json:"a"`
B int32 `json:"b"`
C string `json:"c"`
}
type Body struct {
BxeoContent Content `json:"bxeoContent"`
}
func main() {
bodyArr := &Body{Content{A:"abc",B:123,C:"中文示例",}
//body 为请求body的内容 ,此时为{"bxeoContent":{"a":"abc","b":123,"c":"中文示例"}}
body, _ := json.Marshal(bodyArr)
//md5body为准备进行md5计算的内容
md5Body,_ := json.Marshal(bodyArr.BxeoContent)
//contentMd5为header中md5的值
contentMd5 := md5.Sum(md5Body)
}
php
$arrParams = array();
$arrParams['a'] = "abc";
$arrParams['b'] = 123;
$arrParams['c'] = "中文示例";
$arrBody = array(
'bxeoContent' => $arrParams,
);
$body= json_encode($arrBody);
$contentMd5 = md5(stripslashes(json_encode($arrParams, JSON_UNESCAPED_UNICODE)));