]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/RandomToken.java
8e83bb90aab8943b559ef5c450f6cfb762565d47
[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         static SecureRandom sr = new SecureRandom();
7         public static String generateToken(int length) {
8                 StringBuffer token = new StringBuffer();
9                 for (int i = 0; i < length; i++) {
10                         int rand = sr.nextInt(26 * 2 + 10);
11                         if (rand < 10) {
12                                 token.append('0' + rand);
13                         }
14                         rand -= 10;
15                         if (rand < 26) {
16                                 token.append('a' + rand);
17                         }
18                         rand -= 26;
19                         token.append('A' + rand);
20                 }
21                 return token.toString();
22         }
23 }