Java Digest digest(byte[] message, String hash_alg)

Here you can find the source of digest(byte[] message, String hash_alg)

Description

Compute digest

License

Open Source License

Parameter

Parameter Description
message a parameter
hash_alg a parameter

Declaration

public static byte[] digest(byte[] message, String hash_alg) 

Method Source Code

//package com.java2s;
/*   Copyright (C) 2013 Marius C. Silaghi
  Author: Marius Silaghi: msilaghi@fit.edu
  Florida Tech, Human Decision Support Systems Laboratory
       //w  w w  . j  av  a2 s  . c  o  m
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation; either the current version of the License, or
   (at your option) any later version.
       
  This program 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.
      
  You should have received a copy of the GNU Affero General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */

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

public class Main {
    private static final boolean DEBUG = false;

    /**
     * Compute digest
     * @param message
     * @param hash_alg
     * @return
     */
    public static byte[] digest(byte[] message, String hash_alg) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(hash_alg); //Cipher.SHA256
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        if (DEBUG)
            System.out.println("ECC:digest");
        digest.update(message);
        byte result[] = digest.digest();
        return result;
    }

    public static byte[] digest(byte[][] messages, String hash_alg) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(hash_alg); //Cipher.SHA256
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        if (DEBUG)
            System.out.println("ECC:digest");
        for (int k = 0; k < messages.length; k++)
            digest.update(messages[k]);
        byte result[] = digest.digest();
        return result;
    }
}

Related

  1. digest(byte[] data, String algorithm)
  2. digest(byte[] data, String algorithm)
  3. digest(byte[] input)
  4. digest(byte[] input, final String algorithm)
  5. digest(byte[] input, String algorithm, byte[] salt, int iterations)
  6. digest(final @Nullable String[] tokens, @Nullable final Date[] dates)
  7. digest(final byte[] data)
  8. digest(final InputStream inputStream, final MessageDigest digester)
  9. digest(final java.security.MessageDigest messageDigest, final java.nio.ByteBuffer data)