001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package com.nabhinc.util;
023:
024: import java.security.MessageDigest;
025:
026: /**
027: * Enrypt a clear text string to hexadecimal format of string using
028: * algorithm supported by java.security.MessageDigest class.
029: *
030: * @author Wirawan Chokry
031: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
032: */
033: public class EncryptionUtil {
034:
035: /**
036: * Encrypts a string (password) using the specified algorithm and
037: * returns the result in a hexadecimal form of string.
038: * The algorithm used has to be one of the digest algorithm supported
039: * by java.security.MessageDigest class (SHA, MD2, or MD5).
040: * If exception occurs, the original string will be returned.
041: * @param password The string that need to be encrypted.
042: * @param algorithm One of these algorithm: SHA, MD2, or MD5
043: * @return Enrypted string in hexadecimal format.
044: */
045: public final static String encrypt(String password, String algorithm) {
046:
047: try {
048: MessageDigest md = (MessageDigest) MessageDigest
049: .getInstance(algorithm).clone();
050: md.update(password.getBytes());
051: // Digest the credentials and return as hexadecimal
052: return convert(md.digest());
053: } catch (Exception ex) {
054: ex.printStackTrace();
055: return password;
056: }
057:
058: }
059:
060: /**
061: * Convert a byte array into a printable format containing a
062: * String of hexadecimal digit characters (two per byte).
063: *
064: * @param bytes Byte array need to be converted.
065: * @return String in hexadecimal format.
066: */
067: private static String convert(byte[] bytes) {
068:
069: StringBuffer sb = new StringBuffer(bytes.length * 2);
070: for (int i = 0; i < bytes.length; i++) {
071: sb.append(convertDigit((int) (bytes[i] >> 4)));
072: sb.append(convertDigit((int) (bytes[i] & 0x0f)));
073: }
074: return (sb.toString());
075:
076: }
077:
078: /**
079: * Convert the value (0 - 15) to the corresponding
080: * hexadecimal digit (i.e. 0 - 9, a - e).
081: *
082: * @param value Value to be converted
083: */
084: private static char convertDigit(int value) {
085:
086: value &= 0x0f;
087: if (value >= 10)
088: return ((char) (value - 10 + 'a'));
089: else
090: return ((char) (value + '0'));
091:
092: }
093:
094: public static void main(String[] args) {
095:
096: System.out.println("admin: "
097: + EncryptionUtil.encrypt("admin", "MD5"));
098: System.out.println("default: "
099: + EncryptionUtil.encrypt("default", "MD5"));
100: System.out.println("login: "
101: + EncryptionUtil.encrypt("login", "MD5"));
102: }
103:
104: }
|