001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.config;
019
020import static java.util.stream.Collectors.toSet;
021
022import java.util.Arrays;
023import java.util.Set;
024import java.util.stream.Stream;
025
026/**
027 * Digest Algorith enum
028 * @author cbeer
029 *
030 * Moved to its own class for Fedora 6.0.0
031 */
032public enum DigestAlgorithm {
033    SHA1("SHA", "urn:sha1", "sha-1", "sha1"),
034    SHA256("SHA-256", "urn:sha-256", "sha256"),
035    SHA512("SHA-512", "urn:sha-512", "sha512"),
036    SHA512256("SHA-512/256", "urn:sha-512/256", "sha512/256"),
037    MD5("MD5", "urn:md5"),
038    MISSING("NONE", "missing");
039
040    final private String algorithm;
041    final private String scheme;
042    final private Set<String> aliases;
043
044    DigestAlgorithm(final String alg, final String scheme, final String... aliases) {
045        this.algorithm = alg;
046        this.scheme = scheme;
047        this.aliases = Stream.concat(Arrays.stream(aliases), Stream.of(algorithm)).map(String::valueOf)
048                        .map(String::toLowerCase).collect(toSet());
049    }
050
051    /**
052     * Return the scheme associated with the provided algorithm (e.g. SHA-1 returns urn:sha1)
053     *
054     * @param alg for which scheme is requested
055     * @return scheme
056     */
057    public static String getScheme(final String alg) {
058        return Arrays.stream(values()).filter(value ->
059                value.algorithm.equalsIgnoreCase(alg) || value.algorithm.replace("-", "").equalsIgnoreCase(alg)
060        ).findFirst().orElse(MISSING).scheme;
061    }
062
063    /**
064     * Return enum value for the provided scheme (e.g. urn:sha1 returns SHA-1)
065     *
066     * @param argScheme for which enum is requested
067     * @return enum value associated with the arg scheme
068     */
069    public static DigestAlgorithm fromScheme(final String argScheme) {
070        return Arrays.stream(values()).filter(value -> value.scheme.equalsIgnoreCase(argScheme)
071        ).findFirst().orElse(MISSING);
072    }
073
074    /**
075     * Return enum value for the provided algorithm
076     *
077     * @param alg algorithm name to seek
078     * @return enum value associated with the algorithm name, or missing if not found
079     */
080    public static DigestAlgorithm fromAlgorithm(final String alg) {
081        final String seek = alg.toLowerCase();
082        return Arrays.stream(values())
083                .filter(value -> value.aliases.contains(seek))
084                .findFirst()
085                .orElse(MISSING);
086    }
087
088    /**
089     * Return true if the provided algorithm is included in this enum
090     *
091     * @param alg to test
092     * @return true if arg algorithm is supported
093     */
094    public static boolean isSupportedAlgorithm(final String alg) {
095        return !getScheme(alg).equals(MISSING.scheme);
096    }
097
098    /**
099     * @return the aliases
100     */
101    public Set<String> getAliases() {
102        return aliases;
103    }
104
105    /**
106     * @return the algorithm
107     */
108    public String getAlgorithm() {
109        return algorithm;
110    }
111}