Skip to content

Instantly share code, notes, and snippets.

@RHavar
Created December 21, 2016 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RHavar/048b0465ba4b222631bd29512f6dadd7 to your computer and use it in GitHub Desktop.
Save RHavar/048b0465ba4b222631bd29512f6dadd7 to your computer and use it in GitHub Desktop.
function randomPassword(len) { var chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNP123456789"; if (window && window.crypto && window.crypto.getRandomValues && Uint32Array && Uint32Array.prototype.reduce) { return window.crypto.getRandomValues(new Uint32Array(len)) .reduce(function(p, n) { return p + chars[n % chars.length] }, ""); } else { console.warn('Falling back to pseudo-random client seed'); var pass = ""; for (var i = 0; i < len; ++i) pass += chars[Math.floor(Math.random() * chars.length)]; return pass; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment