Implementation of the MD5 Algorithm in Java

Source: Internet
Author: User

</P> <p>/** <br/> * the MD5 algorithm is defined in RFC1321. <br/> * in RFC 1321, test suite is provided to verify whether your implementation is correct: <br/> * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e <br/> * MD5 ("") = digest <br/> * MD5 ("abc") = digest <br/> * MD5 ("message digest") = digest <br/> * MD5 ("abcdefghijklmnopqrstuvwxyz ") = c3fcd3d76192e4007dfb496cca67e13b <br/> * @ author haogj <br/> * input parameter: A byte array <br/> * output parameter: MD5 result String of the byte array <br/> */<br/> public class MD5 {<br/> public static String getMD5 (String pwd) {<br/> byte [] source = pwd. getBytes (); <br/> String s = null; <br/> char hexDigits [] = {// used to convert bytes into hexadecimal characters <br/> '0', '1', '2 ', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C ', 'D', 'E', 'F'}; <br/> try <br/>{< br/> java. security. messageDigest md = java. security. messageDigest. getInstance ("MD5"); <br/> md. update (source); <br/> byte tmp [] = md. digest (); // the MD5 calculation result is a 128-bit long integer, <br/> // 16 bytes in bytes <br/> char str [] = new char [16*2]; // if each byte is represented in hexadecimal notation, two characters are used. <br/> // Therefore, it must be 32 characters in hexadecimal notation. <br/> int k = 0; // indicates the character position in the conversion result. <br/> for (int I = 0; I <16; I ++) {// starts from the first byte, convert each MD5 byte <br/> // to a hexadecimal character <br/> byte byte0 = tmp [I]; // take the I-th byte <br/> str [k ++] = hexDigits [byte0> 4 & 0xf]; // convert the numbers of the 4-digit high in the byte, <br/> //> shifts the right of the logic and shifts the symbol bits right together. <br/> str [k ++] = hexDigits [byte0 & 0xf]; // convert the numbers of the Middle and Lower 4 digits in a byte <br/>}< br/> s = new String (str ); // convert the result to a string </p> <p >}catch (Exception e) {<br/> e. printStackTrace (); <br/>}< br/> return s; <br/>}</p> <p> public static void main (String xu []) <br/> {// calculate the MD5 code of "a", which should be: 0cc175b9c0f1b6a831c399e269772661 <br/> System. out. println (MD5.getMD5 ("a"); <br/>}</p> <p>

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.