Java Digest digest(String provider, File file, int radix)

Here you can find the source of digest(String provider, File file, int radix)

Description

Digest a file with a specified provider.

License

Open Source License

Parameter

Parameter Description
provider the name of the provider (e.g., "MD5") for use in digesting the file.
file the file to digest.
radix the radix of the returned result.

Return

the digest of the file, converted to an integer string in the specified radix, or the empty string if an error occurs.

Declaration

public static String digest(String provider, File file, int radix) 

Method Source Code


//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2009 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

public class Main {
    /**/*w w  w  . j  a  v  a  2  s  .  c  o  m*/
     * Digest a file with a specified provider.
     * @param provider the name of the provider (e.g., "MD5")
     * for use in digesting the file.
     * @param file the file to digest.
     * @param radix the radix of the returned result.
     * @return the digest of the file, converted to an integer
     * string in the specified radix, or the empty string if
     * an error occurs.
     */
    public static String digest(String provider, File file, int radix) {
        String result;
        BufferedInputStream bis = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(provider);
            bis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[8192];
            int n;
            while ((n = bis.read(buffer)) != -1)
                messageDigest.update(buffer, 0, n);
            byte[] hashed = messageDigest.digest();
            BigInteger bi = new BigInteger(1, hashed);
            result = bi.toString(radix);
        } catch (Exception ex) {
            result = "";
        } finally {
            try {
                bis.close();
            } catch (Exception ignore) {
            }
        }
        return result;
    }
}

Related

  1. digest(String name, String source)
  2. digest(String orgin, String algorithm)
  3. digest(String password)
  4. digest(String plain, String algorithm)
  5. digest(String planeText)
  6. digest(String raw, String algorithm)
  7. digest(String s, String algorithm)
  8. digest(String source)
  9. digest(String source, byte[] salt)