图普公钥见tupu_public_key
const crypto = require("crypto")
const fs = require("fs");
function sign(secretId, timestamp, nonce, privateKey) {
// 1、push all sign params, by order: secretId, timestamp, nonce
// 2、sign with 'RSA-SHA256' algorithms, and out put result in 'base64' format
let sign_string = [secretId, timestamp, nonce].join(",")
let signer = crypto.createSign("RSA-SHA256")
signer.update(sign_string, "utf-8")
let signature = signer.sign(privateKey, "base64")
return signature
}
function verify(signature, json, publicKey) {
let verifier = crypto.createVerify('RSA-SHA256')
verifier.update(json, 'utf-8')
let verification = verifier.verify(publicKey, signature, 'base64')
return verification
}
function getKeyByFile(filename) {
let myPrivateKeyPath = `${__dirname}/${filename}`
return fs.readFileSync(myPrivateKeyPath).toString()
}
function testSign() {
let secretId = "your_secret_id" // your secretId
let myPrivateKey = getKeyByFile("my_private_key.pem") // your private key
let timestamp = Math.round(new Date().getTime() / 1000)
let nonce = Number(Math.random()).toString()
let signature = sign(secretId, timestamp, nonce, myPrivateKey)
console.log(signature)
}
function testVerify() {
let mockResponse
try {
const {tupuService} = require("./mockTupuService")
mockResponse = tupuService()
} catch {
mockResponse = {
"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"
}
}
let TUPU_PUBLIC_KEY = getKeyByFile("tupu_public_key.pem") // tupu's public key
let verification = verify(mockResponse.signature, mockResponse.json, TUPU_PUBLIC_KEY)
console.log(verification)
}
function main() {
testSign()
// eg: NC8Q0LR0MvKFYC93CrHQZkHGUzRa8qt3dUIix/NOyNX5ELfuq+LAVZ5dfxTI8JKThH4feqJAiYGxwJaRqFzkOHsHVZBMbimFVRp8tQ+I/m7riy+PhpSHf+E08roFi/pzvTrzF/CUUJdKFLvKBS6wyKkuPo4wIbdbvXxzcStSnZc=
testVerify()
// eg: true or false
}
main()