Digests the given message with the default digest algorithm - Android java.security

Android examples for java.security:MessageDigest

Description

Digests the given message with the default digest algorithm

Demo Code


//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final String DEFAULT_DIGEST_ALGORITHM = "SHA-512";

    /**/*from ww w .j ava2 s .co  m*/
     * Digests the given message with the default digest algorithm ({@link CipherUtils#DEFAULT_DIGEST_ALGORITHM})
     *
     * @param originalMessage the original message
     * @return the bytes of the digested message
     */
    public static byte[] digestMessage(byte[] originalMessage) {

        byte[] bytes;

        try {
            bytes = digestMessage(originalMessage, DEFAULT_DIGEST_ALGORITHM);
        } catch (NoSuchAlgorithmException ignored) {
            throw new IllegalStateException(
                    "Default digest algorithm not found");
        }

        return bytes;
    }

    /**
     * Digests the given message with the given algorithm
     *
     * @param originalMessage the original message
     * @param algorithm the algorithm
     * @return the byte [ ]
     * @throws NoSuchAlgorithmException if the given algorithm does not exist in JCA
     */
    public static byte[] digestMessage(byte[] originalMessage,
            String algorithm) throws NoSuchAlgorithmException {
        MessageDigest messageDigest;
        messageDigest = MessageDigest.getInstance(algorithm);

        messageDigest.update(originalMessage, 0, originalMessage.length);
        return messageDigest.digest();
    }
}

Related Tutorials