Create MD5 hash in JSP
String plainText = "123456"; MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5"); mdAlgorithm.update(plainText.getBytes()); byte[] digest = mdAlgorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < digest.length; i++) { plainText = Integer.toHexString(0xFF & digest[i]); if (plainText.length() < 2) { plainText = "0" + plainText; } hexString.append(plainText); } out.print(hexString.toString());
Conversation
IMHO a very nice solution, all the others I've seen so far involve pages of code or some obscure libraries. Thank You!
I have been looking for this a long time, thanks!
great solution!
import java.security
Works like a charm, the solution I was using,
String md5 = new BigInteger( 1, md.digest() ).toString(16);
wanted to truncated leading 0's, this one doesn't.
I used your code but modified it so I can hash strings and other objects.
Thanks! This solution saves me some time ;)
<script>
var i=0;
while (var<5)
{
documnet.write("Hello World!");
var=var+1;
}
</script>
Excellent example!
I'm just starting out building Java apps and need to add some authentication.
I decided upon storing user details in a plain text file (college project - simplest option - not allowed to use a DB) and wanted to shadow passwords. Now I can!
nice job!!!