logo

NJP

Password Generator Script Include

Import · Oct 17, 2018 · article

Name: Password_GeneratorAPI Name: global.Password_GeneratorClient callable: trueApplication: GlobalAccessible from: All application scopesActive: true

Script:

var Password_Generator = Class.create();
Password_Generator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    initialize: function() {
    },
    run: function(givenPasswordLength){
        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('');
        };
        //adjust the pick numbers here to increase or decrease password strength
        var ent = givenPasswordLength - 4;
        if (ent < 0){
            ent = 0;
        }
        var password = (specials.pick(1) + lowercase.pick(1) + uppercase.pick(1) + numbers.pick(1) + all.pick(ent)).shuffle();
        return(password + '');
    },
    type: 'Password_Generator'
});

image

View original source

https://www.servicenow.com/community/now-platform-articles/password-generator-script-include/ta-p/2309946