Last active
          August 29, 2015 14:05 
        
      - 
      
- 
        Save honorlin/575aa565e4a1955bc60d to your computer and use it in GitHub Desktop. 
    java password generator
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | *way 1. | |
| import java.security.SecureRandom; | |
| public final class SessionIdentifierGenerator { | |
| private SecureRandom random = new SecureRandom(); | |
| public String nextSessionId() { | |
| return new BigInteger(130, random).toString(32); | |
| } | |
| } | |
| *way 2. | |
| static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| static Random rnd = new Random(); | |
| String randomString( int len ) | |
| { | |
| StringBuilder sb = new StringBuilder( len ); | |
| for( int i = 0; i < len; i++ ) | |
| sb.append( AB.charAt( rnd.nextInt(AB.length()) ) ); | |
| return sb.toString(); | |
| } | |
| *way 3. | |
| public static String RandomAlphaNumericString(int size){ | |
| String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
| String ret = ""; | |
| int length = chars.length(); | |
| for (int i = 0; i < size; i ++){ | |
| ret += chars.split("")[ (int) (Math.random() * (length - 1)) ]; | |
| } | |
| return ret; | |
| } | |
| *way 4. | |
| // "0123456789" + "ABCDE...Z" | |
| String validCharacters = $('0', '9').join() + $('A', 'Z').join(); | |
| String randomString(int length) { | |
| return $(validCharacters).shuffle().slice(length).toString(); | |
| } | |
| @Test | |
| public void buildFiveRandomStrings() { | |
| for (int i : $(5)) { | |
| System.out.println(randomString(12)); | |
| } | |
| } | |
| *way 5. | |
| public static String getRandomString(int length) | |
| { | |
| String randomStr = UUID.randomUUID().toString(); | |
| while(randomStr.length() < length) { | |
| randomStr += UUID.randomUUID().toString(); | |
| } | |
| return randomStr.substring(0, length); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment