閱讀提示
請先閱讀:大模型會話接口集成概述
計算會話接口簽名
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
public static String getSign(String streamSecret, String timestamp) throws NoSuchAlgorithmException {
String toHash = "streamSecret=" + streamSecret + "×tamp=" + timestamp;
byte[] md5Bytes = md5(toHash);
return bytesToHex(md5Bytes);
}
private static byte[] md5(String text) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
return digest.digest(text.getBytes(StandardCharsets.UTF_8));
}
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
import hashlib
def get_sign(stream_secret, timestamp):
text_to_hash = f"streamSecret={stream_secret}×tamp={timestamp}"
md5_hash = hashlib.md5(text_to_hash.encode('utf-8')).hexdigest()
return md5_hash
<?php
function getSign($streamSecret, $timestamp) {
$toHash = "streamSecret=" . $streamSecret . "×tamp=" . $timestamp;
$md5Bytes = md5($toHash, true); // true 參數表示返回原始的二進制數據
return bytesToHex($md5Bytes);
}
function bytesToHex($bytes) {
$hexArray = str_split('0123456789abcdef');
$hexChars = '';
for ($j = 0; $j < strlen($bytes); $j++) {
$v = ord($bytes[$j]) & 0xFF;
$hexChars .= $hexArray[($v >> 4) & 0x0F];
$hexChars .= $hexArray[$v & 0x0F];
}
return $hexChars;
}
function getCurrentTimestampMillis() {
return round(microtime(true) * 1000);
}
// 使用示例
// $streamSecret = "yourSecret";
// $timestamp = getCurrentTimestampMillis();
// $signature = getSign($streamSecret, $timestamp);
// echo "Signature: " . $signature . "\n";
// echo "Timestamp: " . $timestamp . "\n";
?>
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"time"
)
func getSign(streamSecret, timestamp string) string {
toHash := "streamSecret=" + streamSecret + "×tamp=" + timestamp
md5Bytes := md5.Sum([]byte(toHash))
return hex.EncodeToString(md5Bytes[:]) // 將字節切片轉為十六進制字符串
}
func getCurrentTimestampMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond) // 獲取當前的時間戳(毫秒)
}
// func main() {
// streamSecret := "yourSecret"
// timestamp := fmt.Sprintf("%v", getCurrentTimestampMillis())
// signature := getSign(streamSecret, timestamp)
// fmt.Println("Signature:", signature)
// fmt.Println("Timestamp:", timestamp)
// }
const crypto = require('crypto');
// import * as crypto from 'crypto'; // typescript請使用語句
function getSign(streamSecret, timestamp) {
const toHash = `streamSecret=${streamSecret}×tamp=${timestamp}`;
const md5Hash = crypto.createHash('md5').update(toHash).digest('hex');
return md5Hash;
}
function getCurrentTimestampMillis() {
return new Date().getTime(); // 獲取當前的時間戳(毫秒)
}
// 使用示例
// const streamSecret = "yourSecret";
// const timestamp = getCurrentTimestampMillis().toString(); // 轉換為字符串
// const signature = getSign(streamSecret, timestamp);
// console.log('Signature:', signature);
// console.log('Timestamp:', timestamp);
using System;
using System.Security.Cryptography;
using System.Text;
public class SignatureGenerator
{
// public static void Main(string[] args)
// {
// string streamSecret = "yourSecret";
// string timestamp = GetCurrentTimestampMillis().ToString();
// string signature = GetSign(streamSecret, timestamp);
// Console.WriteLine("Signature: " + signature);
// Console.WriteLine("Timestamp: " + timestamp);
// }
public static string GetSign(string streamSecret, string timestamp)
{
string toHash = "streamSecret=" + streamSecret + "×tamp=" + timestamp;
using (MD5 md5 = MD5.Create())
{
byte[] md5Bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(toHash));
return BytesToHex(md5Bytes);
}
}
private static string BytesToHex(byte[] bytes)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
public static long GetCurrentTimestampMillis()
{
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
}
}
Java(第三方庫)
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
public static String getSign(String streamSecret, String timestamp) {
String toHash = "streamSecret=" + streamSecret + "×tamp=" + timestamp;
byte[] md5Hash = DigestUtils.md5(toHash);
return Hex.encodeHexString(md5Hash);
}
文檔內容是否對您有幫助?