how to get 16 digit random password using PwdCryptoSecureAutoGenPassword() ?

saranyavs
Tera Expert

Hi team,

 

I am using below script to generate random password, and its generating 10 digit password by default.

Can we get 16 digit random password instead of 10?

===========================

         var encr= new GlideEncrypter();

                var clearString = new PwdCryptoSecureAutoGenPassword().generatePassword();

                var encrString = encr.encrypt(clearString); 

                workflow.scratchpad.pass = encrString;  

                var decrString = workflow.scratchpad.clearpass = encr.decrypt(encrString);  

=============================

 

Regards,

Saranya

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@saranyavs 

My thoughts

1) OOTB that function will give password between 8 to 12

AnkurBawiskar_0-1756376926385.png

 

2) if you want to increase to 16 then you will have to update the OOTB script include -> Not recommended way

Another way is to use that script and use directly and then enhance to generate between 8 to 16

something like this

function insertCharacterAtIndex(str, c, idx) {
    return (str.slice(0, idx) + c + str.slice(idx));
}
var encr = new GlideEncrypter();
var specialCharacters = " !\"#$%'()*+,-./:;=?@[\\]^_`{|}~";
var secureRandom = GlideSecureRandomUtil;

// Decide number of special characters you want (e.g. 3)
var numSpecialCharacters = secureRandom.getSecureRandomIntBound(3) + 1; // 1-3 special characters
var pwdBaseLength = 16 - numSpecialCharacters; // so final length is 16
var newPwd = secureRandom.getSecureRandomString(pwdBaseLength);

for (var i = 0; i < numSpecialCharacters; i++) {
    var idx = secureRandom.getSecureRandomIntBound(newPwd.length + 1); // insert in any position
    var c = specialCharacters.charAt(secureRandom.getSecureRandomIntBound(specialCharacters.length));
    newPwd = insertCharacterAtIndex(newPwd, c, idx);
}

var encrString = encr.encrypt(newPwd);
workflow.scratchpad.pass = encrString;
var decrString = encr.decrypt(encrString);

Output:

AnkurBawiskar_1-1756377786537.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@saranyavs 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Rafael Batistot
Tera Sage

 Hi @saranyavs 

 

This solution might help you 

 

https://www.servicenow.com/community/developer-articles/oob-passwords-setting-and-generation-passwor...

 

in this example is used with 100 characters but you can configure for 16