-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSHA2Test.java
More file actions
182 lines (153 loc) · 7.91 KB
/
Copy pathSHA2Test.java
File metadata and controls
182 lines (153 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright 2016-2022 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.salt;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.core.OS;
import org.junit.Test;
import javax.xml.bind.DatatypeConverter;
import static net.openhft.chronicle.salt.TestUtil.nativeBytesStore;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeFalse;
public class SHA2Test {
private static void doTest256(String inputStr, String expectedHex) {
doTest256(inputStr.getBytes(), expectedHex);
}
private static void doTest256(byte[] inputStr, String expectedHex) {
Bytes<?> input = Bytes.allocateElasticDirect(inputStr.length);
input.write(inputStr);
Bytes<?> hash256 = Bytes.allocateElasticDirect();
SHA2.appendSha256(hash256, input);
Bytes<?> expected = Bytes.allocateElasticDirect();
expected.write(DatatypeConverter.parseHexBinary(expectedHex));
assertEquals(expected.toHexString(), hash256.toHexString());
expected.releaseLast();
input.releaseLast();
hash256.releaseLast();
}
private static void doTest512(String inputStr, String expectedHex) {
Bytes<?> input = Bytes.allocateElasticDirect(inputStr.length());
input.append(inputStr);
Bytes<?> hash512 = Bytes.allocateElasticDirect();
SHA2.appendSha512(hash512, input);
hash512.readPosition(0);
Bytes<?> expected = Bytes.allocateElasticDirect();
expected.write(DatatypeConverter.parseHexBinary(expectedHex));
assertEquals(expected.toHexString(), hash512.toHexString());
expected.releaseLast();
input.releaseLast();
hash512.releaseLast();
}
@Test
public void test256() {
assumeFalse(OS.isWindows());
doTest256(new byte[0], "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
doTest256("abc".getBytes(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
doTest256(DatatypeConverter.parseHexBinary("de188941a3375d3a8a061e67576e926d"),
"067c531269735ca7f541fdaca8f0dc76305d3cada140f89372a410fe5eff6e4d");
doTest256(
DatatypeConverter.parseHexBinary(
"de188941a3375d3a8a061e67576e926dc71a7fa3f0cceb97452b4d3227965f9ea8cc75076d9fb9c5417aa5cb30fc22198b34982dbb629e"),
"038051e9c324393bd1ca1978dd0952c2aa3742ca4f1bd5cd4611cea83892d382");
// see https://en.wikipedia.org/wiki/SHA-2#Test_vectors
// see https://www.di-mgt.com.au/sha_testvectors.html
doTest256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
doTest256("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1");
StringBuilder strOneMillionChara = new StringBuilder();
for (int i = 0; i < 1_000_000; i++) {
strOneMillionChara.append('a');
}
doTest256(strOneMillionChara.toString(), "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
}
@Test
public void test512() {
assumeFalse(OS.isWindows());
// see https://www.di-mgt.com.au/sha_testvectors.html
doTest512("",
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
doTest512("abc",
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
doTest512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445");
doTest512("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909");
StringBuilder strOneMillionChara = new StringBuilder();
for (int i = 0; i < 1_000_000; i++) {
strOneMillionChara.append('a');
}
doTest512(strOneMillionChara.toString(),
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");
}
@Test
public void testMultiPart256() {
assumeFalse(OS.isWindows());
BytesStore message1 = nativeBytesStore("Message part1");
BytesStore message2 = nativeBytesStore("Message part2");
BytesStore message3 = nativeBytesStore("Message part3");
SHA2.MultiPartSHA256 multi = new SHA2.MultiPartSHA256();
multi.add(message1);
multi.add(message2);
multi.add(message3);
BytesStore hash = multi.hash();
assertEquals("34A26FB451F7A08C239F48D8086DCD1628FD8BDE5F54E4600EE91BA5BEBC21AB", DatatypeConverter.printHexBinary(hash.toByteArray()));
}
@Test
public void testMultiPart512() {
assumeFalse(OS.isWindows());
BytesStore message1 = nativeBytesStore("Message part1");
BytesStore message2 = nativeBytesStore("Message part2");
BytesStore message3 = nativeBytesStore("Message part3");
SHA2.MultiPartSHA512 multi = new SHA2.MultiPartSHA512();
multi.add(message1);
multi.add(message2);
multi.add(message3);
BytesStore hash = multi.hash();
assertEquals("D03F370D9C234701370A0323AF9BB5D0E13AEB128C6C14C427DD25B1FFCFA7EB"
+ "B505665AD2C97D989A3F460715D3C688FE04B9FC8AAA3213051486930A3B3876", DatatypeConverter.printHexBinary(hash.toByteArray()));
}
@Test
public void testMultiPartREADME() {
assumeFalse(OS.isWindows());
BytesStore message1 = nativeBytesStore("abcdefgh");
BytesStore message2 = nativeBytesStore("ijklmnop");
BytesStore message3 = nativeBytesStore("qrstuvwxyz");
// Initialise a MultiPartSHA256 wrapper
SHA2.MultiPartSHA256 multi256 = new SHA2.MultiPartSHA256();
multi256.add(message1);
multi256.add(message2);
multi256.add(message3);
// Generate the single SHA-256 hash of the set of messages
BytesStore hash256 = multi256.hash();
// Initialise a MultiPartSHA512 wrapper
SHA2.MultiPartSHA512 multi512 = new SHA2.MultiPartSHA512();
multi512.add(message1);
multi512.add(message2);
multi512.add(message3);
// Generate the single SHA-512 hash of the set of messages
BytesStore hash512 = multi512.hash();
System.out.println("SHA256: " + DatatypeConverter.printHexBinary(hash256.toByteArray()));
System.out.println("SHA512: " + DatatypeConverter.printHexBinary(hash512.toByteArray()));
BytesStore message = nativeBytesStore("abcdefghijklmnopqrstuvwxyz");
BytesStore hash1 = SHA2.sha256(message);
assertArrayEquals(hash1.toByteArray(), hash256.toByteArray());
BytesStore hash2 = SHA2.sha512(message);
assertArrayEquals(hash2.toByteArray(), hash512.toByteArray());
}
}