01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.Digest;
04: import org.bouncycastle.crypto.digests.WhirlpoolDigest;
05: import org.bouncycastle.util.Arrays;
06: import org.bouncycastle.util.encoders.Hex;
07: import org.bouncycastle.util.test.SimpleTest;
08:
09: /**
10: * ISO vector test for Whirlpool
11: *
12: */
13: public class WhirlpoolDigestTest extends SimpleTest {
14: private static String[][] _isoVectors = {
15: {
16: "",
17: "19FA61D75522A4669B44E39C1D2E1726C530232130D407F89AFEE0964997F7A73E83BE698B288FEBCF88E3E03C4F0757EA8964E59B63D93708B138CC42A66EB3" },
18: {
19: "a",
20: "8ACA2602792AEC6F11A67206531FB7D7F0DFF59413145E6973C45001D0087B42D11BC645413AEFF63A42391A39145A591A92200D560195E53B478584FDAE231A" },
21: {
22: "abc",
23: "4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5" },
24: {
25: "message digest",
26: "378C84A4126E2DC6E56DCC7458377AAC838D00032230F53CE1F5700C0FFB4D3B8421557659EF55C106B4B52AC5A4AAA692ED920052838F3362E86DBD37A8903E" },
27: {
28: "abcdefghijklmnopqrstuvwxyz",
29: "F1D754662636FFE92C82EBB9212A484A8D38631EAD4238F5442EE13B8054E41B08BF2A9251C30B6A0B8AAE86177AB4A6F68F673E7207865D5D9819A3DBA4EB3B" },
30: {
31: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
32: "DC37E008CF9EE69BF11F00ED9ABA26901DD7C28CDEC066CC6AF42E40F82F3A1E08EBA26629129D8FB7CB57211B9281A65517CC879D7B962142C65F5A7AF01467" },
33: {
34: "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
35: "466EF18BABB0154D25B9D38A6414F5C08784372BCCB204D6549C4AFADB6014294D5BD8DF2A6C44E538CD047B2681A51A2C60481E88C5A20B2C2A80CF3A9A083B" },
36: {
37: "abcdbcdecdefdefgefghfghighijhijk",
38: "2A987EA40F917061F5D6F0A0E4644F488A7A5A52DEEE656207C562F988E95C6916BDC8031BC5BE1B7B947639FE050B56939BAAA0ADFF9AE6745B7B181C3BE3FD" } };
39:
40: private static String _millionAResultVector = "0C99005BEB57EFF50A7CF005560DDF5D29057FD86B20BFD62DECA0F1CCEA4AF51FC15490EDDC47AF32BB2B66C34FF9AD8C6008AD677F77126953B226E4ED8B01";
41:
42: private static String _thirtyOneZeros = "3E3F188F8FEBBEB17A933FEAF7FE53A4858D80C915AD6A1418F0318E68D49B4E459223CD414E0FBC8A57578FD755D86E827ABEF4070FC1503E25D99E382F72BA";
43:
44: public String getName() {
45: return "Whirlpool";
46: }
47:
48: public void performTest() {
49: for (int i = 0; i < _isoVectors.length; i++) {
50: performStandardVectorTest("ISO vector test [" + i + "]",
51: _isoVectors[i][0], _isoVectors[i][1]);
52: }
53:
54: byte[] thirtyOneZeros = new byte[31];
55: performStandardVectorTest("31 zeroes test", thirtyOneZeros,
56: _thirtyOneZeros);
57:
58: byte[] millionAInByteArray = new byte[1000000];
59: Arrays.fill(millionAInByteArray, (byte) 'a');
60:
61: performStandardVectorTest("Million 'a' test",
62: millionAInByteArray, _millionAResultVector);
63: }
64:
65: private void performStandardVectorTest(String testTitle,
66: byte[] inputBytes, String resultsAsHex) {
67: doPerformTest(testTitle, inputBytes, resultsAsHex);
68: }
69:
70: private void doPerformTest(String testTitle, byte[] inputBytes,
71: String resultsAsHex) {
72: String resStr = createHexOutputFromDigest(inputBytes);
73: if (!resultsAsHex.equals(resStr.toUpperCase())) {
74: fail(testTitle, resultsAsHex, resStr);
75: }
76: }
77:
78: private void performStandardVectorTest(String testTitle,
79: String inputBytesAsString, String resultsAsHex) {
80: doPerformTest(testTitle, inputBytesAsString.getBytes(),
81: resultsAsHex);
82: }
83:
84: private String createHexOutputFromDigest(byte[] digestBytes) {
85: String resStr;
86: Digest digest = new WhirlpoolDigest();
87: byte[] resBuf = new byte[digest.getDigestSize()];
88: digest.update(digestBytes, 0, digestBytes.length);
89: digest.doFinal(resBuf, 0);
90: resStr = new String(Hex.encode(resBuf));
91: return resStr;
92: }
93:
94: public static void main(String[] args) {
95: runTest(new WhirlpoolDigestTest());
96: }
97: }
|