文档

会话接口签名

更新时间:

阅读提示

请先阅读:大模型会话接口集成概述

计算会话接口签名

private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();

public static String getSign(String streamSecret, String timestamp) throws NoSuchAlgorithmException {
    String toHash = "streamSecret=" + streamSecret + "&timestamp=" + 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}&timestamp={timestamp}"
    md5_hash = hashlib.md5(text_to_hash.encode('utf-8')).hexdigest()
    return md5_hash
<?php

function getSign($streamSecret, $timestamp) {
    $toHash = "streamSecret=" . $streamSecret . "&timestamp=" . $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 + "&timestamp=" + 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}&timestamp=${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 + "&timestamp=" + 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 + "&timestamp=" + timestamp;
    byte[] md5Hash = DigestUtils.md5(toHash);
    return Hex.encodeHexString(md5Hash);
}

  • 本页导读 (0)