图普公钥见tupu_public_key
<?php
function sign($secretId, $timestamp, $nonce, $privateKey): string
{
$sign_string = $secretId . "," . $timestamp . "," . $nonce;
openssl_sign($sign_string, $signature, $privateKey, OPENSSL_ALGO_SHA256);
return base64_encode($signature);
}
function verify($signature, $json, $publicKey): bool|int
{
$verification = openssl_verify($json, base64_decode($signature), $publicKey, "sha256WithRSAEncryption");
return $verification;
}
function testSign(): void
{
$timestamp = time();
$secretId = "your_secret_id";
$nonce = rand(100, 999999);
$privateKey = file_get_contents('./my_private_key.pem');
$signature = sign($secretId, $timestamp, $nonce, $privateKey);
echo $signature;
}
function testVerify(): void
{
try {
require "./mockService.php";
$mockResponse = mockService();
} catch (Exception) {
$mockResponse = array(
"json" => "{\"5c8213b9bc807806aab0a574\":{\"speechs\":[{\"name\":\"13.mp3\",\"label\":1,\"review\":false,\"details\":[{\"startTime\":5,\"endTime\":10,\"label\":1,\"rate\":0.9640088528394699},{\"startTime\":35,\"endTime\":40,\"label\":1,\"rate\":0.7449841499328613}]},{\"name\":\"za8gq-zb1kt.wma\",\"label\":0,\"review\":false,\"details\":[]}]},\"code\":0,\"message\":\"success\",\"nonce\":\"0.01627771095362096\",\"timestamp\":1552391372490}",
"signature" => "signature_from_tupu_service"
);
}
$tupu_public_key = file_get_contents('./tupu_public_key.pem');
$verification = verify($mockResponse["signature"], $mockResponse["json"], $tupu_public_key);
echo $verification;
}
testSign();
// eg :f6RX3dJFUf/zd3EaM68df+u9b8pSJzKJUcQsJGlNWP4iOf7w3V0plNO1ub4ZkRtr4CkDHl2WzjNhpIpa5jHMQ5ewv1K9vYb08dnRKOmEFSqDnka0wYGtR7Spf23wf9xwEdJQPg5cLJG8ze3Hr2i5zwx+hf4dzR+zF8eumEBGuko=
echo "\n";
testVerify();
// eg: 0 , 1 or -1
// 0: incorrect
// 1: correct
// -1: error