-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBatchSha256Rc4Test.java
More file actions
107 lines (95 loc) · 3.89 KB
/
Copy pathBatchSha256Rc4Test.java
File metadata and controls
107 lines (95 loc) · 3.89 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
/*
* 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
*
* http://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.BytesUtil;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.wire.TextWire;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;
public class BatchSha256Rc4Test {
private static final ThreadLocal<Bytes<?>> hash256Bytes = ThreadLocal.withInitial(() -> Bytes.allocateDirect(SHA2.HASH_SHA256_BYTES));
static BytesForTesting bft = new BytesForTesting();
static int testCounter = 0;
static long timePassed = 0;
private static Bytes<?> testDataBytes;
@SuppressWarnings("unchecked")
public static Collection<Object[]> data() throws IOException {
String paramFile = "test-vectors/sha256-shadd256.yaml";
ArrayList<Object[]> params = new ArrayList<>();
int maxTestsToRun = 3500;
TextWire textWire = new TextWire(BytesUtil.readFile(paramFile)).useTextDocuments();
List<Map<String, Object>> testData = (List<Map<String, Object>>) textWire.readMap().get("tests");
long maxSize = 0;
for (Map<String, Object> data : testData) {
Object[] param = new Object[3];
param[0] = data.get("NAME");
long size = Long.parseLong(data.get("SIZE").toString());
param[1] = size;
param[2] = data.get("HASH");
params.add(param);
maxSize = Math.max(maxSize, size);
if (--maxTestsToRun == 0) {
break;
}
}
testDataBytes = generateRc4(maxSize);
return params;
}
@AfterAll
public static void after() {
bft.cleanup();
}
public static Bytes<?> generateRc4(long len) {
int[] key = new int[]{0};
Rc4Cipher cipher = new Rc4Cipher(key);
Bytes<?> bytes = Bytes.allocateDirect(len);
cipher.prga(bytes, len);
return bytes;
}
@ParameterizedTest(name = "{0}")
@MethodSource("data")
public void testHash(String name, long size, String sha256) {
assumeFalse(OS.isWindows());
if ((testCounter % 250) == 0) {
long newTime = System.currentTimeMillis();
System.out.println("Executing test number " + testCounter + " for data size " + size + " time since last log "
+ String.format("%.2f", ((newTime - timePassed) / 1000.0)) + " sec(s)");
timePassed = newTime;
}
testCounter++;
Bytes<?> bytesMessage = testDataBytes;
bytesMessage.readPositionRemaining(0, size);
Bytes<?> sha256Actual = hash256Bytes.get();
sha256Actual.writePosition(0);
SHA2.appendSha256(sha256Actual, bytesMessage);
sha256Actual.readPosition(0);
Bytes<?> sha256Expected = bft.fromHex(sha256);
sha256Expected.readPosition(0);
assertEquals(sha256Expected.toHexString(SHA2.HASH_SHA256_BYTES), sha256Actual.toHexString(SHA2.HASH_SHA256_BYTES));
sha256Expected.releaseLast();
}
}