KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > common > crypto > MD5


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.common.crypto;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.security.MessageDigest JavaDoc;
23 import java.security.NoSuchAlgorithmException JavaDoc;
24
25
26 public class MD5
27 {
28     private static final char[] hex = {
29         '0', '1', '2', '3', '4', '5', '6', '7',
30         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
31     };
32
33     /**
34      * Turns array of bytes into string representing each byte as
35      * unsigned hex number.
36      *
37      * @param hash array of bytes to convert to hex-string
38      * @return generated hex string
39      */

40     private static final String JavaDoc toHex(byte hash[])
41     {
42         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(hash.length * 2);
43
44         for (int idx=0; idx<hash.length; idx++)
45             buf.append(hex[(hash[idx] >> 4) & 0x0f]).append(hex[hash[idx] & 0x0f]);
46
47         return buf.toString();
48     }
49
50     /**
51      * Digest the input.
52      *
53      * @param input the data to be digested.
54      * @return the md5-digested input
55      */

56     private static final byte[] digest(byte[] input)
57     {
58         try
59         {
60             MessageDigest JavaDoc md5 = MessageDigest.getInstance("MD5");
61             return md5.digest(input);
62         }
63         catch (NoSuchAlgorithmException JavaDoc nsae)
64         {
65             throw new Error JavaDoc(nsae.toString());
66         }
67     }
68
69     /**
70      * Digest the input.
71      *
72      * @param input1 the first part of the data to be digested.
73      * @param input2 the second part of the data to be digested.
74      * @return the md5-digested input
75      */

76     private static final byte[] digest(byte[] input1, byte[] input2)
77     {
78         try
79         {
80             MessageDigest JavaDoc md5 = MessageDigest.getInstance("MD5");
81             md5.update(input1);
82             return md5.digest(input2);
83         }
84         catch (NoSuchAlgorithmException JavaDoc nsae)
85         {
86             throw new Error JavaDoc(nsae.toString());
87         }
88     }
89
90     /**
91      * Digest the input.
92      *
93      * @param input the data to be digested.
94      * @return the md5-digested input as a hex string
95      */

96     public static final String JavaDoc encode(String JavaDoc input)
97     {
98         try {
99             return toHex(digest(input.getBytes("8859_1")));
100         } catch(UnsupportedEncodingException JavaDoc uee) {
101             throw new Error JavaDoc(uee.toString());
102         }
103     }
104 }
105
Popular Tags