Quick and easy MD5 hash function





6
Date Submitted Thu. Aug. 24th, 2006 1:58 PM
Revision 1 of 1
Beginner jamesstaylor
Tags Java
Comments 1 comments
This is a quick and easy class that will generate MD5 hashes from strings. I originally wrote it to insert into my Oracle database so I could generate MD5 hases for passwords from PL/SQL.

Example

String md5Hash = stringMD5("my password");

import java.io.*;
import java.security.*;

/**
 *  Quick MD5 tool.
 *  The intended use of this file is to be inserted into Oracle and referenced through PL/SQL
 *  but this can be used as a quick and easy way to generate a MD5 hash of a string
 *
 *@author     jtaylor
 *@created    January 10, 2005

 */

public class stringMD5 {
       
        /**
         *  Method used to convert byte array to string.
         *
         *@param  array  Byte Array you would like converted to string format
         *@return        String format of byte array
         */

        private static String hex(byte[] array)
        {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < array.length; ++i)
                {
                        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));
                }
                return sb.toString();
        }


        /**
         *  Static method to generate an MD5 hash from a string
         *
         *@param  message  String or message that you would like an MD5 hash of.
         *@return          MD5 has value of string
         */

        public static String md5(String message)
        {
                try
                {
                        MessageDigest md = MessageDigest.getInstance("MD5");
                        return hex(md.digest(message.getBytes("CP1252")));
                } catch (NoSuchAlgorithmException e)
                {
                } catch (UnsupportedEncodingException e)
                {
                }
                return null;
        }

}
 

James Taylor

Comments

Comments md5 broken
Mon. Sep. 4th, 2006 1:30 PM    Newbie Salindor

Voting