1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.proxy;
21  
22  import static org.apache.mina.proxy.utils.ByteUtilities.asHex;
23  
24  import java.security.MessageDigest;
25  import java.security.NoSuchAlgorithmException;
26  import java.security.NoSuchProviderException;
27  import java.security.Security;
28  
29  import org.apache.mina.proxy.utils.MD4Provider;
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * MD4Test.java - JUnit testcase that tests the rfc 1320 test suite.
35   * @see <a href="http://www.ietf.org/rfc/rfc1320.txt">RFC 1320</a>
36   * 
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev: 751473 $, $Date: 2009-03-08 18:22:14 +0100 (Sun, 08 Mar 2009) $
39   * @since MINA 2.0.0-M3
40   */
41  public class MD4Test extends TestCase {
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      protected void setUp() throws Exception {
48          if (Security.getProvider(MD4Provider.PROVIDER_NAME) == null) {
49              System.out.print("Adding MINA provider...");
50              Security.addProvider(new MD4Provider());
51              //System.out.println(" [Ok]");
52          }
53      }
54  
55      /**
56       * Test suite for the MD4 algorithm. 
57       */
58      public void testRFCVectors() throws NoSuchAlgorithmException,
59              NoSuchProviderException {
60          MessageDigest md4 = MessageDigest.getInstance("MD4",
61                  MD4Provider.PROVIDER_NAME);
62          doTest(md4, "31d6cfe0d16ae931b73c59d7e0c089c0", "");
63          doTest(md4, "bde52cb31de33e46245e05fbdbd6fb24", "a");
64          doTest(md4, "a448017aaf21d8525fc10ae87aa6729d", "abc");
65          doTest(md4, "d9130a8164549fe818874806e1c7014b", "message digest");
66          doTest(md4, "d79e1c308aa5bbcdeea8ed63df412da9",
67                  "abcdefghijklmnopqrstuvwxyz");
68          doTest(md4, "043f8582f241db351ce627e153e7f0e4",
69                  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
70          doTest(
71                  md4,
72                  "e33b4ddc9c38f2199c3e7b164fcc0536",
73                  "12345678901234567890123456789012345678901234567890123456789012345678901234567890");
74      }
75  
76      /**
77       * Original test vector found on <a href="http://en.wikipedia.org/wiki/MD4">wikipedia(en)</a>
78       * and <a href="http://fr.wikipedia.org/wiki/MD4">wikipedia(fr)</a>
79       */
80      public void testWikipediaVectors() throws NoSuchAlgorithmException,
81              NoSuchProviderException {
82          MessageDigest md4 = MessageDigest.getInstance("MD4",
83                  MD4Provider.PROVIDER_NAME);
84          doTest(md4, "b94e66e0817dd34dc7858a0c131d4079",
85                  "Wikipedia, l'encyclopedie libre et gratuite");
86          doTest(md4, "1bee69a46ba811185c194762abaeae90",
87                  "The quick brown fox jumps over the lazy dog");
88          doTest(md4, "b86e130ce7028da59e672d56ad0113df",
89                  "The quick brown fox jumps over the lazy cog");
90      }
91  
92      /**
93       * Performs md4 digesting on the provided test vector and verifies that the
94       * result equals to the expected result.
95       * 
96       * @param md4 the md4 message digester
97       * @param expected the expected hex formatted string
98       * @param testVector the string message
99       */
100     private static void doTest(MessageDigest md4, String expected,
101             String testVector) {
102         System.out.print("Testing vector [" + testVector + "]: ");
103         String result = asHex(md4.digest(testVector.getBytes()));
104         System.out.print(result);
105         assertEquals(expected, result);
106         //System.out.println(" ... Ok");
107     }
108 }