|
发表于 2024-9-10 00:28:53
|
显示全部楼层
海南省海口市
function calculateSignature(secret,nonce) {
//这两个扔上去就可以了
//const secret = document.getElementById('secret').value;
//const nonce = document.getElementById('nonce').value;
// Convert Base64 to binary
function base64ToArrayBuffer(base64) {
const binaryString = window.atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
// Convert binary to Base64
function arrayBufferToBase64(buffer) {
const binary = Array.from(new Uint8Array(buffer))
.map(byte => String.fromCharCode(byte))
.join('');
return window.btoa(binary);
}
async function hashSHA256(secret, nonce) {
const secretBytes = base64ToArrayBuffer(secret);
const nonceBytes = base64ToArrayBuffer(nonce);
const combined = new Uint8Array(secretBytes.byteLength + nonceBytes.byteLength);
combined.set(new Uint8Array(secretBytes), 0);
combined.set(new Uint8Array(nonceBytes), secretBytes.byteLength);
const hashBuffer = await crypto.subtle.digest('SHA-256', combined);
return arrayBufferToBase64(hashBuffer);
}
hashSHA256(secret, nonce).then(signedNonce => {
document.getElementById('result').textContent = signedNonce;
}).catch(error => {
document.getElementById('result').textContent = 'Error: ' + error.message;
});
} |
|