01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.digests.RIPEMD128Digest;
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: * RIPEMD128 HMac Test, test vectors from RFC 2286
14: */
15: public class RIPEMD128HMacTest implements Test {
16: final static String[] keys = {
17: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
18: "4a656665",
19: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
20: "0102030405060708090a0b0c0d0e0f10111213141516171819",
21: "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
22: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
23: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
24:
25: final static String[] digests = {
26: "fbf61f9492aa4bbf81c172e84e0734db",
27: "875f828862b6b334b427c55f9f7ff09b",
28: "09f0b2846d2f543da363cbec8d62a38d",
29: "bdbbd7cf03e44b5aa60af815be4d2294",
30: "e79808f24b25fd031c155f0d551d9a3a",
31: "dc732928de98104a1f59d373c150acbb",
32: "5c6bec96793e16d40690c237635f30c5" };
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 "RIPEMD128HMac";
45: }
46:
47: public TestResult perform() {
48: HMac hmac = new HMac(new RIPEMD128Digest());
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: RIPEMD128HMacTest test = new RIPEMD128HMacTest();
71: TestResult result = test.perform();
72:
73: System.out.println(result);
74: }
75: }
|