View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.sesn;
17  
18  import java.security.SecureRandom;
19  
20  /**
21   * This class generates a random string for creating Distributed Session Tickets 
22   * 
23   * @author Kuali Rice Team (rice.collab@kuali.org)
24   *
25   */
26  public class SessionIdGenerator {
27  
28      /** The array of printable characters to be used in our random string. */
29      private static final char[] PRINTABLE_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345679"
30          .toCharArray();
31  
32      private static final SecureRandom randomizer = new SecureRandom();
33  
34      public static String getNewString() {
35          final byte[] random = getNewStringAsBytes();
36  
37          return convertBytesToString(random);
38      }
39  
40  
41      private static byte[] getNewStringAsBytes() {
42          final byte[] random = new byte[40];
43  
44          randomizer.nextBytes(random);
45          
46          return random;
47      }
48  
49      private static String convertBytesToString(final byte[] random) {
50          final char[] output = new char[random.length];
51          for (int i = 0; i < random.length; i++) {
52              final int index = Math.abs(random[i] % PRINTABLE_CHARACTERS.length);
53              output[i] = PRINTABLE_CHARACTERS[index];
54          }
55  
56          return new String(output);
57      }
58  }