This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.checksum;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022
023import org.apache.commons.io.FilenameUtils;
024
025import java.nio.file.Path;
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.Collections;
029import java.util.HashMap;
030import java.util.HashSet;
031import java.util.List;
032import java.util.Map;
033import java.util.Set;
034
035/**
036 * Enumeration of available ChecksumAlgorithm techniques.
037 *
038 * Each algorithm represents a message digest algorithm and has a unique type.
039 * The type string may be used in the hash files (FreeBSD and OpenSSL add the type to the hash file)
040 *
041 * There are multiple file extensions. The first one is considered the default extension.
042 *
043 */
044public enum ChecksumAlgorithm {
045    MD5("MD5", "MD5", "md5"),
046    SHA1("SHA-1", "SHA1", "sha1", "sha128", "sha-128"),
047    SHA256("SHA-256", "SHA256", "sha256", "sha2", "sha-256"),
048    SHA384("SHA-384", "SHA384", "sha384", "sha3", "sha-384"),
049    SHA512("SHA-512", "SHA512", "sha512", "sha5", "sha-512"),
050    ASC("ASC", "ASC", "asc");
051
052    public static ChecksumAlgorithm getByExtension( Path file )
053    {
054        String ext = FilenameUtils.getExtension( file.getFileName().toString() ).toLowerCase();
055        if (extensionMap.containsKey(ext)) {
056            return extensionMap.get(ext);
057        }
058        throw new IllegalArgumentException( "Filename " + file.getFileName() + " has no valid extension." );
059    }
060
061    private static final Map<String, ChecksumAlgorithm> extensionMap = new HashMap<>(  );
062
063    static {
064        for (ChecksumAlgorithm alg : ChecksumAlgorithm.values()) {
065            for (String extString : alg.getExt())
066            {
067                extensionMap.put( extString.toLowerCase(), alg );
068            }
069        }
070    }
071
072    public static Set<String> getAllExtensions() {
073        return extensionMap.keySet();
074    }
075
076    /**
077     * The MessageDigest algorithm for this hash.
078     */
079    private final String algorithm;
080
081    /**
082     * The file extensions for this ChecksumAlgorithm.
083     */
084    private final List<String> ext;
085
086    /**
087     * The checksum type, the key that you see in checksum files.
088     */
089    private final String type;
090
091    /**
092     * Construct a ChecksumAlgorithm
093     * 
094     * @param algorithm the MessageDigest algorithm
095     * @param type a unique identifier for the type
096     * @param ext the list of file extensions
097     */
098    private ChecksumAlgorithm( String algorithm, String type, String... ext )
099    {
100        this.algorithm = algorithm;
101        this.ext = Arrays.asList( ext );
102        this.type = type;
103
104    }
105
106    /**
107     * Returns the message digest algorithm identifier
108     * @return
109     */
110    public String getAlgorithm()
111    {
112        return algorithm;
113    }
114
115    /**
116     * Returns the list of extensions
117     * @return
118     */
119    public List<String> getExt()
120    {
121        return ext;
122    }
123
124    /**
125     * Returns the checksum identifier
126     * @return
127     */
128    public String getType()
129    {
130        return type;
131    }
132
133    /**
134     * Returns the default extension of the current algorithm
135     * @return
136     */
137    public String getDefaultExtension() {
138        return ext.get(0);
139    }
140    
141    
142}