1package org.apache.archiva.checksum;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */212223import org.apache.commons.io.FilenameUtils;
2425import java.nio.file.Path;
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.Collections;
29import java.util.HashMap;
30import java.util.HashSet;
31import java.util.List;
32import java.util.Map;
33import java.util.Set;
3435/**36 * Enumeration of available ChecksumAlgorithm techniques.37 *38 * Each algorithm represents a message digest algorithm and has a unique type.39 * The type string may be used in the hash files (FreeBSD and OpenSSL add the type to the hash file)40 *41 * There are multiple file extensions. The first one is considered the default extension.42 *43 */44public enum ChecksumAlgorithm {
45 MD5("MD5", "MD5", "md5"),
46 SHA1("SHA-1", "SHA1", "sha1", "sha128", "sha-128"),
47 SHA256("SHA-256", "SHA256", "sha256", "sha2", "sha-256"),
48 SHA384("SHA-384", "SHA384", "sha384", "sha3", "sha-384"),
49 SHA512("SHA-512", "SHA512", "sha512", "sha5", "sha-512"),
50 ASC("ASC", "ASC", "asc");
5152publicstaticChecksumAlgorithm getByExtension( Path file )
53 {
54 String ext = FilenameUtils.getExtension( file.getFileName().toString() ).toLowerCase();
55if (extensionMap.containsKey(ext)) {
56return extensionMap.get(ext);
57 }
58thrownew IllegalArgumentException( "Filename " + file.getFileName() + " has no valid extension." );
59 }
6061privatestaticfinal Map<String, ChecksumAlgorithm> extensionMap = new HashMap<>( );
6263static {
64for (ChecksumAlgorithm alg : ChecksumAlgorithm.values()) {
65for (String extString : alg.getExt())
66 {
67 extensionMap.put( extString.toLowerCase(), alg );
68 }
69 }
70 }
7172publicstatic Set<String> getAllExtensions() {
73return extensionMap.keySet();
74 }
7576/**77 * The MessageDigest algorithm for this hash.78 */79privatefinal String algorithm;
8081/**82 * The file extensions for this ChecksumAlgorithm.83 */84privatefinal List<String> ext;
8586/**87 * The checksum type, the key that you see in checksum files.88 */89privatefinal String type;
9091/**92 * Construct a ChecksumAlgorithm93 * 94 * @param algorithm the MessageDigest algorithm95 * @param type a unique identifier for the type96 * @param ext the list of file extensions97 */98privateChecksumAlgorithm( String algorithm, String type, String... ext )
99 {
100this.algorithm = algorithm;
101this.ext = Arrays.asList( ext );
102this.type = type;
103104 }
105106/**107 * Returns the message digest algorithm identifier108 * @return109 */110public String getAlgorithm()
111 {
112return algorithm;
113 }
114115/**116 * Returns the list of extensions117 * @return118 */119public List<String> getExt()
120 {
121return ext;
122 }
123124/**125 * Returns the checksum identifier126 * @return127 */128public String getType()
129 {
130return type;
131 }
132133/**134 * Returns the default extension of the current algorithm135 * @return136 */137public String getDefaultExtension() {
138return ext.get(0);
139 }
140141142 }