01: /*
02: * @(#)RandomData.java 1.5 02/07/24 @(#)
03: *
04: * Copyright (c) 2000-2001 Sun Microsystems, Inc. All rights reserved.
05: * PROPRIETARY/CONFIDENTIAL
06: * Use is subject to license terms.
07: */
08:
09: package com.sun.portal.ksecurity;
10:
11: /**
12: * Implements an abstract class that generalizes random number
13: * generators.
14: */
15: public abstract class RandomData {
16: /** Identifies a utility pseudo random number generation algorithm. */
17: public static final byte ALG_PSEUDO_RANDOM = 1;
18:
19: /**
20: * Identifies a cryptographically secure random number generation
21: * algorithm.
22: */
23: public static final byte ALG_SECURE_RANDOM = 2;
24:
25: /**
26: * Protected constructor for subclassing.
27: */
28: protected RandomData() {
29: }
30:
31: /**
32: * Creates a RandomData instance of the selected algorithm. <BR />
33: * @param alg the desired random number generation algorithm, e.g.
34: * ALG_PSEUDO_RANDOM
35: * @return a RandomData instance implementing the selected algorithm.
36: * @exception CryptoException with reason code set to NO_SUCH_ALGORITHM
37: * if an unsupported algorithm is requested.
38: * <P />
39: * <B>WARNING:</B> Requests for a secure random number generator
40: * are currently redirected to a class that implements a weakly
41: * unpredictable source of random data. Licensees of this reference
42: * implementation are strongly urged to link requests for
43: * ALG_SECURE_RANDOM to better generators that may be available
44: * on their specific platforms.
45: */
46: public static RandomData getInstance(byte alg)
47: throws CryptoException {
48: switch (alg) {
49: case ALG_SECURE_RANDOM:
50: // return (new SRand());
51: case ALG_PSEUDO_RANDOM:
52: return (new PRand());
53: default:
54: throw new CryptoException(CryptoException.NO_SUCH_ALGORITHM);
55: }
56: }
57:
58: /**
59: * Generates random data. <BR />
60: * @param buf output buffer in which the random data is to be placed
61: * @param off starting offset within buf for the random data
62: * @param len number of bytes of random data to be placed in buf
63: */
64: public abstract void generateData(byte[] buf, short off, short len);
65:
66: /**
67: * Seeds the random number generator. <BR />
68: * @param buf input buffer containing the seed
69: * @param off offset within buf where the seed starts
70: * @param len number of bytes of seed data in buf
71: */
72: public abstract void setSeed(byte[] buf, short off, short len);
73: }
|