001/**
002 * Copyright 2014 Florent Daigniere
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package net.jsign;
018
019import java.security.MessageDigest;
020import java.security.NoSuchAlgorithmException;
021
022import org.bouncycastle.asn1.ASN1ObjectIdentifier;
023import org.bouncycastle.tsp.TSPAlgorithms;
024
025/**
026 * Digest algorithm.
027 * 
028* @author Florent Daigniere
029* @since 1.3
030*/
031public enum DigestAlgorithm {
032    MD5("MD5", TSPAlgorithms.MD5),
033    SHA1("SHA-1", TSPAlgorithms.SHA1),
034    SHA256("SHA-256", TSPAlgorithms.SHA256),
035    SHA384("SHA-384", TSPAlgorithms.SHA384),
036    SHA512("SHA-512", TSPAlgorithms.SHA512);
037
038    /** The JCE name of the algorithm */
039    public final String id;
040
041    /** The object identifier of the algorithm */
042    public final ASN1ObjectIdentifier oid;
043
044    DigestAlgorithm(String id, ASN1ObjectIdentifier oid) {
045        this.id = id;
046        this.oid = oid;
047    }
048
049    /**
050     * Parse the specified value and returns the corresponding digest algorithm.
051     * This method is more permissive than {@link #valueOf(String)}, it's case
052     * insensitive and ignores hyphens.
053     * 
054     * @param name the name of the digest algorithm
055     * @return the digest algorithm, or <code>null</code> if the name specified doesn't match any supported algorithm
056     */
057    public static DigestAlgorithm of(String name) {
058        if (name == null) {
059            return null;
060        }
061        
062        name = name.toUpperCase().replaceAll("-", "");
063        for (DigestAlgorithm algorithm : values()) {
064            if (algorithm.name().equals(name)) {
065                return algorithm;
066            }
067        }
068        
069        if ("SHA2".equals(name)) {
070            return SHA256;
071        }
072        
073        return null;
074    }
075
076    /**
077     * Return the algorithm matching the specified object identifier.
078     * 
079     * @param oid the ASN.1 object identifier of the algorithm
080     * @return the digest algorithm, or <code>null</code> if none matches the specified oid
081     */
082    public static DigestAlgorithm of(ASN1ObjectIdentifier oid) {
083        for (DigestAlgorithm algorithm : values()) {
084            if (algorithm.oid.equals(oid)) {
085                return algorithm;
086            }
087        }
088        
089        return null;
090    }
091
092    /**
093     * Return a MessageDigest for this algorithm.
094     * 
095     * @return a MessageDigest for this algorithm
096     */
097    public MessageDigest getMessageDigest() {
098        try {
099            return MessageDigest.getInstance(id);
100        } catch (NoSuchAlgorithmException e) {
101            throw new RuntimeException(e);
102        }
103    }
104
105    /**
106     * Return the default algorithm (currently SHA-256, SHA-1 has been deprecated since January 1st 2016).
107     * 
108     * @return the default algorithm ({@link #SHA256})
109     * @see <a href="http://social.technet.microsoft.com/wiki/contents/articles/1760.windows-root-certificate-program-technical-requirements-version-2-0.aspx">Windows Root Certificate Program - Technical Requirements version 2.0</a>
110     * @see <a href="http://blogs.technet.com/b/pki/archive/2011/02/08/common-questions-about-sha2-and-windows.aspx">Common Questions about SHA2 and Windows</a>
111     */
112    public static DigestAlgorithm getDefault() {
113        return SHA256;
114    }
115}