Rich Dennis
Tera Expert

Name: Password_Generator
API Name: global.Password_Generator
Client callable: true
Application: Global
Accessible from: All application scopes
Active: 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'
});
Comments
Inactive_US1603
Kilo Contributor

How to we use this script Include?

I have tried below and doesnt help.

function onLoad() {
        var gen=new GlideAjax(Password_Generator);
        gen.addParam('sysparm_name','run');
        gen.getXML(pass);
        function pass(response) {
        var pass = response.responseXML.documentElement.getAttribute("pass");}
        

        g_form.setValue('password',pass);
        
}

sidkak
Tera Contributor

How to we use this script Include?

I have tried below and doesnt help.

function onLoad() {
        var gen=new GlideAjax('Password_Generator');
        gen.addParam('sysparm_name','initialise');
        gen.getXML(pass1);
        alert(gen);
        function pass1(response) {
        var pass = response.responseXML.documentElement.getAttribute("pass");
        g_form.setValue('password',pass);
        }
        }

Rich Dennis
Tera Expert

I used this in an Orchestration Workflow, so I called it in a server-side script as follows:

// Initial Password
var pwg = new Password_Generator();
var encr = new GlideEncrypter();
var encrString = encr.encrypt(pwg.run(16));
workflow.scratchpad.password = encrString;

For client-side AJAX I think you have to add something like the following to the script include so that you can pass in the desired password length:

var gpl = this.getParameter('sysparm_givenPasswordLength');
Then in the client script you should be able to add:
gen.addParam('sysparm_givenPasswordLength',16);
And edit the line
run: function(givenPasswordLength){
To:
run: function(){

I don't currently have a dev instance ready for testing my theory.
 
ursnani
Giga Guru

A very good Article, though I modified this for creating a random combination of Letters and Numbers for creating a Server Name.

Really this helped me.

Danny Rhoades
Kilo Guru

To get mine to work in a catalog client script I had to make the following modifications:

 

Script Include:

var Password_Generator = Class.create();
Password_Generator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	generatePassword: function() {
		var gpl = this.getParameter('sysparm_passLength');
		var specials = '!@#$%&*()_+<>[].~';
		var lowercase = 'abcdefghijklmnopqrstuvwxyz';
		var uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		var numbers = '0123456789';
		var all = specials + lowercase + uppercase + numbers;

		function pick(type, 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 += type.charAt(Math.floor(Math.random() * type.length));
			}
			return chars;
		}
		
		// Credit to @Christoph: http://stackoverflow.com/a/962890/464744
		function shuffle(picks) {
			var array = picks.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 = gpl - 4;
		if (ent < 0){
			ent = 0;
		}
		var password = (pick(specials,1) + pick(lowercase,1) + pick(uppercase,1) + pick(numbers,1) + shuffle(pick(all,ent)));
		return password.toString();
	},
	
	type: 'Password_Generator'
});

 

Catalog Client Script:

var gen = new GlideAjax('Password_Generator');
gen.addParam('sysparm_name', 'generatePassword');
gen.addParam('sysparm_passLength', '16');
gen.getXMLAnswer(setPass);

function setPass(response) {
	g_form.setValue('temp_pw', response);
}
Version history
Last update:
‎10-17-2018 05:51 AM
Updated by: