KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mvnforum > util > MD5


1 package org.mvnforum.util;
2 import java.security.MessageDigest JavaDoc;
3 import java.security.NoSuchAlgorithmException JavaDoc;
4
5 import sun.misc.BASE64Encoder;
6
7 //==============================================================================
8
// The JavaReference.com Software License, Version 1.0
9
// Copyright (c) 2002-2005 JavaReference.com. All rights reserved.
10
//
11
//
12
// Redistribution and use in source and binary forms, with or without
13
// modification, are permitted provided that the following conditions
14
// are met:
15
//
16
// 1. Redistributions of source code must retain the above copyright notice,
17
// this list of conditions and the following disclaimer.
18
//
19
// 2. Redistributions in binary form must reproduce the above copyright notice,
20
// this list of conditions and the following disclaimer in the documentation
21
// and/or other materials provided with the distribution.
22
//
23
// 3. The end-user documentation included with the redistribution, if any, must
24
// include the following acknowlegement:
25
//
26
// "This product includes software developed by the Javareference.com
27
// (http://www.javareference.com/)."
28
//
29
// Alternately, this acknowlegement may appear in the software itself, if and
30
// wherever such third-party acknowlegements normally appear.
31
//
32
// 4. The names "JavaReference" and "Javareference.com", must not be used to
33
// endorse or promote products derived from this software without prior written
34
// permission. For written permission, please contact webmaster@javareference.com.
35
//
36
// 5. Products derived from this software may not be called "Javareference" nor may
37
// "Javareference" appear in their names without prior written permission of
38
// Javareference.com.
39
//
40
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
41
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
42
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
43
// JAVAREFERENCE.COM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
44
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
46
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
47
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
//
51
//================================================================================
52
// Software from this site consists of contributions made by various individuals
53
// on behalf of Javareference.com. For more information on Javareference.com,
54
// please see http://www.javareference.com
55
//================================================================================
56

57  
58 /**
59  * @author anandh
60  */

61 public class MD5 {
62     static char [] carr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
63
64     public static void main(String JavaDoc[] args) {
65         
66         MessageDigest JavaDoc digest = null;
67             
68         try {
69             digest = MessageDigest.getInstance("MD5");
70         } catch (NoSuchAlgorithmException JavaDoc e) {
71             // TODO Auto-generated catch block
72
e.printStackTrace();
73         }
74         
75         try {
76                 digest.update(args[0].getBytes("UTF-8"));
77              } catch (java.io.UnsupportedEncodingException JavaDoc ex) {
78                    ex.printStackTrace();
79              }
80              
81         byte[] rawData = digest.digest();
82         StringBuffer JavaDoc printable = new StringBuffer JavaDoc();
83         
84         System.out.print("PHPBB :");
85         for(int i = 0; i<rawData.length; i++)
86         {
87             printable.append(carr[((rawData[i] & 0xF0) >> 4)]);
88             printable.append(carr[(rawData[i] & 0x0F)]);
89         }
90         
91         System.out.println(printable);
92         System.out.println("MVNFORUM :" + getMD5_Base64(args[0]));
93         System.out.println("PHPBB->MVNFORUM :" + getBase64FromMD5(printable.toString()));
94     }
95     
96     public static synchronized String JavaDoc getBase64FromMD5(String JavaDoc input) {
97         byte barr[] = new byte[16];
98         int bcnt = 0;
99         for(int i=0; i<32; i+=2)
100         {
101          char c1 = input.charAt(i);
102          char c2 = input.charAt(i+1);
103          int i1 = intfromchar(c1);
104          int i2 = intfromchar(c2);
105          
106           barr[bcnt] = 0;
107           barr[bcnt] |= (byte) ((i1 & 0x0F) << 4);
108           barr[bcnt] |= (byte) (i2 & 0x0F) ;
109           bcnt++;
110         }
111         
112         BASE64Encoder bencoder = new BASE64Encoder();
113         return bencoder.encode(barr);
114     }
115     
116     
117     public static synchronized String JavaDoc getMD5_Base64(String JavaDoc input) {
118         // please note that we dont use digest, because if we
119
// cannot get digest, then the second time we have to call it
120
// again, which will fail again
121
MessageDigest JavaDoc digest = null;
122         
123         try {
124             digest = MessageDigest.getInstance("MD5");
125         } catch (Exception JavaDoc ex) {
126             ex.printStackTrace();
127         }
128         
129         if (digest == null) return input;
130
131         // now everything is ok, go ahead
132
try {
133             digest.update(input.getBytes("UTF-8"));
134         } catch (java.io.UnsupportedEncodingException JavaDoc ex) {
135             ex.printStackTrace();
136         }
137         byte[] rawData = digest.digest();
138         BASE64Encoder bencoder = new BASE64Encoder();
139         return bencoder.encode(rawData);
140     }
141         
142     static int intfromchar(char c)
143     {
144         char clower = Character.toLowerCase(c);
145         for(int i = 0; i<carr.length; i++)
146         {
147             if(clower == carr[i])
148                 return i;
149         }
150         
151         return 0;
152     }
153     
154     
155 }
156
157
Popular Tags