JSFiddle - React, Tailwind, and code Playground

by wind_chime18

HTML

<html>
<head>

</head>

<body>

<button onclick="generate()" type="button">Generate</button>
<br><br>
<span id="nonce"></span>

</body>

</html>

JavaScript

function generateRandomNonce(length) {
    const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    let nonce = "";

    for (let i = 0; i < length; i++) {
      const randomIndex = Math.floor(Math.random() * charset.length);
      nonce += charset[randomIndex];
    }

    return nonce;
}

function generate() {
    // Usage example:
    const length = 16; // Change this value to set the desired length of the nonce
    const randomNonce = generateRandomNonce(length);
    document.getElementById('nonce').innerHTML = randomNonce;
}