Java MD5 Application

Source: Internet
Author: User

MD5 (Message-Digest algorithm 5 (Information-Digest algorithm 5) is an algorithm that is not encrypted and used to ensure complete and consistent information transmission. It is a one-way hash algorithm and cannot be reversed. The message digest can be seen as the fingerprint of the original data. If the fingerprint is different, the original data is different. MD5 can generate a unique "digital fingerprint" for any file. If anyone makes any changes to the file, the MD5 value, that is, the corresponding "digital fingerprint", will change.

General steps
Call the update and digest methods of the messagedigest object
Special processing of streaming data:
Use digestinputstream or digestoutputstream to wrap the messagedigest object,
Call the read method of digestinputstream or the write method of digestoutputstream to read and write data,
Call the Digest Method of the messagedigest object to process all the data in the stream.

The messagedigest class provides information digest algorithms for applications, such as MD5 or Sha algorithms. Information digest is a secure one-way hash function that receives data of any size and outputs a hash value of a fixed length.

The messagedigest object is initialized. This object uses the update method to process data. You can call the reset method to reset the Summary at any time. Once all the data to be updated has been updated, call one of the digest methods to complete hash calculation.

The Digest method can only be called once for a specified number of updated data. After digest is called, The messagedigest object is reset to its initial state.

 

Package COM. study. messagedigest; import Java. security. messagedigest;/*** digital digest ** @ classname: messagedigesttest * @ description: todo * @ author * @ date 2012-5-17 */public class messagedigesttest {/*** @ Param ARGs * @ throws exception */public static void main (string [] ARGs) throws exception {// todo auto-generated method stubmd5 ();}/***** @ return * @ throws exception */Private Static string MD5 () Throws exception {messagedigest digest = messagedigest. getinstance ("MD5"); Digest. Update ("Hello Java! ". Getbytes ("UTF-8"); byte [] result = digest. digest (); // to facilitate debugging and observation, write a method to convert the byte array into a hexadecimal system. out. println ("message Summary:" + tohex (result); system. out. println ("message digest length:" + result. length); return NULL;} // convert the byte array to a hexadecimal Private Static string tohex (byte [] BUF) {stringbuffer = new stringbuffer (); for (INT I = 0; I <Buf. length; I ++) {int HI = (BUF [I]> 4) & 0x0f); int Lo = Buf [I] & 0x0f; stringbuffer. append (HI> 9? (Char) (hi-10) + 'A'): (char) (hi + '0'); stringbuffer. append (Lo> 9? (Char) (Lo-10) + 'A'): (char) (Lo + '0');} return stringbuffer. tostring ();}}

After testing, we will find that no matter how the content of the message digest to be generated changes its length to 16, it will not change. That is to say, the length of MD5 generated is fixed, as long as the content of the message is changed, the result of the message digest generated by another user changes, which means MD5 can be used to determine whether a file has been tampered with by another user.

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.