Java Hash Code Calculate generateHash(File file)

Here you can find the source of generateHash(File file)

Description

generate Hash

License

Open Source License

Declaration

public static byte[] generateHash(File file) throws IOException, NoSuchAlgorithmException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String generateHash(String string) throws NoSuchAlgorithmException, IOException {
        return hashToString(generateHash(string.getBytes("UTF-8")));
    }/*  ww w . j  ava2 s  .c o m*/

    public static byte[] generateHash(File file) throws IOException, NoSuchAlgorithmException {
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        byte hash[] = null;

        try {
            hash = generateHash(in);
        } finally {
            in.close();
        }

        return hash;
    }

    public static byte[] generateHash(InputStream in) throws IOException, NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");

        byte buffer[] = new byte[16 * 1024];
        while (true) {
            int read = in.read(buffer, 0, buffer.length);
            if (read == -1) {
                break;
            }
            md.update(buffer, 0, read);
        }

        return md.digest();
    }

    public static byte[] generateHash(byte data[]) throws NoSuchAlgorithmException {
        return generateHash(data, 0, data.length);
    }

    public static byte[] generateHash(byte data[], int offset, int length) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data, offset, length);
        return md.digest();
    }

    public static String hashToString(byte hash[]) {
        StringBuilder sb = new StringBuilder();
        for (byte b : hash) {
            int nibble1 = (b & 0xF0) >> 4;
            int nibble2 = b & 0x0F;
            sb.append(Integer.toHexString(nibble1));
            sb.append(Integer.toHexString(nibble2));
        }
        return sb.toString();
    }
}

Related

  1. combineHashesMurmur(int hash2, int hash1)
  2. combineHashesOld(int hash1, int hash2)
  3. combineHashesUnsorted(final int a, final int b)
  4. generateHash(byte[] data)
  5. generateHash(char[] password, byte[] salt)
  6. generateHash(final String data)
  7. generateHash(final String input)
  8. generateHash(final String msg, final String hashAlgorithm)
  9. generateHash(String algo, byte[]... bytes)