Java hash algorithm Introduction
Introduction to Java Hash hash algorithm-MD5 & SHA-512
In daily development work, we often encounter such a scenario: We need a reliable and effective method to test and judge the integrity of data in the transmission process. One of the most common cases is that when we transfer files, the downloaded files may be incomplete due to network faults or other factors, this brings us some difficulties in daily development and maintenance. Another common scenario is: is there an effective way for us to easily determine whether the files on the server have the latest data updates, such as our current mobile Hybird App development, we often release some client pack data updates, so each time we open an App, we must first check whether the server has the latest updates to download, so that we can use the latest App features in a timely manner.
For these application scenarios, the most common method is to use the secure hash algorithm MD5 root SHA-512, which has the following characteristics:
-Uniqueness: the same data has only one hash value.
-Reliability: two different data types have different hash values (MD5 has the same value in some special cases)
-Irreversible: the original value cannot be reversed by hash value.
Both algorithms have extremely high security, but in comparison, SHA-512 algorithms have a higher security level, so this algorithm has been adopted by most people.
The specific implementation code of these two algorithms is as follows:
package com.andycbluo.secure.checksum.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class SecureUtil { private final String SHA512 = "SHA-512"; private static SecureUtil _instance = null; private SecureUtil() {} /** * * @return */ public static SecureUtil getInstance() { return _instance; } /** * * @param data * @return */ public String generateChecksum(String data) { return generateChecksum(data, SHA512); } /** * * @param data * @param algorithom * @return */ public String generateChecksum(String data, String algorithom) { String checksum = null; try { MessageDigest instance = MessageDigest.getInstance(algorithom); instance.update(data.getBytes("UTF-8")); checksum = convertBytes2String(instance.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return checksum; } /** * * @param data * @return */ private String convertBytes2String(byte[] data) { StringBuffer sb = new StringBuffer(); for(int i = 0; i < data.length; i++) { sb.append(Integer.toString(data[i] & 0xFF + 0x100, 16).substring(1)); } return sb.toString(); } /** * * @param filePath * @return */ public String generateFileChecksum(String filePath) { return generateFileChecksum(filePath, SHA512); } /** * * @param filePath * @param algorithom * @return */ public String generateFileChecksum(String filePath, String algorithom) { String checksum = null; MessageDigest instance; try { instance = MessageDigest.getInstance(algorithom); InputStream fis = new FileInputStream(new File(filePath)); byte[] blob = new byte[1024]; int numRead; do { numRead = fis.read(blob); if (numRead > 0) { instance.update(blob, 0, numRead); } } while (numRead != -1); checksum = convertBytes2String(instance.digest()); fis.close(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return checksum; } /** * */ static { _instance = new SecureUtil(); }}
We can use a simple use case to snoop the MD5 root SHA-512 output result:
package com.andycbluo.secure.checksum;import com.andycbluo.secure.checksum.util.SecureUtil;public class TestRunner { private static final String TEST_DATA = "This is a testing data : 123456789"; private static final String FILE_PATH = "<Replace Your File Path Name Here>"; public static void main(String[] args) { System.out.println("SHA-512 : " + SecureUtil.getInstance().generateChecksum(TEST_DATA)); System.out.println("MD5 : " + SecureUtil.getInstance().generateChecksum(TEST_DATA, "MD5")); System.out.println("File SHA-512 : " + SecureUtil.getInstance().generateFileChecksum(FILE_PATH)); System.out.println("File MD5 : " + SecureUtil.getInstance().generateFileChecksum(FILE_PATH, "MD5")); }}
The output result is as follows: