Skip to content

Instantly share code, notes, and snippets.

@HemanthJabalpuri
Last active March 19, 2026 04:12
Show Gist options
  • Select an option

  • Save HemanthJabalpuri/7048ac6ad92e8c33c4306b10d3b14b8b to your computer and use it in GitHub Desktop.

Select an option

Save HemanthJabalpuri/7048ac6ad92e8c33c4306b10d3b14b8b to your computer and use it in GitHub Desktop.
Password generator for use in Browser devtools
/**
* KeePass-style password generator
* Designed for browser usage (DevTools snippets)
*/
const charSets = {
a: "abcdefghijklmnopqrstuvwxyz0123456789",
A: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
U: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
d: "0123456789",
h: "0123456789abcdef",
H: "0123456789ABCDEF",
l: "abcdefghijklmnopqrstuvwxyz",
L: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
u: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
p: ".,:;",
b: "()[]{}<>",
s: "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
};
charSets.S = charSets.A + charSets.s;
const MAX_REPEAT = 1000;
/**
* Secure random character (no modulo bias)
*/
function getRandomChar(set) {
const arr = new Uint32Array(1);
const max = 0xffffffff;
const limit = max - (max % set.length);
do {
crypto.getRandomValues(arr);
} while (arr[0] >= limit);
return set[arr[0] % set.length];
}
/**
* Generate password from pattern
*/
function generatePassword(pattern) {
let result = "";
for (let i = 0; i < pattern.length; i++) {
let token = pattern[i];
let isLiteral = false;
// escape handling
if (token === "\\" && i + 1 < pattern.length) {
token = pattern[++i];
isLiteral = true;
}
// repeat handling
let repeat = 1;
if (i + 1 < pattern.length && pattern[i + 1] === "{") {
const end = pattern.indexOf("}", i + 2);
if (end !== -1) {
const num = parseInt(pattern.slice(i + 2, end), 10);
if (!Number.isNaN(num)) {
repeat = num;
i = end;
}
}
}
if (repeat > MAX_REPEAT) {
throw new Error("Repeat value too large");
}
const set = isLiteral ? null : charSets[token];
if (!isLiteral && !set) {
throw new Error(`Unknown token: ${token}`);
}
for (let r = 0; r < repeat; r++) {
result += set ? getRandomChar(set) : token;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment