JavaScript (WebCrypto)
// Provably Fair verification — JavaScript reference implementation
// Matches AWPBattle.com backend (apps/backend/src/common/crypto.util.ts)
async function sha256Hex(input) {
const buffer = new TextEncoder().encode(input);
const digest = await crypto.subtle.digest('SHA-256', buffer);
return Array.from(new Uint8Array(digest))
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
}
function hashToUnitFloat(hashHex) {
const slice = hashHex.slice(0, 13);
const intValue = parseInt(slice, 16);
return intValue / 0x10000000000000;
}
async function computeRoll(serverSeed, clientSeed, serverSalt, nonce) {
const message = `${serverSeed}:${clientSeed}:${serverSalt}:${nonce}`;
const hash = await sha256Hex(message);
return { hash, roll: hashToUnitFloat(hash) };
}
// Usage:
// computeRoll('SERVER_SEED', 'CLIENT_SEED', 'SERVER_SALT', 0).then(console.log);