MessageDigestAlgorithms.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.codec.digest;

  18. import java.security.MessageDigest;

  19. /**
  20.  * Standard {@link MessageDigest} algorithm names from the <cite>Java Cryptography Architecture Standard Algorithm Name
  21.  * Documentation</cite>.
  22.  * <p>
  23.  * This class is immutable and thread-safe.
  24.  * </p>
  25.  * <ul>
  26.  * <li>Java 8 and up: SHA-224.</li>
  27.  * <li>Java 9 and up: SHA3-224, SHA3-256, SHA3-384, SHA3-512.</li>
  28.  * </ul>
  29.  *
  30.  * @see <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest">
  31.  *      Java 8 Cryptography Architecture Standard Algorithm Name Documentation</a>
  32.  * @see <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html#messagedigest-algorithms">
  33.  *      Java 11 Cryptography Architecture Standard Algorithm Name Documentation</a>
  34.  * @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#messagedigest-algorithms">
  35.  *      Java 17 Cryptography Architecture Standard Algorithm Name Documentation</a>
  36.  * @see <a href="https://docs.oracle.com/en/java/javase/21/docs/specs/security/standard-names.html#messagedigest-algorithms">
  37.  *      Java 21 Cryptography Architecture Standard Algorithm Name Documentation</a>
  38.  *
  39.  * @see <a href="https://dx.doi.org/10.6028/NIST.FIPS.180-4">FIPS PUB 180-4</a>
  40.  * @see <a href="https://dx.doi.org/10.6028/NIST.FIPS.202">FIPS PUB 202</a>
  41.  * @since 1.7
  42.  */
  43. public class MessageDigestAlgorithms {

  44.     /**
  45.      * The MD2 message digest algorithm defined in RFC 1319.
  46.      */
  47.     public static final String MD2 = "MD2";

  48.     /**
  49.      * The MD5 message digest algorithm defined in RFC 1321.
  50.      */
  51.     public static final String MD5 = "MD5";

  52.     /**
  53.      * The SHA-1 hash algorithm defined in the FIPS PUB 180-2.
  54.      */
  55.     public static final String SHA_1 = "SHA-1";

  56.     /**
  57.      * The SHA-224 hash algorithm defined in the FIPS PUB 180-3.
  58.      * <p>
  59.      * Present in Oracle Java 8.
  60.      * </p>
  61.      *
  62.      * @since 1.11
  63.      */
  64.     public static final String SHA_224 = "SHA-224";

  65.     /**
  66.      * The SHA-256 hash algorithm defined in the FIPS PUB 180-2.
  67.      */
  68.     public static final String SHA_256 = "SHA-256";

  69.     /**
  70.      * The SHA-384 hash algorithm defined in the FIPS PUB 180-2.
  71.      */
  72.     public static final String SHA_384 = "SHA-384";

  73.     /**
  74.      * The SHA-512 hash algorithm defined in the FIPS PUB 180-2.
  75.      */
  76.     public static final String SHA_512 = "SHA-512";

  77.     /**
  78.      * The SHA-512 hash algorithm defined in the FIPS PUB 180-4.
  79.      * <p>
  80.      * Included starting in Oracle Java 9.
  81.      * </p>
  82.      *
  83.      * @since 1.14
  84.      */
  85.     public static final String SHA_512_224 = "SHA-512/224";

  86.     /**
  87.      * The SHA-512 hash algorithm defined in the FIPS PUB 180-4.
  88.      * <p>
  89.      * Included starting in Oracle Java 9.
  90.      * </p>
  91.      *
  92.      * @since 1.14
  93.      */
  94.     public static final String SHA_512_256 = "SHA-512/256";

  95.     /**
  96.      * The SHA3-224 hash algorithm defined in the FIPS PUB 202.
  97.      * <p>
  98.      * Included starting in Oracle Java 9.
  99.      * </p>
  100.      *
  101.      * @since 1.11
  102.      */
  103.     public static final String SHA3_224 = "SHA3-224";

  104.     /**
  105.      * The SHA3-256 hash algorithm defined in the FIPS PUB 202.
  106.      * <p>
  107.      * Included starting in Oracle Java 9.
  108.      * </p>
  109.      *
  110.      * @since 1.11
  111.      */
  112.     public static final String SHA3_256 = "SHA3-256";

  113.     /**
  114.      * The SHA3-384 hash algorithm defined in the FIPS PUB 202.
  115.      * <p>
  116.      * Included starting in Oracle Java 9.
  117.      * </p>
  118.      *
  119.      * @since 1.11
  120.      */
  121.     public static final String SHA3_384 = "SHA3-384";

  122.     /**
  123.      * The SHA3-512 hash algorithm defined in the FIPS PUB 202.
  124.      * <p>
  125.      * Included starting in Oracle Java 9.
  126.      * </p>
  127.      *
  128.      * @since 1.11
  129.      */
  130.     public static final String SHA3_512 = "SHA3-512";

  131.     /**
  132.      * Gets all constant values defined in this class.
  133.      *
  134.      * @return all constant values defined in this class.
  135.      * @since 1.11
  136.      */
  137.     public static String[] values() {
  138.         // Do not use a constant array here as that can be changed externally by accident or design
  139.         return new String[] {
  140.             MD2, MD5, SHA_1, SHA_224, SHA_256, SHA_384,
  141.             SHA_512, SHA_512_224, SHA_512_256, SHA3_224, SHA3_256, SHA3_384, SHA3_512
  142.         };
  143.     }

  144.     private MessageDigestAlgorithms() {
  145.         // cannot be instantiated.
  146.     }

  147. }