Java Hash Calculate getHash(File file, String hashType)

Here you can find the source of getHash(File file, String hashType)

Description

get Hash

License

Apache License

Declaration

private static String getHash(File file, String hashType) throws Exception 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class Main {
    private static String getHash(File file, String hashType) throws Exception {
        InputStream fis = new FileInputStream(file);
        byte buffer[] = new byte[1024];
        MessageDigest md5 = MessageDigest.getInstance(hashType);
        for (int numRead = 0; (numRead = fis.read(buffer)) > 0;) {
            md5.update(buffer, 0, numRead);
        }//from   w  w  w.j a v a2s  . co m

        fis.close();
        return toHexString(md5.digest());
    }

    private static String toHexString(byte b[]) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            sb.append(Integer.toHexString(b[i] & 0xFF));
        }
        return sb.toString();
    }
}

Related

  1. getHash(byte[] data)
  2. getHash(byte[] data, int offset, int length)
  3. getHash(byte[] inputBytes)
  4. getHash(byte[]... bytesToHash)
  5. getHash(File file)
  6. getHash(final byte[] data, final String hashAlgorithm)
  7. getHash(final String text, final Charset charset, final MessageDigest algorithm)
  8. getHash(InputStream in, String algorithm)
  9. getHash(InputStream is, String algorithm)