Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/SecureRandom/TestDeserialization.java
38855 views
1
/*
2
* Copyright (c) 2010, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 6837847
27
* @summary Ensure a deserialized PKCS#11 SecureRandom is functional.
28
* @library ..
29
*/
30
31
import java.security.*;
32
import java.io.*;
33
34
public class TestDeserialization extends PKCS11Test {
35
36
public void main(Provider p) throws Exception {
37
// Skip this test for providers not found by java.security.Security
38
if (Security.getProvider(p.getName()) != p) {
39
System.out.println("Skip test for provider " + p.getName());
40
return;
41
}
42
SecureRandom r;
43
try {
44
r = SecureRandom.getInstance("PKCS11", p);
45
System.out.println("SecureRandom instance " + r);
46
} catch (NoSuchAlgorithmException e) {
47
System.out.println("Provider " + p +
48
" does not support SecureRandom, skipping");
49
e.printStackTrace();
50
return;
51
}
52
r.setSeed(System.currentTimeMillis());
53
byte[] buf = new byte[16];
54
byte[] ser = toByteArray(r);
55
System.out.println("Serialized Len = " + ser.length);
56
SecureRandom r2 = fromByteArray(ser);
57
System.out.println("Deserialized into " + r2);
58
r2.nextBytes(buf);
59
System.out.println("Done");
60
}
61
62
public static void main(String[] args) throws Exception {
63
main(new TestDeserialization());
64
}
65
66
private byte[] toByteArray(SecureRandom r) throws Exception {
67
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
68
ObjectOutputStream outStream = null;
69
try {
70
outStream = new ObjectOutputStream(out);
71
outStream.writeObject(r);
72
return out.toByteArray();
73
} finally {
74
if (outStream != null) {
75
outStream.close();
76
}
77
}
78
}
79
80
private SecureRandom fromByteArray(byte[] buf) throws Exception {
81
SecureRandom r = null;
82
ByteArrayInputStream is = new ByteArrayInputStream(buf);
83
ObjectInputStream ois = null;
84
try {
85
ois = new ObjectInputStream(is);
86
r = (SecureRandom) ois.readObject();
87
} finally {
88
if (ois != null) {
89
ois.close();
90
}
91
}
92
return r;
93
}
94
}
95
96