Session API signature
Updated at:
Before you begin
Read Large model session API integration overview.
Calculate session API signature
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); // The 'true' parameter returns raw binary data.
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);
}
// Example usage
// $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[:]) // Convert the byte slice to a hexadecimal string.
}
func getCurrentTimestampMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond) // Returns the current timestamp in milliseconds.
}
// 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'; // For TypeScript, use this import statement.
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(); // Returns the current timestamp in milliseconds.
}
// Example usage
// const streamSecret = "yourSecret";
// const timestamp = getCurrentTimestampMillis().toString(); // Convert to a string.
// 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 (third-party library)
<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);
}Is this page helpful?