Skip to content

Instantly share code, notes, and snippets.

@Skhmt
Last active December 20, 2024 19:56
Show Gist options
  • Select an option

  • Save Skhmt/5870d0eb3d23d3c7a6ab91e7a2e7442e to your computer and use it in GitHub Desktop.

Select an option

Save Skhmt/5870d0eb3d23d3c7a6ab91e7a2e7442e to your computer and use it in GitHub Desktop.

Revisions

  1. Skhmt revised this gist Dec 20, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pw.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ const decimalNumbers = '0123456789';

    console.log(createRandomString(12, true, 'password', true));

    function createRandomString(length = 8, useSymbols = false, prefix = '', useNumbers = false) {
    function createRandomString(length = 16, useSymbols = true, prefix = '', useNumbers = true) {
    let possibleChars = charactersUpper + charactersLower + (useSymbols ? symbols : '') + (useNumbers ? decimalNumbers : '');

    const requiredChars = [rand(charactersUpper, 1), rand(charactersLower, 1)];
  2. Skhmt revised this gist Dec 6, 2024. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions pw.js
    Original file line number Diff line number Diff line change
    @@ -28,13 +28,8 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb
    }

    function shuffleArray(arr) {
    const randArr = new Uint32Array(arr.length + 1);
    crypto.getRandomValues(randArr);

    const randRangeArr = randArr.map(x => x % arr.length);

    for (let i = arr.length - 1; i >= 0; i--) {
    const j = randRangeArr[i];
    const j = randomInt(i + 1);
    [arr[i], arr[j]] = [arr[j], arr[i]];
    }

    @@ -49,4 +44,8 @@ function rand(str, num) {
    output.push(str[n % str.length]);
    }
    return output;
    }

    function randomInt(max) {
    return crypto.getRandomValues(new Uint32Array(1))[0] % max;
    }
  3. Skhmt revised this gist Dec 6, 2024. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions pw.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ const charactersLower = 'abcdefghijklmnopqrstuvwxyz';
    const symbols = '!@#$%&?^*.:,;_-';
    const decimalNumbers = '0123456789';

    console.log(createRandomString(16, true, 'pw', true));
    console.log(createRandomString(12, true, 'password', true));

    function createRandomString(length = 8, useSymbols = false, prefix = '', useNumbers = false) {
    let possibleChars = charactersUpper + charactersLower + (useSymbols ? symbols : '') + (useNumbers ? decimalNumbers : '');
    @@ -14,9 +14,13 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb
    if (useSymbols) requiredChars.push(rand(symbols, 1));
    if (useNumbers) requiredChars.push(rand(decimalNumbers, 1));

    if (length < requiredChars.length + prefix.length) {
    throw new Error('Length must be at least ' + (requiredChars.length + prefix.length));
    }

    const randLength = length - requiredChars.length - prefix.length;

    const unshuffled = (randLength > 0) ? requiredChars.concat(rand(possibleChars, randLength)) : requiredChars;
    const unshuffled = requiredChars.concat(rand(possibleChars, randLength));

    const shuffled = shuffleArray(unshuffled);

  4. Skhmt revised this gist Dec 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pw.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb

    const randLength = length - requiredChars.length - prefix.length;

    const unshuffled = (randLength > 0) ? requiredChars.concat(rand(possibleChars, randLength) : requiredChars;
    const unshuffled = (randLength > 0) ? requiredChars.concat(rand(possibleChars, randLength)) : requiredChars;

    const shuffled = shuffleArray(unshuffled);

  5. Skhmt revised this gist Dec 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pw.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb

    const randLength = length - requiredChars.length - prefix.length;

    const unshuffled = requiredChars.concat(rand(possibleChars, (randLength > 0) ? randLength : 0));
    const unshuffled = (randLength > 0) ? requiredChars.concat(rand(possibleChars, randLength) : requiredChars;

    const shuffled = shuffleArray(unshuffled);

  6. Skhmt revised this gist Dec 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pw.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import * as crypto from 'node:crypto';

    const charactersUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    const charactersUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const charactersLower = 'abcdefghijklmnopqrstuvwxyz';
    const symbols = '!@#$%&?^*.:,;_-';
    const decimalNumbers = '0123456789';
  7. Skhmt created this gist Dec 6, 2024.
    48 changes: 48 additions & 0 deletions pw.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    import * as crypto from 'node:crypto';

    const charactersUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    const charactersLower = 'abcdefghijklmnopqrstuvwxyz';
    const symbols = '!@#$%&?^*.:,;_-';
    const decimalNumbers = '0123456789';

    console.log(createRandomString(16, true, 'pw', true));

    function createRandomString(length = 8, useSymbols = false, prefix = '', useNumbers = false) {
    let possibleChars = charactersUpper + charactersLower + (useSymbols ? symbols : '') + (useNumbers ? decimalNumbers : '');

    const requiredChars = [rand(charactersUpper, 1), rand(charactersLower, 1)];
    if (useSymbols) requiredChars.push(rand(symbols, 1));
    if (useNumbers) requiredChars.push(rand(decimalNumbers, 1));

    const randLength = length - requiredChars.length - prefix.length;

    const unshuffled = requiredChars.concat(rand(possibleChars, (randLength > 0) ? randLength : 0));

    const shuffled = shuffleArray(unshuffled);

    return prefix + shuffled.join('');
    }

    function shuffleArray(arr) {
    const randArr = new Uint32Array(arr.length + 1);
    crypto.getRandomValues(randArr);

    const randRangeArr = randArr.map(x => x % arr.length);

    for (let i = arr.length - 1; i >= 0; i--) {
    const j = randRangeArr[i];
    [arr[i], arr[j]] = [arr[j], arr[i]];
    }

    return arr;
    }

    function rand(str, num) {
    const randArr = new Uint32Array(num);
    crypto.getRandomValues(randArr);
    let output = [];
    for (let n of randArr) {
    output.push(str[n % str.length]);
    }
    return output;
    }