Java Digest digest(final String algorithm, final byte[] bytes)

Here you can find the source of digest(final String algorithm, final byte[] bytes)

Description

Gets the given byte array's hash value according to the specified algorithm.

License

Open Source License

Parameter

Parameter Description
algorithm The algorithm to use when generating the hash value.
bytes The byte array for which to compute the hash value.

Return

The computed hash value, or null if the digest algorithm is not available.

Declaration

public static byte[] digest(final String algorithm, final byte[] bytes) 

Method Source Code


//package com.java2s;

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

public class Main {
    /**//from  w  ww.  ja  v a2s. c  o m
     * Gets the given byte array's hash value according to the specified
     * algorithm.
     * 
     * @param algorithm The algorithm to use when generating the hash value.
     * @param bytes The byte array for which to compute the hash value.
     * @return The computed hash value, or null if the digest algorithm is not
     *         available.
     * @see MessageDigest
     */
    public static byte[] digest(final String algorithm, final byte[] bytes) {
        try {
            final MessageDigest digest = MessageDigest.getInstance(algorithm);
            digest.update(bytes);
            return digest.digest();
        } catch (final NoSuchAlgorithmException exc) {
            return null;
        }
    }
}

Related

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