Skip to content

Instantly share code, notes, and snippets.

@danielfreitasce
Last active November 20, 2022 17:30
Show Gist options
  • Save danielfreitasce/f2bfd42f38a26e6af90c77d21f0ec8f6 to your computer and use it in GitHub Desktop.
Save danielfreitasce/f2bfd42f38a26e6af90c77d21f0ec8f6 to your computer and use it in GitHub Desktop.
Generate Random Strings in Apex
//Option 1
String myRandomString = EncodingUtil.convertToHex(Crypto.generateAesKey(128)).substring(0, 32)
//Option 2
Integer len = 32;
Blob blobKey = crypto.generateAesKey(192);
String key = EncodingUtil.base64encode(blobKey);
String myRandomString = key.substring(0,len);
//Option 2.1
Integer len = 32;
String myRandomString = EncodingUtil.base64encode(Crypto.generateAesKey(192)).substring(0,len);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment