how to get 16 digit random password using PwdCryptoSecureAutoGenPassword() ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2025 02:57 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2025 03:31 AM - edited 08-28-2025 03:43 AM
My thoughts
1) OOTB that function will give password between 8 to 12
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2025 04:51 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2025 08:55 AM
Hi @saranyavs
This solution might help you
in this example is used with 100 characters but you can configure for 16