// Copyright 2022 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package google.registry.tools;

import java.util.Optional;

/**
 * Enumerates the DNSSEC digest types for use with Delegation Signer records.
 *
 * <p>This also enforces the set of types that are valid for use with Cloud DNS. Customers cannot
 * create DS records containing any other digest type.
 *
 * <p>The complete list can be found here:
 * https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml
 */
public enum DigestType {
  SHA1(1, 20),
  SHA256(2, 32),
  // Algorithm number 3 is GOST R 34.11-94 and is deliberately NOT SUPPORTED.
  // This algorithm was reviewed by ise-crypto and deemed academically broken (b/207029800).
  // In addition, RFC 8624 specifies that this algorithm MUST NOT be used for DNSSEC delegations.
  // TODO(sarhabot@): Add note in Cloud DNS code to notify the Registry of any new changes to
  // supported digest types.
  SHA384(4, 48);

  private final int wireValue;
  private final int bytes;

  DigestType(int wireValue, int bytes) {
    this.wireValue = wireValue;
    this.bytes = bytes;
  }

  /** Fetches a DigestType enumeration constant by its IANA assigned value. */
  public static Optional<DigestType> fromWireValue(int wireValue) {
    for (DigestType alg : DigestType.values()) {
      if (alg.getWireValue() == wireValue) {
        return Optional.of(alg);
      }
    }
    return Optional.empty();
  }

  /** Fetches a value in the range [0, 255] that encodes this DS digest type on the wire. */
  public int getWireValue() {
    return wireValue;
  }

  /** Returns the expected length in bytes of the signature. */
  public int getBytes() {
    return bytes;
  }
}

@@ -644,16 +644,127 @@
 tools;%0A%0A
+import com.google.common.collect.ImmutableMap;%0Aimport com.google.common.collect.Maps;%0Aimport java.util.Arrays;%0A
 import j
@@ -1771,320 +1771,407 @@
 %0A%0A  
-/** Fetches a DigestType enumeration constant by its IANA assigned value. */%0A  public static Optional%3CDigestType%3E fromWireValue(int wireValue) %7B%0A    for (DigestType alg : DigestType.values()) %7B%0A      if (alg.getWireValue() == wireValue) %7B%0A        return Optional.of(alg);%0A      %7D%0A    %7D%0A    return Optional.empty(
+private static final ImmutableMap%3CInteger, DigestType%3E WIRE_VALUE_TO_DIGEST_TYPE =%0A      Maps.uniqueIndex(Arrays.stream(DigestType.values()).iterator(), DigestType::getWireValue);%0A%0A  /** Fetches a DigestType enumeration constant by its IANA assigned value. */%0A  public static Optional%3CDigestType%3E fromWireValue(int wireValue) %7B%0A    return Optional.ofNullable(WIRE_VALUE_TO_DIGEST_TYPE.get(wireValue)
 );%0A 

Make DigestType.fromWireValue() more performant (#1555)