Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/PRNG.java
41159 views
1
/*
2
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package sun.security.mscapi;
27
28
import java.lang.ref.Cleaner;
29
import java.security.ProviderException;
30
import java.security.SecureRandomSpi;
31
32
/**
33
* Native PRNG implementation for Windows using the Microsoft Crypto API.
34
*
35
* @since 1.6
36
*/
37
38
public final class PRNG extends SecureRandomSpi
39
implements java.io.Serializable {
40
41
private static final long serialVersionUID = 4129268715132691532L;
42
43
/*
44
* The CryptGenRandom function fills a buffer with cryptographically random
45
* bytes.
46
*/
47
private static native byte[] generateSeed(long ctxt, int length, byte[] seed);
48
private static native long getContext();
49
private static native void releaseContext(long ctxt);
50
private static final Cleaner CLEANER = Cleaner.create();
51
52
private transient long ctxt;
53
54
private static class State implements Runnable {
55
private final long ctxt;
56
57
State(long ctxt) {
58
this.ctxt = ctxt;
59
}
60
61
@Override
62
public void run() {
63
releaseContext(ctxt);
64
}
65
}
66
/**
67
* Creates a random number generator.
68
*/
69
public PRNG() {
70
ctxt = getContext();
71
CLEANER.register(this, new State(ctxt));
72
}
73
74
/**
75
* Reseeds this random object. The given seed supplements, rather than
76
* replaces, the existing seed. Thus, repeated calls are guaranteed
77
* never to reduce randomness.
78
*
79
* @param seed the seed.
80
*/
81
@Override
82
protected synchronized void engineSetSeed(byte[] seed) {
83
if (seed != null) {
84
generateSeed(ctxt, -1, seed);
85
}
86
}
87
88
/**
89
* Generates a user-specified number of random bytes.
90
*
91
* @param bytes the array to be filled in with random bytes.
92
*/
93
@Override
94
protected synchronized void engineNextBytes(byte[] bytes) {
95
if (bytes != null) {
96
if (generateSeed(ctxt, 0, bytes) == null) {
97
throw new ProviderException("Error generating random bytes");
98
}
99
}
100
}
101
102
/**
103
* Returns the given number of seed bytes. This call may be used to
104
* seed other random number generators.
105
*
106
* @param numBytes the number of seed bytes to generate.
107
*
108
* @return the seed bytes.
109
*/
110
@Override
111
protected synchronized byte[] engineGenerateSeed(int numBytes) {
112
byte[] seed = generateSeed(ctxt, numBytes, null);
113
114
if (seed == null) {
115
throw new ProviderException("Error generating seed bytes");
116
}
117
return seed;
118
}
119
120
private void readObject(java.io.ObjectInputStream s)
121
throws java.io.IOException, ClassNotFoundException
122
{
123
s.defaultReadObject();
124
ctxt = getContext();
125
CLEANER.register(this, new State(ctxt));
126
}
127
}
128
129