Skip to content

Instantly share code, notes, and snippets.

@tonyjunkes
Last active April 27, 2022 05:33
Show Gist options
  • Save tonyjunkes/93c4899a8d6552cafae9d8b17c49126c to your computer and use it in GitHub Desktop.
Save tonyjunkes/93c4899a8d6552cafae9d8b17c49126c to your computer and use it in GitHub Desktop.
Generate a random alpha/numeric string based on a given length using Java
<cfscript>
string function randomAlphaNumericString(required numeric length) {
var alphaNum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var SecureRandom = new java("java.security.SecureRandom"); // Note: CF2018 syntax
var rng = SecureRandom.getInstanceStrong();
var result = "";
for (var i = 0; i < arguments.length; i++) {
result &= alphaNum.charAt( rng.nextInt(alphaNum.len()) );
}
return result;
}
writeDump(randomAlphaNumericString(64));
</cfscript>
@Lurg01
Copy link

Lurg01 commented Apr 27, 2022

hope this will work, thank you ❤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment