String –> MD5 String (in JSP)

I’m not a huge fan of JSP, but it scales better than PHP, so I guess it’s got its uses. Here is a script I wrote in JSP which takes a string and creates a new string which is the MD5 hash of the input string. It is pretty heavily commented, so you should be able to follow it reasonably easily. Don’t miss the import of java.security.MessageDigest which we use. Download md5sum.jsp

This page is OLD and probably contains errors, out of date information, security flaws or other problems. I am keeping it around because it might be helpful to someone.

<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.security.MessageDigest"%>
<html>
<body>
This jsp code shows an example of a functioning md5 hash.<br>
The plaintext password is 'password' and is hard coded.<br>
The MD5 hash version of 'password' is '5f4dcc3b5aa765d61d8327deb882cf99'<br>
and is calculated each time the page is loaded.<br><br>
<%

// Set password string, and print it out
String passwd = "password";
out.println("Password is: " + passwd + ".<br>");

// Create a new instance of MessageDigest, using MD5. SHA and other
// digest algorithms are also available.
MessageDigest alg = MessageDigest.getInstance("MD5");

// Reset the digest, in case it's been used already during this section of code
// This probably isn't needed for pages of 210 simplicity
alg.reset(); 

// Calculate the md5 hash for the password. md5 operates on bytes, so give
// MessageDigest the byte verison of the string
alg.update(passwd.getBytes());

// Create a byte array from the string digest
byte[] digest = alg.digest();

// Convert the hash from whatever format it's in, to hex format
// which is the normal way to display and report md5 sums
// This is done byte by byte, and put into a StringBuffer
StringBuffer hashedpasswd = new StringBuffer();
String hx;
for (int i=0;i<digest.length;i++){
	hx =  Integer.toHexString(0xFF & digest[i]);
	//0x03 is equal to 0x3, but we need 0x03 for our md5sum
	if(hx.length() == 1){hx = "0" + hx;}
	hashedpasswd.append(hx);
}

// Print out the string hex version of the md5 hash
out.println("MD5 version is: " + hashedpasswd.toString() + "<br>");
%>
</body>
</html>

This entry was posted in Programming, Projects and tagged , . Bookmark the permalink.

6 Responses to String –> MD5 String (in JSP)

  1. Maui1983 says:

    Thanks!

  2. MONU says:

    Thanks Sir.

  3. Sakthilakshmi says:

    Good Morning,

    Thank you so much for providing the code with clear information. I got the answer. You are doing a great job for the beginners.

    Thanks for your help !

  4. R says:

    Excelente bro

  5. Suranga upul says:

    Tnx sir.very helpful for me this.thanx again.

Leave a Reply

Your email address will not be published. Required fields are marked *