01: /*
02: * JOSSO: Java Open Single Sign-On
03: *
04: * Copyright 2004-2008, Atricore, Inc.
05: *
06: * This is free software; you can redistribute it and/or modify it
07: * under the terms of the GNU Lesser General Public License as
08: * published by the Free Software Foundation; either version 2.1 of
09: * the License, or (at your option) any later version.
10: *
11: * This software is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this software; if not, write to the Free
18: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20: */
21:
22: package org.josso.gateway.assertion.service;
23:
24: import org.apache.commons.logging.Log;
25: import org.apache.commons.logging.LogFactory;
26: import org.josso.util.id.AbstractIdGenerator;
27:
28: /**
29: * Creates unique assertion identifiers based on random and hashing algorithms.
30: *
31: * @author <a href="mailto:gbrigand@josso.org">Gianluca Brigandi</a>
32: * @version $Id$
33: */
34:
35: public class AssertionIdGeneratorImpl extends AbstractIdGenerator
36: implements AssertionIdGenerator {
37:
38: private static final Log logger = LogFactory
39: .getLog(AssertionIdGeneratorImpl.class);
40:
41: private int assertionIdLength = 8;
42:
43: /**
44: * Generate and return a new assertion identifier.
45: */
46: public synchronized String generateId() {
47:
48: byte random[] = new byte[16];
49:
50: // Render the result as a String of hexadecimal digits
51: StringBuffer result = new StringBuffer();
52: int resultLenBytes = 0;
53: while (resultLenBytes < assertionIdLength) {
54: getRandomBytes(random);
55: random = getDigest().digest(random);
56: for (int j = 0; j < random.length
57: && resultLenBytes < assertionIdLength; j++) {
58: byte b1 = (byte) ((random[j] & 0xf0) >> 4);
59: byte b2 = (byte) (random[j] & 0x0f);
60: if (b1 < 10)
61: result.append((char) ('0' + b1));
62: else
63: result.append((char) ('A' + (b1 - 10)));
64: if (b2 < 10)
65: result.append((char) ('0' + b2));
66: else
67: result.append((char) ('A' + (b2 - 10)));
68: resultLenBytes++;
69: }
70: }
71: return (result.toString());
72:
73: }
74:
75: }
|