Skip to content

Instantly share code, notes, and snippets.

@sXakil
Created December 2, 2018 16:12
Show Gist options
  • Save sXakil/05793eec661b1d2fa83aa7c2d26d79f4 to your computer and use it in GitHub Desktop.
Save sXakil/05793eec661b1d2fa83aa7c2d26d79f4 to your computer and use it in GitHub Desktop.
Generate a random alphaneumeric password
package com.ums.pau.resources;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
public class GenPass {
private String str;
private StringBuilder sb;
private List<Integer> l;
public GenPass() {
this.l = new ArrayList<>();
this.sb = new StringBuilder();
buildPassword();
}
private void buildPassword() {
for (int i = 48; i <= 122; i++) {
if ((i > 57 && i < 65) || (i > 90 && i < 97))
continue;
l.add(i);
}
for (int i = 0; i < 8; i++) {
int randInt = l.get(new SecureRandom().nextInt(62));
sb.append((char) randInt);
}
str = sb.toString();
}
public String getString() {
return str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment