"Go" Java compute hash value of the file

Source: Internet
Author: User



Original address: http://blog.csdn.net/qq_25646191/article/details/78863110



How do I know if a file has changed? Of course, with the method of comparing file hash value, file hash is called file signature, even if a bit in the file is changed, the file hash will be different.

The more commonly used file hash algorithms are MD5 and SHA-1.
I am using the MD5 algorithm, in Java, the calculation MD5 can be used messagedigest this class.



Here's the code:





[Java]View plain copy



Package com.test;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.InputStream;
Import Java.math.BigInteger;
Import Java.security.MessageDigest;
Public class Md5util {
public static void Main (string[] args) {
try {
//Here I'm testing the MD5 value of my native JDK source file
String FilePath = "C:\\Program Files\\java\\jdk1.7.0_45\\src.zip";
String Md5hashcode = Md5hashcode (FilePath);
String md5hashcode32 = md5hashcode32 (FilePath);
System.out.println (Md5hashcode + ": MD5 value of the file");
System.out.println (md5hashcode32+": MD5 value of the file 32 bits");
//system.out.println ( -100 & 0xff);
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
/** 
* Get the MD5 value of the file, it may not be 32-bit
* @param filePath file path
* @return
* @throws FileNotFoundException
*/
public static string Md5hashcode (String FilePath) throws filenotfoundexception{
FileInputStream FIS = new FileInputStream (FilePath);
return Md5hashcode (FIS);
}
/** 
* Ensure that the MD5 value of the file is 32 bits
* @param filePath file path
* @return
* @throws FileNotFoundException
*/
public static string Md5hashcode32 (String FilePath) throws filenotfoundexception{
FileInputStream FIS = new FileInputStream (FilePath);
return md5hashcode32 (FIS);
}
/** 
* Java gets the MD5 value of the file
* @param FIS Input stream
* @return
*/
public static String Md5hashcode (InputStream fis) {
try {
//Get a MD5 converter, if you want to use SHA-1 or SHA-256, pass in sha-1,sha-256
MessageDigest MD = messagedigest.getinstance ("MD5");
//several times to read a file in, for large files, it is recommended this way, less memory consumption.
byte[] buffer = new byte[1024];
int length =-1;
While (length = fis.read (buffer, 0, 1024x768))! =-1) {
Md.update (buffer, 0, length);
}
Fis.close ();
//Convert and return a byte array containing 16 elements, returning a numeric range of 128 to 127
byte[] md5bytes = Md.digest ();
BigInteger bigInt = new BigInteger (1, md5bytes); 1 stands for absolute value
return bigint.tostring (+); Convert to 16 binary
} catch (Exception e) {
E.printstacktrace ();
return "";
}
}
/** 
* Java Compute file 32-bit MD5 value
* @param FIS Input stream
* @return
*/
public static String md5hashcode32 (InputStream fis) {
try {
//Get a MD5 converter, if you want to use SHA-1 or SHA-256, pass in sha-1,sha-256
MessageDigest MD = messagedigest.getinstance ("MD5");
//several times to read a file in, for large files, it is recommended this way, less memory consumption.
byte[] buffer = new byte[1024];
int length =-1;
While (length = fis.read (buffer, 0, 1024x768))! =-1) {
Md.update (buffer, 0, length);
}
Fis.close ();
//Convert and return a byte array containing 16 elements, returning a numeric range of 128 to 127
byte[] md5bytes = Md.digest ();
StringBuffer hexvalue = new StringBuffer ();
For (int i = 0; i < md5bytes.length; i++) {
int val = ((int) md5bytes[i]) & 0xFF; Explanation See the bottom
if (Val < ) {
/** 
* If less than 16, then the 16 binary form of the Val value must be a bit,
* Because the decimal 0,1...9,10,11,12,13,14,15 corresponds to the 16 binary for the 0,1...9,a,b,c,d,e,f;
* The high point is 0.
*/
Hexvalue.append ("0");
}
//Here is the use of the integer class method to achieve the conversion of 16 binary
Hexvalue.append (Integer.tohexstring (Val));
}
return hexvalue.tostring ();
} catch (Exception e) {
E.printstacktrace ();
return "";
}
}
/** 
* Method Md5hashcode32 ((int) md5bytes[i]) & 0xFF operation Explanation:
* Some operations that involve byte array data in the Java language, often see Byte[i] & 0xFF, here is a summary of what is included here:
* 1, 0xFF is 16 binary (decimal is 255), it is shaped by default, bits is 32 bits, the lowest eight bits is "1111 1111", the remaining 24 bits are 0.
* 2, & arithmetic: If 2 bit is 1, then 1, otherwise 0;
* 3, Byte[i] & 0xFF: First, this operation is generally in the process of transferring the byte data into int or other shaping data, and using this operation, the final shaping data is only 8 bits with data and the other bits are 0.
* 4, the operation of the resulting shaping data is greater than or equal to 0 and less than or equal to 255
*/
}





Running results such as:






PS: In fact, there is one more important point, is how to know that their generated MD5 value is correct?



A lot of methods, in fact, there is a very simple method, do not need to install what software.



Use the command that comes with Windows: Certutil-hashfile [File path] MD5,



Examples are as follows:









"Go" Java compute hash value of the file


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.