Java Digest digest(byte[] buffer)

Here you can find the source of digest(byte[] buffer)

Description

calculate fingerprint of a given byte[] using SHA-1 agorithm

License

Open Source License

Parameter

Parameter Description
buffer the buffer on which calculate the fingerprint

Exception

Parameter Description
NoSuchAlgorithmException an exception
NoSuchProviderException an exception
IOException an exception

Return

finger print

Declaration

@Deprecated
public static byte[] digest(byte[] buffer)
        throws NoSuchAlgorithmException, NoSuchProviderException, IOException 

Method Source Code

//package com.java2s;
/*//www . j a  v a2 s.  c  o  m
 * Copyright (C) 2010 - 2012 Jenia Software.
 *
 * This file is part of Sinekarta
 *
 * Sinekarta is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sinekarta is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

import java.io.IOException;
import java.io.InputStream;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class Main {
    private static final String BC = "BC";
    private static final String SHA_1 = "SHA-1";

    /**
     * calculate fingerprint of a given byte[] using SHA-1 agorithm
     * @param buffer the buffer on which calculate the fingerprint
     * @return finger print
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     * @throws IOException
     */
    @Deprecated
    public static byte[] digest(byte[] buffer)
            throws NoSuchAlgorithmException, NoSuchProviderException, IOException {
        return digest(SHA_1, buffer);
    }

    /**
     * calculate fingerprint of a given byte[] using SHA-256 agorithm
     * @param buffer the buffer on which calculate the fingerprint
     * @return finger print
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     * @throws IOException
     */
    public static byte[] digest(String algorithm, byte[] buffer)
            throws NoSuchAlgorithmException, NoSuchProviderException, IOException {
        MessageDigest md = MessageDigest.getInstance(algorithm, BC);
        md.update(buffer, 0, buffer.length);
        return md.digest();
    }

    /**
     * calculate fingerprint of a given InputStream using SHA-1 agorithm
     * 
     * @param is the inputStream on which calculate the fingerprint
     * @return finger print
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     * @throws IOException
     */
    @Deprecated
    public static byte[] digest(InputStream is)
            throws NoSuchAlgorithmException, NoSuchProviderException, IOException {
        return digest(SHA_1, is);
    }

    /**
     * calculate fingerprint of a given InputStream using given agorithm
     * 
     * @param algorithm the algorithm to use
     * @param is the inputStream on which calculate the fingerprint
     * @return finger print
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     * @throws IOException
     */
    private static byte[] digest(String alghoritm, InputStream is)
            throws NoSuchAlgorithmException, NoSuchProviderException, IOException {
        byte[] buffer = new byte[1024];
        MessageDigest md = MessageDigest.getInstance(alghoritm, BC);
        int read = is.read(buffer);
        while (read != -1) {
            //         System.out.println(byteToHex(buffer,0,read));
            md.update(buffer, 0, read);
            read = is.read(buffer);
        }
        is.close();
        return md.digest();
    }
}

Related

  1. digest(byte[] bytes)
  2. digest(byte[] bytes, String algorithm)
  3. digest(byte[] bytes, String algorithmName)
  4. digest(byte[] bytes, String txt)