001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/util/MD5.java,v 1.6 2007/01/15 10:27:31 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.6 $
005: * $Date: 2007/01/15 10:27:31 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author:
039: */
040: package org.mvnforum.util;
041:
042: import java.io.UnsupportedEncodingException;
043: import java.security.MessageDigest;
044: import java.security.NoSuchAlgorithmException;
045:
046: import sun.misc.BASE64Encoder;
047:
048: //==============================================================================
049: // The JavaReference.com Software License, Version 1.0
050: // Copyright (c) 2002-2005 JavaReference.com. All rights reserved.
051: //
052: //
053: // Redistribution and use in source and binary forms, with or without
054: // modification, are permitted provided that the following conditions
055: // are met:
056: //
057: // 1. Redistributions of source code must retain the above copyright notice,
058: // this list of conditions and the following disclaimer.
059: //
060: // 2. Redistributions in binary form must reproduce the above copyright notice,
061: // this list of conditions and the following disclaimer in the documentation
062: // and/or other materials provided with the distribution.
063: //
064: // 3. The end-user documentation included with the redistribution, if any, must
065: // include the following acknowlegement:
066: //
067: // "This product includes software developed by the Javareference.com
068: // (http://www.javareference.com/)."
069: //
070: // Alternately, this acknowlegement may appear in the software itself, if and
071: // wherever such third-party acknowlegements normally appear.
072: //
073: // 4. The names "JavaReference" and "Javareference.com", must not be used to
074: // endorse or promote products derived from this software without prior written
075: // permission. For written permission, please contact webmaster@javareference.com.
076: //
077: // 5. Products derived from this software may not be called "Javareference" nor may
078: // "Javareference" appear in their names without prior written permission of
079: // Javareference.com.
080: //
081: // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
082: // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
083: // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
084: // JAVAREFERENCE.COM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
085: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
086: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
087: // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
088: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
089: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
090: // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
091: //
092: //================================================================================
093: // Software from this site consists of contributions made by various individuals
094: // on behalf of Javareference.com. For more information on Javareference.com,
095: // please see http://www.javareference.com
096: //================================================================================
097:
098: /**
099: * @author anandh
100: */
101: public class MD5 {
102:
103: static char[] carr = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
104: '9', 'a', 'b', 'c', 'd', 'e', 'f' };
105:
106: public static String getBase64FromHEX(String input) {
107:
108: byte barr[] = new byte[16];
109: int bcnt = 0;
110: for (int i = 0; i < 32; i += 2) {
111: char c1 = input.charAt(i);
112: char c2 = input.charAt(i + 1);
113: int i1 = intFromChar(c1);
114: int i2 = intFromChar(c2);
115:
116: barr[bcnt] = 0;
117: barr[bcnt] |= (byte) ((i1 & 0x0F) << 4);
118: barr[bcnt] |= (byte) (i2 & 0x0F);
119: bcnt++;
120: }
121:
122: BASE64Encoder encoder = new BASE64Encoder();
123: return encoder.encode(barr);
124: }
125:
126: public static synchronized String getMD5_Base64(String input) {
127: // please note that we dont use digest, because if we
128: // cannot get digest, then the second time we have to call it
129: // again, which will fail again
130: MessageDigest digest = null;
131:
132: try {
133: digest = MessageDigest.getInstance("MD5");
134: } catch (Exception ex) {
135: ex.printStackTrace();
136: }
137:
138: if (digest == null)
139: return input;
140:
141: // now everything is ok, go ahead
142: try {
143: digest.update(input.getBytes("UTF-8"));
144: } catch (java.io.UnsupportedEncodingException ex) {
145: ex.printStackTrace();
146: }
147: byte[] rawData = digest.digest();
148: BASE64Encoder bencoder = new BASE64Encoder();
149: return bencoder.encode(rawData);
150: }
151:
152: private static int intFromChar(char c) {
153: char clower = Character.toLowerCase(c);
154: for (int i = 0; i < carr.length; i++) {
155: if (clower == carr[i]) {
156: return i;
157: }
158: }
159:
160: return 0;
161: }
162:
163: public static void main(String[] args) {
164:
165: //String password = args[0];
166: String password = "test";
167:
168: MessageDigest digest = null;
169:
170: try {
171: digest = MessageDigest.getInstance("MD5");
172: } catch (NoSuchAlgorithmException e) {
173: e.printStackTrace();
174: }
175:
176: try {
177: digest.update(password.getBytes("UTF-8"));
178: } catch (UnsupportedEncodingException ex) {
179: ex.printStackTrace();
180: }
181:
182: byte[] rawData = digest.digest();
183: StringBuffer printable = new StringBuffer();
184:
185: for (int i = 0; i < rawData.length; i++) {
186: printable.append(carr[((rawData[i] & 0xF0) >> 4)]);
187: printable.append(carr[(rawData[i] & 0x0F)]);
188: }
189: String phpbbPassword = printable.toString();
190:
191: System.out.println("PHPBB : " + phpbbPassword);
192: System.out.println("MVNFORUM : "
193: + getMD5_Base64(password));
194: System.out.println("PHPBB->MVNFORUM : "
195: + getBase64FromHEX(phpbbPassword));
196: }
197:
198: }
|