Last Updated on 02 July 2019   |   Print Email
In cryptography, MD5 (Message Digest version 5) and SHA (Secure Hash Algorithm) are two well-known message digest algorithms. They are also referred as cryptographic hash functions, which take arbitrary-sized data as input (message) and produce a fixed-length hash value. One of the most important properties of hash functions is, it’s infeasible to generate a message that has a given hash (secure one-way). Hash functions are frequently used to check data integrity such as checking integrity of a downloaded file against its publicly-known hash value. Another common usage is to encrypt user’s password in database.The Java platform provides two implementation of hashing functions: MD5 (produces 128-bit hash value), SHA-1 (160-bit) and SHA-2 (256-bit). This tutorial demonstrates how to generate MD5 and SHA hash values from String or file using Java.Here are general steps to generate a hash value from an input (message):
First approach (suitable for small-sized message):
// algorithm can be "MD5", "SHA-1", "SHA-256"
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] inputBytes = // get bytes array from message
byte[] hashBytes = digest.digest(inputBytes);
// convert hash bytes to string (usually in hexadecimal form)
Second approach (suitable for large-size message, i.e. large file):
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] inputBytes = // get bytes array from message
digest.update(inputBytes);
byte[] hashedBytes = digest.digest();
// convert hash bytes to string (usually in hexadecimal form)
Now, let’s see some examples in details.
1. Generating Hash from String
The following method takes a message and algorithm name as inputs and returns hexadecimal form of the calculated hash value:
The HashGenerationException is a custom exception (you can find this class in the attachment). The convertByteArrayToHexString() method is implemented as follows:
private static String convertByteArrayToHexString(byte[] arrayBytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < arrayBytes.length; i++) {
stringBuffer.append(Integer.toString((arrayBytes[i] & 0xff) + 0x100, 16)
.substring(1));
}
return stringBuffer.toString();
}
The hashString() is a general method. Here are four public utility methods that are specific to each algorithm (MD5, SHA-1 and SHA-256):
To calculate hash value of a large file effectively, it’s recommended to repeatedly put a chunk of bytes to the message digest, until reaching end of file. Here’s such method:
private static String hashFile(File file, String algorithm)
throws HashGenerationException {
try (FileInputStream inputStream = new FileInputStream(file)) {
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] bytesBuffer = new byte[1024];
int bytesRead = -1;
while ((bytesRead = inputStream.read(bytesBuffer)) != -1) {
digest.update(bytesBuffer, 0, bytesRead);
}
byte[] hashedBytes = digest.digest();
return convertByteArrayToHexString(hashedBytes);
} catch (NoSuchAlgorithmException | IOException ex) {
throw new HashGenerationException(
"Could not generate hash from file", ex);
}
}
Here are four public methods that are specific to each algorithm:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Can you put me through how to this follow issue...hashing product for simple text file hashing product managing hashes within the product (storage, e.g. file, array, etc) signature detection, comparison of new hashes with the library
Comments
is there any way to decrypt HMACSHA-256 generated value
file hashing product
managing hashes within the product (storage, e.g. file, array, etc)
signature detection, comparison of new hashes with the library