I am trying to get a hash of a file as quickly as possible. I have a program in which large data arrays (100 GB +) are hashed, consisting of arbitrary file sizes (from a few kilobytes to 5 GB per file) anywhere between several files up to several hundred thousand files.
The program must support all supported Java algorithms (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512).
I am currently using:
public String getHash(String file, String hashAlgo) throws IOException, HashTypeException { StringBuffer hexString = null; try { MessageDigest md = MessageDigest.getInstance(validateHashType(hashAlgo)); FileInputStream fis = new FileInputStream(file); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); } fis.close(); byte[] mdbytes = md.digest(); hexString = new StringBuffer(); for (int i = 0; i < mdbytes.length; i++) { hexString.append(Integer.toHexString((0xFF & mdbytes[i]))); } return hexString.toString(); } catch (NoSuchAlgorithmException | HashTypeException e) { throw new HashTypeException("Unsuppored Hash Algorithm.", e); } }
Is there a better way to get a hash of files? I am looking for extreme performance, and I'm not sure that I took this path in the best way.
source share