Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / March 2005

Tip: Looking for answers? Try searching our database.

How to use md5 in login password

Thread view: 
Cathy - 17 Mar 2005 12:25 GMT
I am doing a web application using JSP
I would like to use md5 hashing for the login password.
Can anyone provide me some source code on md5 hashing using Java??

java.security.MessageDigest , is it useful??how to use it?

Thank you very much!
Chris - 19 Mar 2005 00:06 GMT
> I am doing a web application using JSP
> I would like to use md5 hashing for the login password.
[quoted text clipped - 3 lines]
>
> Thank you very much!

Hi,
Here's some sample code which uses MessageDigest to compute the MD5 of
a given string of text. You can work on it from there:

import java.security.*;

public class MD5Test {
       public static void main(final String[] args) throws Exception {
               final MessageDigest md = MessageDigest.getInstance("MD5");
               final String input = "mypassword";
               System.out.println("Digesting the string: '" + input + "'");
               final byte[] inputBytes = input.getBytes();
               final byte[] outputBytes = md.digest(inputBytes);
               System.out.println("Digested to this array of bytes:");
               for (int i = 0; i < outputBytes.length; i++) {
                       System.out.print(toHex(outputBytes[i]));
               }
       }

       private static final String toHex(final byte b) {
               return Integer.toHexString(((int) b) & 0xFF);
       }
}

Note that the digest() method returns an array of bytes, which range
over the full spectrum of binary data. Do NOT simply use new
String(outputBytes), because that will result in character encoding
problems. You need to encode the bytes somehow (Base-64, hex, or
something) if you need to store them in a string.

Chris
Michel Gallant - 19 Mar 2005 00:33 GMT
Here is a message digest calculator, for both strings and arbitrary
binary files, for both SHA1 and MD5 and displays
results of  20 (or 16) bytes as hex or b64:
  http://www.jensign.com/JavaScience/www/messagedigestj2
The code is base on sample code from "Core Java"  3rd Edn.

- Mitch Gallant

> I am doing a web application using JSP
> I would like to use md5 hashing for the login password.
[quoted text clipped - 3 lines]
>
> Thank you very much!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.