author | lana |
Thu, 25 Jan 2018 20:56:49 +0000 | |
changeset 48708 | 46a2e41ebe59 |
parent 47216 | 71c04702a3d5 |
child 52801 | 0baf34792a27 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
10336
diff
changeset
|
2 |
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.mscapi; |
|
27 |
||
28 |
import java.security.ProviderException; |
|
29 |
import java.security.SecureRandomSpi; |
|
30 |
||
31 |
/** |
|
32 |
* Native PRNG implementation for Windows using the Microsoft Crypto API. |
|
33 |
* |
|
34 |
* @since 1.6 |
|
35 |
*/ |
|
36 |
||
37 |
public final class PRNG extends SecureRandomSpi |
|
38 |
implements java.io.Serializable { |
|
39 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
40 |
private static final long serialVersionUID = 4129268715132691532L; |
2 | 41 |
|
42 |
/* |
|
43 |
* The CryptGenRandom function fills a buffer with cryptographically random |
|
44 |
* bytes. |
|
45 |
*/ |
|
46 |
private static native byte[] generateSeed(int length, byte[] seed); |
|
47 |
||
48 |
/** |
|
49 |
* Creates a random number generator. |
|
50 |
*/ |
|
51 |
public PRNG() { |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Reseeds this random object. The given seed supplements, rather than |
|
56 |
* replaces, the existing seed. Thus, repeated calls are guaranteed |
|
57 |
* never to reduce randomness. |
|
58 |
* |
|
59 |
* @param seed the seed. |
|
60 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
10336
diff
changeset
|
61 |
@Override |
2 | 62 |
protected void engineSetSeed(byte[] seed) { |
63 |
if (seed != null) { |
|
64 |
generateSeed(-1, seed); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Generates a user-specified number of random bytes. |
|
70 |
* |
|
71 |
* @param bytes the array to be filled in with random bytes. |
|
72 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
10336
diff
changeset
|
73 |
@Override |
2 | 74 |
protected void engineNextBytes(byte[] bytes) { |
75 |
if (bytes != null) { |
|
76 |
if (generateSeed(0, bytes) == null) { |
|
77 |
throw new ProviderException("Error generating random bytes"); |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Returns the given number of seed bytes. This call may be used to |
|
84 |
* seed other random number generators. |
|
85 |
* |
|
86 |
* @param numBytes the number of seed bytes to generate. |
|
87 |
* |
|
88 |
* @return the seed bytes. |
|
89 |
*/ |
|
16915
675d1569af3e
6425477: Better support for generation of high entropy random numbers
wetmore
parents:
10336
diff
changeset
|
90 |
@Override |
2 | 91 |
protected byte[] engineGenerateSeed(int numBytes) { |
92 |
byte[] seed = generateSeed(numBytes, null); |
|
93 |
||
94 |
if (seed == null) { |
|
95 |
throw new ProviderException("Error generating seed bytes"); |
|
96 |
} |
|
97 |
return seed; |
|
98 |
} |
|
99 |
} |