1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.core.checksums; 17 18 import java.util.Map; 19 import software.amazon.awssdk.annotations.SdkPublicApi; 20 import software.amazon.awssdk.utils.StringUtils; 21 import software.amazon.awssdk.utils.internal.EnumUtils; 22 23 /** 24 * Enum that indicates all the checksums supported by Flexible checksums in a Service Request/Response Header. 25 */ 26 @SdkPublicApi 27 public enum Algorithm { 28 29 CRC32C("crc32c", 8), 30 CRC32("crc32", 8), 31 SHA256("sha256", 44), 32 SHA1("sha1", 28), 33 ; 34 35 private static final Map<String, Algorithm> VALUE_MAP = EnumUtils.uniqueIndex(Algorithm.class, 36 a -> StringUtils.upperCase(a.value)); 37 38 private final String value; 39 private final int length; 40 Algorithm(String value, int length)41 Algorithm(String value, int length) { 42 this.value = value; 43 this.length = length; 44 } 45 fromValue(String value)46 public static Algorithm fromValue(String value) { 47 if (value == null) { 48 return null; 49 } 50 // The clients will send the algorithm name in all upper case 51 // try using that name directly and if not found then normalize 52 // it and try again. 53 Algorithm algorithm = VALUE_MAP.get(value); 54 if (algorithm == null) { 55 String normalizedValue = StringUtils.upperCase(value); 56 algorithm = VALUE_MAP.get(normalizedValue); 57 if (algorithm == null) { 58 throw new IllegalArgumentException("The provided value is not a valid algorithm " + value); 59 } 60 } 61 return algorithm; 62 } 63 64 @Override toString()65 public String toString() { 66 return String.valueOf(value); 67 } 68 69 /** 70 * Length corresponds to Base64Encoded length for a given Checksum. 71 * This is always fixed for a checksum. 72 * @return length of base64 Encoded checksum. 73 */ base64EncodedLength()74 public Integer base64EncodedLength() { 75 return this.length; 76 } 77 78 79 } 80