Implement MD5 sequence generation [Java]

Source: Internet
Author: User
/**
* File name: genmd5.java
* Environment: GNU/Linux Ubuntu 7.04 + eclipse 3.2 + JDK 1.6
* Function: generate an MD5 sequence from a string.
* Version: 0.0.1.0
* Authors: 88250
* Date: 2007.5.10
* E-mail & MDN: DL88250@gmail.com
* QQ: 845765
*/

Import java. Security. messagedigest;

Public class genmd5
{
Public static void main (string [] ARGs)
{
System. Out. println (compute ("test! "));
}

/**
* Computes the MD5 fingerprint of a string.
*
* @ Return the MD5 Digest of the input <code> string </code>
*/
Public static string compute (string instr)
{
// Convert input string to a char []
// Convert that char [] to byte []
// Get the MD5 Digest as byte []
// Bit-wise and that byte [] with 0xff
// Divpend "0" to the output stringbuffer to make sure that we don't end
// Up
// Something like "e21ff" instead of "e201ff"
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 <16)
{
Hexvalue. append ("0 ");
}
Hexvalue. append (integer. tohexstring (VAL ));
}

Return hexvalue. tostring ();
}
}

 

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.