Comparison of MD5 hash-table encryption in JAVA with MD5 sha encryption in spring __java

Source: Internet
Author: User
Tags md5 hash stringbuffer

L MD5 (Message Digest algorithm 5): a one-way hashing algorithm developed by RSA Data security Company.

L SHA (Secure Hash algorithm): A 160-bit value can be generated for any length of data operation;

SHA-1

In 1993, the Safe hashing algorithm (SHA) was presented by the National Institute of Standards and Technology (NIST) and published as a federal Information Processing standard (FIPS PUB 180), and a revised version of FIPS pub 180-1 was released in 1995, commonly referred to as SHA-1. SHA-1 is based on the MD4 algorithm, and its design is largely modeled on MD4. It is now recognized as one of the safest hashing algorithms and is widely used.

Comparison of MD5 and SHA-1

Because both are derived by MD4, SHA-1 and MD5 are very similar to each other. Correspondingly, their strength and other characteristics are similar, but there are several differences:

• Security for brute force attacks: the most significant and important difference is that the SHA-1 summary is 32 bits longer than the MD5 summary. The use of forced technology to produce any message to make its summary equal to the given report summary of the difficulty of MD5 is a 2^128 order of magnitude of operations, and SHA-1 is 2^160 order of magnitude of operations. In this way, SHA-1 has a greater strength for brute force attacks.

• Security for password analysis: Because of the MD5 design, vulnerable to cryptanalysis, SHA-1 appears vulnerable to such attacks.

L Speed: On the same hardware, SHA-1 runs slower than MD5.


Java Built-in Java.security.MessageDigest implementation:

Package com.redhamr.util;
Import java.io.*;


Import Java.security.MessageDigest;  
        public class Hashcrypt {/*** * MD5 generates 32-bit MD5 code */public static String string2md5 (String inStr) {  
        MessageDigest MD5 = NULL;  
        try{MD5 = messagedigest.getinstance ("MD5");  
            }catch (Exception e) {System.out.println (e.tostring ());  
            E.printstacktrace ();  
        Return "";  
        } char[] Chararray = Instr.tochararray ();  
  
        byte[] ByteArray = new Byte[chararray.length];  
        for (int i = 0; i < chararray.length i++) bytearray[i] = (byte) chararray[i];  
        byte[] md5bytes = Md5.digest (ByteArray);  
        StringBuffer hexvalue = new StringBuffer ();  
            for (int i = 0; i < md5bytes.length i++) {int val = ((int) md5bytes[i]) & 0xFF;  
            if (Val <) hexvalue.append ("0"); Hexvalue.append (Integer.Tohexstring (Val));  
  
    return hexvalue.tostring (); /** * Encryption decryption algorithm performs once encryption, two decryption/public static string ConvertMD5 (String inStr) {Char  
        [] A = Instr.tochararray ();  
        for (int i = 0; i < a.length i++) {A[i] = (char) (a[i] ^ ' t ');  
        string s = new String (a);  
  
    return s;  
        //test main function public static void main (string args[]) {string s = new String ("lcx123");  
        System.out.println ("Original:" + s);  
        System.out.println ("MD5 after:" + STRING2MD5 (s));  
    System.out.println ("decrypted:" + convertMD5 (convertMD5 (s)));
 }  

}

Spring MD5 is made up of Org.springframework.security.authentication.encoding.Md5PasswordEncoder

And the SHA is made by Org.springframework.security.authentication.encoding.ShaPasswordEncoder:

Package com.esoft.core.util;
Import Org.springframework.security.authentication.encoding.Md5PasswordEncoder;
Import Org.springframework.security.authentication.encoding.PasswordEncoder;

Import Org.springframework.security.authentication.encoding.ShaPasswordEncoder;

	public class Hashcrypt {public static String sha = ' Sha ';
	
	public static String MD5 = "MD5";
	
	Private final static Passwordencoder Shapasswordencoder = new Shapasswordencoder ();

	Private final static Passwordencoder Md5passwordencoder = new Md5passwordencoder ();
	public static string Getdigesthash (String str) {return Getdigesthash (str, "SHA");
		public static string Getdigesthash (String str, string hashtype) {if (str = null) return null;
	return Getdigesthash (str, hashtype,null); public static string Getdigesthash (String str, string Hashtype,object salt) {if (Hashtype = null) {Hashtype =
		SHA;
		} if (Hashtype.equals (MD5)) {return Md5passwordencoder.encodepassword (str, salt);
	}	
		return Shapasswordencoder.encodepassword (str, salt);
	public static void Main (string[] args) {System.out.println (Getdigesthash ("jhsx128710104"));
 }
}

empty string:


Sha ("") = da39a3ee5e6b4b0d3255bfef95601890afd80709

MD5 ("") =d41d8cd98f00b204e9800998ecf8427e



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.