Java Build Summary (MD5 SHA-1) __java

Source: Internet
Author: User
Tags md5 md5 digest md5 encryption
Just a little bit of encryption is needed in the recent project, and the Java Security Class Library provides a Java.security.MessageDigest class,This messagedigest class provides an application with the functionality of an information digest algorithm, such as the MD5 or SHA algorithm. The information digest is a secure one-way hash function that receives data of any size and outputs a fixed-length hash value. Have ready-made of course is the best, save effort.
MD5 is very practical in practice. A netizen gives such a description, you can refer to: http://blog.csdn.net/Daping_Zhang/archive/2005/05/28/382688.aspx
of this class getinstance(String algorithm) method returns a MessageDigest entity that encrypts a system of Digest() method and Update(Byte input) method. After encryption to return a byte[],16 bit, we often see a lot of open source Web site download address will have a [MD5] link, open is actually a small piece of text content. For example:
MD5 (commons-logging-1.1.1-bin.zip) = f88520ed791673aed6cc4591bc058b55
This is the MD5 summary information provided by Jakarta's logging component download, which is a summary of the Full-text encryption of this ZIP package, which is the back f88520ed791673aed6cc4591bc058b55, if you download it later, According to MD5 's algorithm to generate their own summary, if the same two summary, it is proved that the file has not been tampered with.
The problem encountered is that the Java MessageDigest class after the execution of the returned byte[16] must be converted into a 16-binary string, if the direct use of the new string (byte[), the result will be incorrect. Algorithm has a lot of netizens to provide, copied. Interestingly, the MD5 that commons-logging provided was not the same as the one I generated (if the file was modified.) ), and later tried the MD5 code provided elsewhere, no problem.
There are a lot of relevant ready-made code, collected a bit to collate as follows (verified): public class Md5builder {

static Logger Logger = Logger.getlogger (Md5builder.class);
Character used to convert bytes into a 16-binary representation
static char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ',
' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};

/**
* The full text of the file generation MD5 summary
* @param file to encrypt
* @return MD5 Digest Code
*/
public static String getMD5 (file file) {
FileInputStream FIS = null;
try {
MessageDigest MD = messagedigest.getinstance ("MD5");

Logger.info ("MD5 Summary length:" + md.getdigestlength ());
FIS = new FileInputStream (file);
byte[] buffer = new byte[2048];
int length =-1;
Logger.info ("Start generating Summary");
Long s = System.currenttimemillis ();
while (length = fis.read (buffer))!=-1) {
Md.update (buffer, 0, length);
}
Logger.info ("Summary build Success, total when:"
+ (System.currenttimemillis ()-s) + "MS");
Byte[] B = md.digest ();
return bytetohexstring (b);
16-bit encryption
Return buf.tostring (). SUBSTRING (8, 24);
catch (Exception ex) {
Logger.error (ex);
Ex.printstacktrace ();
return null;
}finally {
try {
Fis.close ();
catch (IOException ex) {
Ex.printstacktrace ();
}
}
}

/**
* Generate MD5 encryption information for a section of string
* @param message to encrypt a string
* @return generated MD5 information
*/
public static string GetMD5 (String message) {
try {
MessageDigest MD = messagedigest.getinstance ("MD5");
Logger.info ("MD5 Summary length:" + md.getdigestlength ());
Byte[] B = md.digest (Message.getbytes ());
return bytetohexstring (b);
catch (NoSuchAlgorithmException e) {
Logger.error (e);
E.printstacktrace ();
return null;
}
}

/**
* Converts the byte[] array into a hexadecimal string representation
* @param tmp to convert byte[]
* @return Hexadecimal string representation
*/
private static String bytetohexstring (byte[] tmp) {
String s;
The byte representation is 16 bytes.
Char str[] = new char[16 * 2]; Each byte is expressed in 16, using two characters,
So it takes 32 characters to represent a 16 binary.
int k = 0; Represents the corresponding character position in the transformation result
for (int i = 0; i < i++) {//starting from the first byte, for each byte of MD5
Convert to 16-character conversion
byte byte0 = tmp[i]; Take the first byte
str[k++] = hexdigits[byte0 >>> 4 & 0xf]; Takes a 4-bit high number conversion in bytes,
>>> move the symbol to the right, move the sign bit right together
str[k++] = hexdigits[byte0 & 0xf]; Digit conversion with low 4 digits in byte
}
s = new String (str); Convert the result to a string
return s;
}
}
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.