Introduction to the Java Hash hashing algorithm-MD5 & SHA-512
In daily development work, we often encounter a scenario where we need a reliable and proven way to test the integrity of the data in the transmission process. One of the most common situations is when we transfer files, due to network failure or some other factors, we may have downloaded the file is incomplete, which gives us the daily development and maintenance of some problems, and another more common scenario is: Is there an effective way to make it easy to determine whether the files on the server are updated with the latest data, such as our mobile Hybird app development, we often release some of the client pack data updates, So every time we open the app, we have to verify that the server has the latest updates to download, so we can use the latest app features in time.
For these scenarios, the most common approach we use is to use the secure hashing algorithm MD5 root SHA-512, which has the following characteristics:
-Uniqueness: Only the same hash value of the same data
-Reliability: Two different data have different hash values (MD5 have the same value in some special cases)
-Irreversibility: The original value of the input cannot be reversed by a hash value
The two algorithms are highly secure, but compared to the SHA-512 algorithm has a higher level of security, so this algorithm has been adopted by most people.
Here are the specific implementation codes for the two algorithms:
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 case to spy on the output of the MD5 root SHA-512:
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 following is the local output:
Introduction to the Java Hash hashing algorithm-MD5 & SHA-512