"Go" Java compute hash value of the file

Source: Internet
Author: User

Tags: comm Stringbu ref static data convert. NET MD5 value return

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
  1. Package com.test;
  2. Import Java.io.FileInputStream;
  3. Import java.io.FileNotFoundException;
  4. Import Java.io.InputStream;
  5. Import Java.math.BigInteger;
  6. Import Java.security.MessageDigest;
  7. Public class Md5util {
  8. public static void Main (string[] args) {
  9. try {
  10. //Here I'm testing the MD5 value of my native JDK source file
  11. String FilePath = "C:\\Program Files\\java\\jdk1.7.0_45\\src.zip";
  12. String Md5hashcode = Md5hashcode (FilePath);
  13. String md5hashcode32 = md5hashcode32 (FilePath);
  14. System.out.println (Md5hashcode + ": MD5 value of the file");
  15. System.out.println (md5hashcode32+": MD5 value of the file 32 bits");
  16. //system.out.println ( -100 & 0xff);
  17. } catch (FileNotFoundException e) {
  18. E.printstacktrace ();
  19. }
  20. }
  21. /** 
  22. * Get the MD5 value of the file, it may not be 32-bit
  23. * @param filePath file path
  24. * @return
  25. * @throws FileNotFoundException
  26. */
  27. public static string Md5hashcode (String FilePath) throws filenotfoundexception{
  28. FileInputStream FIS = new FileInputStream (FilePath);
  29. return Md5hashcode (FIS);
  30. }
  31. /** 
  32. * Ensure that the MD5 value of the file is 32 bits
  33. * @param filePath file path
  34. * @return
  35. * @throws FileNotFoundException
  36. */
  37. public static string Md5hashcode32 (String FilePath) throws filenotfoundexception{
  38. FileInputStream FIS = new FileInputStream (FilePath);
  39. return md5hashcode32 (FIS);
  40. }
  41. /** 
  42. * Java gets the MD5 value of the file
  43. * @param FIS Input stream
  44. * @return
  45. */
  46. public static String Md5hashcode (InputStream fis) {
  47. try {
  48. //Get a MD5 converter, if you want to use SHA-1 or SHA-256, pass in sha-1,sha-256
  49. MessageDigest MD = messagedigest.getinstance ("MD5");
  50. //several times to read a file in, for large files, it is recommended this way, less memory consumption.
  51. byte[] buffer = new byte[1024];
  52. int length =-1;
  53. While (length = fis.read (buffer, 0, 1024x768))! =-1) {
  54. Md.update (buffer, 0, length);
  55. }
  56. Fis.close ();
  57. //Convert and return a byte array containing 16 elements, returning a numeric range of 128 to 127
  58. byte[] md5bytes = Md.digest ();
  59. BigInteger bigInt = new BigInteger (1, md5bytes); 1 stands for absolute value
  60. return bigint.tostring (+); Convert to 16 binary
  61. } catch (Exception e) {
  62. E.printstacktrace ();
  63. return "";
  64. }
  65. }
  66. /** 
  67. * Java Compute file 32-bit MD5 value
  68. * @param FIS Input stream
  69. * @return
  70. */
  71. public static String md5hashcode32 (InputStream fis) {
  72. try {
  73. //Get a MD5 converter, if you want to use SHA-1 or SHA-256, pass in sha-1,sha-256
  74. MessageDigest MD = messagedigest.getinstance ("MD5");
  75. //several times to read a file in, for large files, it is recommended this way, less memory consumption.
  76. byte[] buffer = new byte[1024];
  77. int length =-1;
  78. While (length = fis.read (buffer, 0, 1024x768))! =-1) {
  79. Md.update (buffer, 0, length);
  80. }
  81. Fis.close ();
  82. //Convert and return a byte array containing 16 elements, returning a numeric range of 128 to 127
  83. byte[] md5bytes = Md.digest ();
  84. StringBuffer hexvalue = new StringBuffer ();
  85. For (int i = 0; i < md5bytes.length; i++) {
  86. int val = ((int) md5bytes[i]) & 0xFF; Explanation See the bottom
  87. if (Val < ) {
  88. /** 
  89. * If less than 16, then the 16 binary form of the Val value must be a bit,
  90. * 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;
  91. * The high point is 0.
  92. */
  93. Hexvalue.append ("0");
  94. }
  95. //Here is the use of the integer class method to achieve the conversion of 16 binary
  96. Hexvalue.append (Integer.tohexstring (Val));
  97. }
  98. return hexvalue.tostring ();
  99. } catch (Exception e) {
  100. E.printstacktrace ();
  101. return "";
  102. }
  103. }
  104. /** 
  105. * Method Md5hashcode32 ((int) md5bytes[i]) & 0xFF operation Explanation:
  106. * 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:
  107. * 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.
  108. * 2, & arithmetic: If 2 bit is 1, then 1, otherwise 0;
  109. * 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.
  110. * 4, the operation of the resulting shaping data is greater than or equal to 0 and less than or equal to 255
  111. */
  112. }

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.

Tags Index: