This project has retired. For details please refer to its Attic page.
ChecksumAlgorithm xref
View Javadoc
1   package org.apache.archiva.checksum;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import org.apache.commons.io.FilenameUtils;
24  
25  import java.nio.file.Path;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Set;
34  
35  /**
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   */
44  public 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");
51  
52      public static ChecksumAlgorithm getByExtension( Path file )
53      {
54          String ext = FilenameUtils.getExtension( file.getFileName().toString() ).toLowerCase();
55          if (extensionMap.containsKey(ext)) {
56              return extensionMap.get(ext);
57          }
58          throw new IllegalArgumentException( "Filename " + file.getFileName() + " has no valid extension." );
59      }
60  
61      private static final Map<String, ChecksumAlgorithm> extensionMap = new HashMap<>(  );
62  
63      static {
64          for (ChecksumAlgorithm alg : ChecksumAlgorithm.values()) {
65              for (String extString : alg.getExt())
66              {
67                  extensionMap.put( extString.toLowerCase(), alg );
68              }
69          }
70      }
71  
72      public static Set<String> getAllExtensions() {
73          return extensionMap.keySet();
74      }
75  
76      /**
77       * The MessageDigest algorithm for this hash.
78       */
79      private final String algorithm;
80  
81      /**
82       * The file extensions for this ChecksumAlgorithm.
83       */
84      private final List<String> ext;
85  
86      /**
87       * The checksum type, the key that you see in checksum files.
88       */
89      private final String type;
90  
91      /**
92       * Construct a ChecksumAlgorithm
93       * 
94       * @param algorithm the MessageDigest algorithm
95       * @param type a unique identifier for the type
96       * @param ext the list of file extensions
97       */
98      private ChecksumAlgorithm( String algorithm, String type, String... ext )
99      {
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 }