public static String md5 ( String text ) { MessageDigest md ; try { md = MessageDigest . getInstance ( "MD5" ) ; } catch ( NoSuchAlgorithmException e ) { throw new BugError ( "Java runtime without MD5 support." ) ; } md . update ( text . getBytes ( ) ) ; byte [ ] md5 = md . digest ( ) ; char [ ] buffer = new char [ 32 ] ; for ( int i = 0 ; i < 16 ; i ++ ) { buffer [ i * 2 ] = HEXA [ ( md5 [ i ] & 0xf0 ) >> 4 ] ; buffer [ i * 2 + 1 ] = HEXA [ md5 [ i ] & 0x0f ] ; } return new String ( buffer ) ; }
Generate text MD5 hash into hexadecimal format . Returned string will be exactly 32 characters long . This function is designed but not limited to store password into databases . Anyway be aware that MD5 is not a strong enough hash function to be used on public transfers .