Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

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

    /**
     * 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();
    }
}