top of page

Wix Password Generator: Web Crypto and Velo Integration

Writer: Faysal Jaali
Faysal Jaali
Feb 28, 2024
5 min read

Updated: 4 days ago

A password generator needs an unpredictable random source. JavaScript’s Math.random() is not designed for security-sensitive randomness. MDN recommends Web Crypto’s getRandomValues() for cryptographically strong random values.


For a Wix website, a small HTML component can generate a password inside the visitor’s browser. The example below keeps the generated value inside that component rather than sending it to a backend, analytics event or CMS collection.


UI Password Generator Page of CodeMasters Agency
Original password-generator interface example. Use the Web Crypto implementation in this updated guide.

Choose the right implementation boundary


Velo remains useful for the surrounding website, but the random-generation function does not need a server call. Put this widget in an HTML embed component and test it on the published HTTPS site. Do not paste its DOM code directly into $w page code.


The sample is a standalone component. It does not create member accounts, change existing passwords or replace Wix’s authentication system.


Add the browser-based widget


Use a Wix HTML embed that accepts code, paste the following document and give the component enough height to display its labels, controls and status message.


<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Password generator</title>
<label for="length">Password length (12–128)</label>
<input id="length" type="number" min="12" max="128" value="20">
<button id="generate" type="button">Generate password</button>
<label for="result">Generated password</label>
<input id="result" type="text" readonly autocomplete="off" spellcheck="false">
<p id="status" role="status"></p>
<script>
function generatePassword(length = 20) {
  if (!Number.isInteger(length) || length < 12 || length > 128) {
    throw new Error("Choose a whole-number length from 12 to 128.");
  }
  if (!globalThis.crypto?.getRandomValues) {
    throw new Error("Web Crypto is unavailable in this browser.");
  }
  const alphabet =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@$%*-_";
  const limit = 256 - (256 % alphabet.length);
  let password = "";
  while (password.length < length) {
    const bytes = new Uint8Array(length);
    crypto.getRandomValues(bytes);
    for (const byte of bytes) {
      if (byte < limit) password += alphabet[byte % alphabet.length];
      if (password.length === length) break;
    }
  }
  return password;
}
document.getElementById("generate").addEventListener("click", () => {
  const result = document.getElementById("result");
  const status = document.getElementById("status");
  result.value = "";
  try {
    result.value = generatePassword(Number(document.getElementById("length").value));
    status.textContent = "Password generated locally. Save it in your password manager.";
  } catch (error) {
    status.textContent = error.message;
  }
});
</script>
</html>

The function accepts lengths from 12 to 128 characters and defaults to 20. Those limits are design choices for this example, not a statement about every website’s password policy.


Why the rejection step matters


The alphabet has fewer than 256 characters. Mapping every random byte directly with a remainder would make some characters slightly more likely when the alphabet size does not divide 256 evenly.


This example discards bytes above a calculated limit before mapping them to the alphabet. That keeps the accepted character choices uniform. It also stops with an error when Web Crypto is unavailable instead of falling back to a weaker random source.


The generator samples from its alphabet; it does not guarantee that every result contains one character from each category. If a destination requires a particular composition rule, enforce that rule explicitly and test the resulting algorithm.


Keep passwords out of surrounding integrations


Avoid adding password values to event tracking, error reports, chat messages or form submissions. The widget has no network request and no automatic clipboard write.


If Velo is used around the component, keep messages limited to interface needs such as readiness or layout. Do not send the password back to page code just to record that a button was clicked.


Test before offering the tool to visitors


Check minimum and maximum lengths, decimals, empty inputs and values outside the allowed range. Confirm that every generated character belongs to the alphabet.


Inspect the published page’s network activity while generating several samples. Confirm that no surrounding integration captures the generated value. Try keyboard navigation, phone layouts and a browser where the required API is unavailable.


The generation function has been checked locally for length boundaries, allowed characters and the rejection path. That does not constitute an independent security audit of an embedded Wix page or its third-party scripts.


Set up the component in a predictable order


Create a page for the tool, add an HTML embed that accepts code and paste the complete HTML document from the example. The document includes its own labels, length input, button and result field. Keep those elements together inside the component; they are ordinary browser HTML, rather than Wix $w elements.


Give the component sufficient height for its controls, result and status message. Preview the page, then check the published HTTPS version in a browser that supports the required Web Crypto method. Test a phone layout as well as the editor’s desktop view. A functioning generator is not useful if its result is clipped or its controls cannot be reached.


The restored image shows an earlier interface design. Its visual controls do not establish how randomness is generated. Use the updated code as the implementation reference and describe only the options that the current widget actually supports.


Understand the length and character choices


The sample uses a fixed alphabet and allows a bounded password length. Each generated character is selected from that alphabet. Longer output provides more possible combinations under this model, but a destination website may impose its own length or composition rules.


Do not add a checkbox to the interface unless the generation logic implements it. If visitors can exclude a character category, update the alphabet before sampling and reject an empty selection. If a destination requires at least one character from several categories, that is an additional algorithmic requirement; do not claim the existing example guarantees it.


Use unique generated passwords for different accounts and store them appropriately. Avoid presenting the tool as a way to recover a forgotten password or inspect an existing account’s credentials. Those are separate authentication workflows.


Check usability without recording the password


Verify the labels, keyboard focus order and status messages. Generate a value at the minimum and maximum length, then check the visible output. Try an empty input, a decimal and a value beyond the permitted range. Confirm that failures produce an understandable message rather than an apparently successful blank result.


If you later add a copy control, make copying a deliberate user action and explain the result. Do not send generated values to analytics to prove that the tool was used. A count of button activations, if needed at all, should remain separate from the generated value.


Keep the security claim proportional to the work performed. The Web Crypto documentation supports the choice of random source. It does not certify the entire surrounding website, every embedded script or a future modification to the widget.


Check the sampling arithmetic


Quantity

Calculated value

Meaning

Alphabet size

69 characters

26 uppercase + 26 lowercase + 10 digits + 7 symbols

Possible byte values

256

The integers 0 through 255

Accepted byte values

207

Three complete groups of 69; accept values below 207

Rejected byte values

49

Values 207 through 255 are discarded and sampled again

Default / allowed length

20 / 12–128

Configuration of this example, not a universal password policy


These numbers are calculated from the published example, not password-cracking benchmarks. Rejection removes the unequal remainder before the modulo operation. Review the complete algorithm if the alphabet or composition policy changes.


Should I use Math.random() for a password?


No. Use a cryptographically appropriate random source and review the complete implementation.


Does this save generated passwords?


The sample does not save or transmit them. Visitors should store unique passwords in a suitable password manager.


For help integrating a small tool into a Wix website, contact CodeMasters. Our custom contact-form guide covers a different workflow where information is intentionally submitted.

bottom of page