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 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 java.io.File;
  21. import org.apache.commons.io.FilenameUtils;
  22. /**
  23. * Enumeration of available ChecksumAlgorithm techniques.
  24. *
  25. *
  26. */
  27. public enum ChecksumAlgorithm {
  28. SHA1("SHA-1", "sha1", "SHA1"),
  29. MD5("MD5", "md5", "MD5");
  30. public static ChecksumAlgorithm getByExtension( File file )
  31. {
  32. String ext = FilenameUtils.getExtension( file.getName() ).toLowerCase();
  33. if ( ChecksumAlgorithm.SHA1.getExt().equals( ext ) )
  34. {
  35. return ChecksumAlgorithm.SHA1;
  36. }
  37. else if ( ChecksumAlgorithm.MD5.getExt().equals( ext ) )
  38. {
  39. return ChecksumAlgorithm.MD5;
  40. }
  41. throw new IllegalArgumentException( "Filename " + file.getName() + " has no associated extension." );
  42. }
  43. /**
  44. * The MessageDigest algorithm for this hash.
  45. */
  46. private String algorithm;
  47. /**
  48. * The file extension for this ChecksumAlgorithm.
  49. */
  50. private String ext;
  51. /**
  52. * The checksum type, the key that you see in checksum files.
  53. */
  54. private String type;
  55. /**
  56. * Construct a ChecksumAlgorithm
  57. *
  58. * @param algorithm the MessageDigest algorithm
  59. * @param ext the file extension.
  60. * @param type the checksum type.
  61. */
  62. private ChecksumAlgorithm( String algorithm, String ext, String type )
  63. {
  64. this.algorithm = algorithm;
  65. this.ext = ext;
  66. this.type = type;
  67. }
  68. public String getAlgorithm()
  69. {
  70. return algorithm;
  71. }
  72. public String getExt()
  73. {
  74. return ext;
  75. }
  76. public String getType()
  77. {
  78. return type;
  79. }
  80. }