01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.digests.RIPEMD160Digest;
04: import org.bouncycastle.crypto.macs.HMac;
05: import org.bouncycastle.crypto.params.KeyParameter;
06: import org.bouncycastle.util.Arrays;
07: import org.bouncycastle.util.encoders.Hex;
08: import org.bouncycastle.util.test.SimpleTestResult;
09: import org.bouncycastle.util.test.Test;
10: import org.bouncycastle.util.test.TestResult;
11:
12: /**
13: * RIPEMD160 HMac Test, test vectors from RFC 2286
14: */
15: public class RIPEMD160HMacTest implements Test {
16: final static String[] keys = {
17: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
18: "4a656665",
19: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
20: "0102030405060708090a0b0c0d0e0f10111213141516171819",
21: "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
22: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
23: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
24:
25: final static String[] digests = {
26: "24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668",
27: "dda6c0213a485a9e24f4742064a7f033b43c4069",
28: "b0b105360de759960ab4f35298e116e295d8e7c1",
29: "d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4",
30: "7619693978f91d90539ae786500ff3d8e0518e39",
31: "6466ca07ac5eac29e1bd523e5ada7605b791fd8b",
32: "69ea60798d71616cce5fd0871e23754cd75d5a0a" };
33:
34: final static String[] messages = {
35: "Hi There",
36: "what do ya want for nothing?",
37: "0xdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
38: "0xcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
39: "Test With Truncation",
40: "Test Using Larger Than Block-Size Key - Hash Key First",
41: "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" };
42:
43: public String getName() {
44: return "RIPEMD160HMac";
45: }
46:
47: public TestResult perform() {
48: HMac hmac = new HMac(new RIPEMD160Digest());
49: byte[] resBuf = new byte[hmac.getMacSize()];
50:
51: for (int i = 0; i < messages.length; i++) {
52: byte[] m = messages[i].getBytes();
53: if (messages[i].startsWith("0x")) {
54: m = Hex.decode(messages[i].substring(2));
55: }
56: hmac.init(new KeyParameter(Hex.decode(keys[i])));
57: hmac.update(m, 0, m.length);
58: hmac.doFinal(resBuf, 0);
59:
60: if (!Arrays.areEqual(resBuf, Hex.decode(digests[i]))) {
61: return new SimpleTestResult(false, getName()
62: + ": Vector " + i + " failed");
63: }
64: }
65:
66: return new SimpleTestResult(true, getName() + ": Okay");
67: }
68:
69: public static void main(String[] args) {
70: RIPEMD160HMacTest test = new RIPEMD160HMacTest();
71: TestResult result = test.perform();
72:
73: System.out.println(result);
74: }
75: }
|