Last active
April 27, 2022 05:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hope this will work, thank you ❤