Java Digest digest(MessageDigest method, File f)

Here you can find the source of digest(MessageDigest method, File f)

Description

digest

License

Apache License

Declaration

public static String digest(MessageDigest method, File f)
            throws NoSuchAlgorithmException, FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*//from   w w  w .j a  v  a 2 s  .c o m
 * Copyright 2011 Future Systems
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final String[] hexTable = new String[256];

    public static String digest(MessageDigest method, File f)
            throws NoSuchAlgorithmException, FileNotFoundException, IOException {

        byte[] b = new byte[4096];
        FileInputStream fs = new FileInputStream(f);
        while (true) {
            int readBytes = fs.read(b);
            if (readBytes < 0)
                break;

            method.update(b, 0, readBytes);
        }

        byte[] digest = method.digest();
        return toHexString(digest);
    }

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

Related

  1. digest(final String algorithm, final byte[] bytes)
  2. digest(final String password)
  3. digest(InputStream input, String algorithm)
  4. digest(InputStream is, String digestAlgorithm)
  5. digest(MessageDigest digest, InputStream data)
  6. digest(Serializable object)
  7. digest(String alg, byte[] plainByte)
  8. digest(String algorithm, byte[] bytes)
  9. digest(String algorithm, byte[] data)