MD5 Algorithm Implementation Functions in Java

Source: Internet
Author: User

The MD5 Algorithm Implementation function in Java. the string (MD5) generated by this function is exactly the same as that generated by the C language in this blog. This can be easily replaced.

 

 

Private Static string MD5 (string input ){
If (input = NULL) | (input. Length () = 0 )){
Return "d41d8cd98f00b204e9800998ecf8427e"; // This value exists to be compatible with the C language's dll MD5 value.
}
Else {
Stringbuffer sb = new stringbuffer ();
Try {
Messagedigest algorithm = messagedigest. getinstance ("MD5 ");
Algorithm. Reset ();
Algorithm. Update (input. getbytes ());
Byte [] MD5 = algorithm. Digest ();
String singlebytehex = "";
For (INT I = 0; I <md5.length; I ++ ){
Singlebytehex = integer. tohexstring (0xff & MD5 [I]);
If (singlebytehex. Length () = 1 ){
SB. append ("0 ");
}
SB. append (singlebytehex. touppercase ());
}
} Catch (nosuchalgorithmexception ex ){
Ex. printstacktrace ();
}
Return sb. tostring ();
}
}

 

 

Related Article

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.