KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > MD5Tests


1 /*
2  * MD5 regression test.
3  * Copyright (C) 2004 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.util.*;
21 import java.io.*;
22
23 class MD5Tests {
24
25     private static class TestCase {
26         private String JavaDoc md5;
27         private byte[] bytes;
28         public TestCase(String JavaDoc md5, byte[] bytes){
29             this.md5 = md5;
30             this.bytes = bytes;
31         }
32         private void test() throws Exception JavaDoc {
33             String JavaDoc hashString = MD5.getHashString(bytes);
34             if (!md5.equals(hashString)){
35                 throw new Exception JavaDoc("Failed test. Should be " + md5 + " was " + hashString + ".");
36             }
37         }
38     }
39
40     private static final TestCase[] testCases = new TestCase[]{
41         new TestCase("d41d8cd98f00b204e9800998ecf8427e", new byte[]{}),
42         new TestCase("0cc175b9c0f1b6a831c399e269772661", new byte[]{'a'}),
43         new TestCase("900150983cd24fb0d6963f7d28e17f72", new byte[]{'a','b','c'}),
44         new TestCase("f96b697d7cb7938d525a2f31aaf161d0", new byte[]{'m','e','s','s','a','g','e',' ','d','i','g','e','s','t'}),
45         new TestCase("c3fcd3d76192e4007dfb496cca67e13b", new byte[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}),
46         new TestCase("d174ab98d277d9f5a5611c2c9f419d9f", new byte[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'}),
47         new TestCase("57edf4a22be3c955ac49da2e2107b67a", new byte[]{'1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0'}),
48     };
49
50     public static void main(String JavaDoc[] args){
51         try {
52             for (int i=0; i<testCases.length; i++){
53                 testCases[i].test();
54             }
55         } catch (Exception JavaDoc x){
56             x.printStackTrace(System.err);
57             System.exit(1);
58         }
59         System.exit(0);
60     }
61 }
62
Popular Tags