You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChecksumAlgorithm.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package org.apache.archiva.checksum;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.commons.io.FilenameUtils;
  21. import java.nio.file.Path;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. /**
  31. * Enumeration of available ChecksumAlgorithm techniques.
  32. *
  33. * Each algorithm represents a message digest algorithm and has a unique type.
  34. * The type string may be used in the hash files (FreeBSD and OpenSSL add the type to the hash file)
  35. *
  36. * There are multiple file extensions. The first one is considered the default extension.
  37. *
  38. */
  39. public enum ChecksumAlgorithm {
  40. MD5("MD5", "MD5", "md5"),
  41. SHA1("SHA-1", "SHA1", "sha1", "sha128", "sha-128"),
  42. SHA256("SHA-256", "SHA256", "sha256", "sha2", "sha-256"),
  43. SHA384("SHA-384", "SHA384", "sha384", "sha3", "sha-384"),
  44. SHA512("SHA-512", "SHA512", "sha512", "sha5", "sha-512"),
  45. ASC("ASC", "ASC", "asc");
  46. public static ChecksumAlgorithm getByExtension( Path file )
  47. {
  48. String ext = FilenameUtils.getExtension( file.getFileName().toString() ).toLowerCase();
  49. if (extensionMap.containsKey(ext)) {
  50. return extensionMap.get(ext);
  51. }
  52. throw new IllegalArgumentException( "Filename " + file.getFileName() + " has no valid extension." );
  53. }
  54. private static final Map<String, ChecksumAlgorithm> extensionMap = new HashMap<>( );
  55. static {
  56. for (ChecksumAlgorithm alg : ChecksumAlgorithm.values()) {
  57. for (String extString : alg.getExt())
  58. {
  59. extensionMap.put( extString.toLowerCase(), alg );
  60. }
  61. }
  62. }
  63. public static Set<String> getAllExtensions() {
  64. return extensionMap.keySet();
  65. }
  66. /**
  67. * The MessageDigest algorithm for this hash.
  68. */
  69. private final String algorithm;
  70. /**
  71. * The file extensions for this ChecksumAlgorithm.
  72. */
  73. private final List<String> ext;
  74. /**
  75. * The checksum type, the key that you see in checksum files.
  76. */
  77. private final String type;
  78. /**
  79. * Construct a ChecksumAlgorithm
  80. *
  81. * @param algorithm the MessageDigest algorithm
  82. * @param type a unique identifier for the type
  83. * @param ext the list of file extensions
  84. */
  85. private ChecksumAlgorithm( String algorithm, String type, String... ext )
  86. {
  87. this.algorithm = algorithm;
  88. this.ext = Arrays.asList( ext );
  89. this.type = type;
  90. }
  91. /**
  92. * Returns the message digest algorithm identifier
  93. * @return
  94. */
  95. public String getAlgorithm()
  96. {
  97. return algorithm;
  98. }
  99. /**
  100. * Returns the list of extensions
  101. * @return
  102. */
  103. public List<String> getExt()
  104. {
  105. return ext;
  106. }
  107. /**
  108. * Returns the checksum identifier
  109. * @return
  110. */
  111. public String getType()
  112. {
  113. return type;
  114. }
  115. /**
  116. * Returns the default extension of the current algorithm
  117. * @return
  118. */
  119. public String getDefaultExtension() {
  120. return ext.get(0);
  121. }
  122. }