01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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 edu.yale.its.tp.cas.ticket;
17:
18: import java.security.SecureRandom;
19:
20: /**
21: * Some static utility methods.
22: */
23: public class Util {
24:
25: private static int TRANSACTION_ID_LENGTH = 32;
26:
27: /** Returns a printable String corresponding to a byte array. */
28: public static synchronized String toPrintable(byte[] b) {
29: final char[] alphabet = ("abcdefghijklmnopqrstuvwxyz"
30: + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "1234567890")
31: .toCharArray();
32: char[] out = new char[b.length];
33: for (int i = 0; i < b.length; i++) {
34: int index = b[i] % alphabet.length;
35: if (index < 0)
36: index += alphabet.length;
37: out[i] = alphabet[index];
38: }
39: return new String(out);
40: }
41:
42: public static String getTransactionId() {
43: // produce the random transaction ID
44: byte[] b = new byte[TRANSACTION_ID_LENGTH];
45: SecureRandom sr = new SecureRandom();
46: sr.nextBytes(b);
47: return Util.toPrintable(b);
48: }
49: }
|