Java Checksum Calculate createChecksum(File file, String shaAlgo)

Here you can find the source of createChecksum(File file, String shaAlgo)

Description

create Checksum

License

Apache License

Declaration

public static String createChecksum(File file, String shaAlgo) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.xml.bind.DatatypeConverter;
import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;

import java.security.MessageDigest;

public class Main {
    public static String createChecksum(File file, String shaAlgo) {
        try {//from  w  ww .j a v a2s .  c  o  m
            InputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            MessageDigest complete = MessageDigest.getInstance(shaAlgo);
            int numRead;

            do {
                numRead = fis.read(buffer);
                if (numRead > 0) {
                    complete.update(buffer, 0, numRead);
                }
            } while (numRead != -1);

            fis.close();
            return DatatypeConverter.printHexBinary(complete.digest()).toLowerCase();
        } catch (Exception e) {
            throw new IllegalStateException(
                    "could not create checksum for " + file + " and algo " + shaAlgo + ": " + e.getMessage(), e);
        }
    }
}

Related

  1. checksum(String nmea)
  2. checksum_icmpv6(byte pkt[])
  3. checksumBytesToString(byte[] digestBytes)
  4. checksumFile(File file)
  5. checksumLength(int algorithmCode, int dataLength)
  6. generateChecksum(InputStream is)
  7. generateChecksum(String path, String flavor)
  8. generateChecksum(String pType, String pInFile)
  9. generateCheckSum(String string)