签名代码示例
Go 语言
import (
"bytes"
"crypto/sha1"
"fmt"
"sort"
)
// 生成签名
func CalSignature(token, timestamp, nonce, data string) string {
sortArr := []string{token, timestamp, nonce, data}
sort.Strings(sortArr)
var buffer bytes.Buffer
for _, value := range sortArr {
buffer.WriteString(value)
}
sha := sha1.New()
sha.Write(buffer.Bytes())
signature := fmt.Sprintf("%x", sha.Sum(nil))
return signature
}
Java 语言
import java.security.MessageDigest;
import java.util.Arrays;
public class Sha1Util {
// 生成签名
public static String calSignature(String token, String timestamp, String nonce, String data) {
String[] arr = new String[]{token, timestamp, nonce, data};
Arrays.sort(arr);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
}
return getSha1(sb.toString());
}
// sha1签名算法
public static String getSha1(String str) {
if (str == null || str.length() == 0) {
return null;
}
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("UTF-8"));
byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
return null;
}
}
}
C++ 语言
#include <stdio.h>
#include <string>
#include <algorithm>
#include <openssl/sha.h>
std::string CalSignature(const std::string &token, const std::string ×tamp, const std::string &nonce, const std::string &data)
{
std::string sortArr[] = {token, timestamp, nonce, data};
sort(sortArr, sortArr+4);
std::string buffer;
for (int i = 0; i < 4; i++)
buffer += sortArr[i];
printf("%s\n", buffer.c_str());
unsigned char obuf[21] = {0};
SHA1((unsigned char *)buffer.c_str(), buffer.length(), obuf);
char hexbuf[41] = {0};
for(int i = 0; i < 20; i++)
sprintf(&hexbuf[2*i], "%02x", obuf[i]);
return std::string(hexbuf);
}
PHP(PHP5.4+)
function calSignature($data, $timestamp, $nonce, $token){
$list = [$data, $timestamp, $nonce, $token];
sort($list,SORT_STRING);
$signature = sha1(implode('', $list));
return $signature;
}
Python 语言
import hashlib
def cal_signature(token, timestamp, nonce, data):
vars = [token,timestamp,nonce,data]
vars.sort()
delimiter=''
s = delimiter.join(vars)
sign = hashlib.sha1(str.encode(s)).hexdigest()
return sign
下载 腾讯会议API签名代码示例-腾讯会议.pdf
文章内容是否对您有帮助?
有帮助
无帮助
能否告知我们这篇文章的问题?(选填)