]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/RandomToken.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / org / cacert / gigi / util / RandomToken.java
1 package org.cacert.gigi.util;
2
3 import java.security.SecureRandom;
4
5 public class RandomToken {
6
7     private static SecureRandom sr = new SecureRandom();
8
9     public static String generateToken(int length) {
10         StringBuffer token = new StringBuffer();
11         for (int i = 0; i < length; i++) {
12             int rand = sr.nextInt(26 * 2 + 10);
13             if (rand < 10) {
14                 token.append((char) ('0' + rand));
15                 continue;
16             }
17             rand -= 10;
18             if (rand < 26) {
19                 token.append((char) ('a' + rand));
20                 continue;
21             }
22             rand -= 26;
23             token.append((char) ('A' + rand));
24         }
25         return token.toString();
26     }
27 }