01: /*
02: *
03: *
04: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26:
27: package com.sun.midp.crypto;
28:
29: /**
30: * Implements an abstract class that generalizes random number
31: * generators.
32: */
33: public abstract class SecureRandom {
34: /** Identifies a utility pseudo random number generation algorithm. */
35: public static final byte ALG_PSEUDO_RANDOM = 1;
36:
37: /**
38: * Identifies a cryptographically secure random number generation
39: * algorithm.
40: */
41: public static final byte ALG_SECURE_RANDOM = 2;
42:
43: /**
44: * Protected constructor for subclassing.
45: */
46: protected SecureRandom() {
47: }
48:
49: /**
50: * Creates a RandomData instance of the selected algorithm. <BR />
51: * <P />
52: * <B>WARNING:</B> Requests for a secure random number generator
53: * are currently redirected to a class that implements a weakly
54: * unpredictable source of random data. Licensees of this reference
55: * implementation are strongly urged to link requests for
56: * ALG_SECURE_RANDOM to better generators that may be available
57: * on their specific platforms.
58: *
59: * @param alg the desired random number generation algorithm, e.g.
60: * ALG_PSEUDO_RANDOM
61: *
62: * @return a RandomData instance implementing the selected algorithm.
63: *
64: * @exception NoSuchAlgorithmException if an unsupported algorithm is
65: * requested.
66: */
67: public static SecureRandom getInstance(byte alg)
68: throws NoSuchAlgorithmException {
69: switch (alg) {
70: case ALG_SECURE_RANDOM:
71: // return (new SRand());
72: case ALG_PSEUDO_RANDOM:
73: return (new PRand());
74: default:
75: throw new NoSuchAlgorithmException();
76: }
77: }
78:
79: /**
80: * Generates the next bytes of random data. <BR />
81: * @param buf output buffer in which the random data is to be placed
82: * @param off starting offset within buf for the random data
83: * @param len number of bytes of random data to be placed in buf
84: */
85: public abstract void nextBytes(byte[] buf, int off, int len);
86:
87: /**
88: * Seeds the random number generator. <BR />
89: * @param buf input buffer containing the seed
90: * @param off offset within buf where the seed starts
91: * @param len number of bytes of seed data in buf
92: */
93: public abstract void setSeed(byte[] buf, int off, int len);
94: }
|