签名说明
介绍
SHA1(appId+ appSecret + timestamp),将该三个参数拼接的字符串进行 SHA1 哈希计算从而生成 16 进制字符。
签名生成示例
package treasury;
import java.security.MessageDigest;
import java.math.BigInteger;
public class sha1 {
public static void main(String[] args) throws Exception {
// Parameters
String appId = "***";
String appSecret = "***";
// timestamp
long timestamp = System.currentTimeMillis();
System.out.println("Timestamp: " + timestamp);
// Concatenate the parameters into a single string
String stringToHash = appId + appSecret + timestamp;
System.out.println("sign Content (before hashing): " + stringToHash);
// Generate SHA1 hash
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = digest.digest(stringToHash.getBytes("UTF-8"));
String hash = String.format("%040x", new BigInteger(1, hashBytes));
System.out.println("Generated sign: " + hash);
}
}
const crypto = require('crypto');
function generateSHA1Signature(appId, appSecret) {
// timestamp
const timestamp = Date.now();
console.log("Timestamp: " + timestamp);
// Concatenate the parameters into a single string
const stringToHash = appId + appSecret + timestamp;
console.log("sign Content (before hashing): " + stringToHash);
// Generate SHA1 hash
const hash = crypto.createHash('sha1').update(stringToHash).digest('hex');
console.log("Generated sign: " + hash);
}
// Example usage
const appId = "***";
const appSecret = "***";
generateSHA1Signature(appId, appSecret);
<?php
// Parameters
$appId = "***"; // appId
$appSecret = "***"; // appSecret
// timestamp
$timestamp = strval(time() * 1000);
echo "Timestamp: " . $timestamp . PHP_EOL;
// Concatenate the parameters into a single string
$stringToHash = $appId . $appSecret . $timestamp;
echo "sign Content (before hashing): " . $stringToHash . PHP_EOL;
// Generate SHA1 hash
$hash = sha1($stringToHash);
echo "Generated sign: " . $hash;
?>
Updated 22 days ago