skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, September 14, 2012

Secure Your Passwords Java MD5 Hashing and Salting

Posted by Admin at 2:08 PM – 0 comments
 

import java.math.BigInteger; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException;   
public class JavaMD5Hash 
{       
 public static void main(String[] args) 
 {               String password = "MyPassword123";               
 System.out.println("MD5 in hex: " + md5(password));                             
 System.out.println("MD5 in hex: " + md5(null));             //= d41d8cd98f00b204e9800998ecf8427e                                         System.out.println("MD5 in hex: " + md5("The quick brown fox jumps over the lazy dog"));
 //= 9e107d9d372bb6826bd81d3542a419d6     
 }
 public static String md5(String input) 
 {                   
  String md5 = null;                  
  if(null == input) return null;                  
  try {
   //Create MessageDigest object for MD5         
   MessageDigest digest = MessageDigest.getInstance("MD5");                   
   //Update input string in message digest         
   digest.update(input.getBytes(), 0, input.length());          
   //Converts message digest value in base 16 (hex)          
   md5 = new BigInteger(1, digest.digest()).toString(16);           
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace(); 
  }         
  return md5;     
 } 
} 

Labels: MD5 Hashing Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger