Skip to content

Instantly share code, notes, and snippets.

@natchiketa
Created June 25, 2020 07:34
Show Gist options
  • Select an option

  • Save natchiketa/efd1569f4e525809c8e20984791b563d to your computer and use it in GitHub Desktop.

Select an option

Save natchiketa/efd1569f4e525809c8e20984791b563d to your computer and use it in GitHub Desktop.

Revisions

  1. natchiketa created this gist Jun 25, 2020.
    42 changes: 42 additions & 0 deletions passgen.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    var specials = '!@#$%^&*()_+{}:"<>?\|[];\',./`~';
    var lowercase = 'abcdefghijklmnopqrstuvwxyz';
    var uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var numbers = '0123456789';

    var all = specials + lowercase + uppercase + numbers;

    String.prototype.pick = function(min, max) {
    var n, chars = '';

    if (typeof max === 'undefined') {
    n = min;
    } else {
    n = min + Math.floor(Math.random() * (max - min));
    }

    for (var i = 0; i < n; i++) {
    chars += this.charAt(Math.floor(Math.random() * this.length));
    }

    return chars;
    };


    // Credit to @Christoph: http://stackoverflow.com/a/962890/464744
    String.prototype.shuffle = function() {
    var array = this.split('');
    var tmp, current, top = array.length;

    if (top) while (--top) {
    current = Math.floor(Math.random() * (top + 1));
    tmp = array[current];
    array[current] = array[top];
    array[top] = tmp;
    }

    return array.join('');
    };

    var password = (specials.pick(1) + lowercase.pick(1) + uppercase.pick(1) + all.pick(3, 10)).shuffle();

    alert(password);