bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
RandomString.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.util;
2 
3 import java.security.SecureRandom;
4 
9 public class RandomString
10 {
11  private static final String lettersSpecial = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-$%&/()=?@";
12  private static final String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
13  private static final SecureRandom random = new SecureRandom();
14 
22  public static String generate( int length, boolean specialChars )
23  {
24  String used = ( specialChars ) ? lettersSpecial : letters;
25  String result = "";
26  for ( int i = 0; i < length; i++ ) {
27  int index = (int) ( random.nextDouble() * used.length() );
28  result += used.substring( index, index + 1 );
29  }
30  return result;
31  }
32 
39  public static byte[] generateBinary( int length )
40  {
41  byte[] result = new byte[ length ];
42  random.nextBytes( result );
43  return result;
44  }
45 
46 }
Generate secure random strings.
static String generate(int length, boolean specialChars)
Generate a random string.
static byte[] generateBinary(int length)
Generate random binary data.