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!