Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/MessageDigest/TestSameLength.java
41149 views
1
/*
2
* Copyright (c) 2015, 2020, 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
import static java.lang.System.out;
25
import java.nio.ByteBuffer;
26
import java.security.MessageDigest;
27
import java.security.NoSuchAlgorithmException;
28
import java.security.Security;
29
import jdk.test.lib.RandomFactory;
30
31
/**
32
* @test
33
* @bug 8050371 8156059
34
* @summary Check md.getDigestLength() equal digest output length with various
35
* algorithm/dataLen/(update,digest methods).
36
* @author Kevin Liu
37
* @key randomness
38
* @library /test/lib
39
* @build jdk.test.lib.RandomFactory
40
* @run main TestSameLength
41
*/
42
43
public class TestSameLength {
44
45
public static void main(String[] args) throws Exception {
46
TestSameLength test = new TestSameLength();
47
test.run();
48
}
49
50
private void run() throws Exception {
51
String[] algorithmArr = { "SHA", "Sha", "SHA-1", "sha-1", "SHA1",
52
"sha1", "MD5", "md5", "SHA-224", "SHA-256", "SHA-384",
53
"SHA-512", "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512" };
54
int[] nUpdatesArr = { 0, 1, 2, 3 };
55
int[] dataLenArr = { 1, 50, 2500, 125000, 6250000 };
56
57
for (String algorithm : algorithmArr) {
58
for (UpdateMethod update : UpdateMethod.values()) {
59
for (int dataLen : dataLenArr) {
60
if (!runTest(algorithm, dataLen, update)) {
61
throw new RuntimeException(
62
"Test failed at algorithm/dataLen/numUpdate:"
63
+ algorithm + "/" + dataLen + "/"
64
+ update.toString());
65
}
66
}
67
}
68
}
69
70
out.println("All "
71
+ algorithmArr.length * nUpdatesArr.length * dataLenArr.length
72
+ " tests Passed");
73
}
74
75
private boolean runTest(String algo, long dataLen, UpdateMethod whichUpdate)
76
throws Exception {
77
try {
78
// Do initialization
79
byte[] data = new byte[(int) dataLen];
80
RandomFactory.getRandom().nextBytes(data);
81
MessageDigest md = MessageDigest.getInstance(algo);
82
int outputLen = md.getDigestLength();
83
84
// Perform the update using all available/possible update methods
85
whichUpdate.updateDigest(data, md, dataLen);
86
// Get the output
87
byte[] output = md.digest();
88
89
// Compare input and output
90
return outputLen == output.length;
91
} catch (NoSuchAlgorithmException nae) {
92
throw nae;
93
} catch (Exception ex) {
94
System.err.println("Testing: " + algo + "/" + dataLen + "/"
95
+ whichUpdate.toString()
96
+ " failed with unexpected exception");
97
ex.printStackTrace();
98
throw ex;
99
}
100
}
101
102
private static enum UpdateMethod {
103
UPDATE_BYTE {
104
@Override
105
public void updateDigest(byte[] data, MessageDigest md,
106
long dataLen) {
107
108
for (int i = 0; i < dataLen; i++) {
109
md.update(data[i]);
110
}
111
}
112
},
113
114
UPDATE_BUFFER {
115
@Override
116
public void updateDigest(byte[] data, MessageDigest md,
117
long dataLen) {
118
119
md.update(data);
120
}
121
},
122
123
UPDATE_BUFFER_LEN {
124
@Override
125
public void updateDigest(byte[] data, MessageDigest md,
126
long dataLen) {
127
128
for (int i = 0; i < dataLen; i++) {
129
md.update(data, i, 1);
130
}
131
}
132
},
133
134
UPDATE_BYTE_BUFFER {
135
@Override
136
public void updateDigest(byte[] data, MessageDigest md,
137
long dataLen) {
138
139
md.update(ByteBuffer.wrap(data));
140
}
141
};
142
143
public abstract void updateDigest(byte[] data, MessageDigest md,
144
long dataLen);
145
}
146
}
147
148