001/*
002 * The contents of this file are subject to the license and copyright detailed
003 * in the LICENSE and NOTICE files at the root of the source tree.
004 */
005package org.duraspace.bagit;
006
007import java.security.MessageDigest;
008import java.security.NoSuchAlgorithmException;
009
010public enum BagItDigest {
011    MD5("md5", "MD5"), SHA1("sha1", "SHA-1"), SHA256("sha256", "SHA-256"), SHA512("sha512", "SHA-512");
012
013    private final String bagItName;
014    private final String javaName;
015
016    BagItDigest(final String bagItName, final String javaName) {
017        this.bagItName = bagItName;
018        this.javaName = javaName;
019    }
020
021    /**
022     * Retrieve the bagit formatted version of the algorithm
023     *
024     * @return the algorithm name
025     */
026    public String bagitName() {
027        return bagItName;
028    }
029
030    /**
031     * Retrieve a {@link MessageDigest} for the given algorithm
032     *
033     * @return the MessageDigest
034     */
035    public MessageDigest messageDigest() {
036        try {
037            return MessageDigest.getInstance(javaName);
038        } catch (NoSuchAlgorithmException e) {
039            // this should never happen with known digest types
040            throw new AssertionError(e.getMessage(), e);
041        }
042    }
043}