Sha2Crypt.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.nio.charset.StandardCharsets;
  19. import java.security.MessageDigest;
  20. import java.security.NoSuchAlgorithmException;
  21. import java.security.SecureRandom;
  22. import java.util.Arrays;
  23. import java.util.Random;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;

  26. /**
  27.  * SHA2-based Unix crypt implementation.
  28.  * <p>
  29.  * Based on the C implementation released into the Public Domain by Ulrich Drepper &lt;drepper@redhat.com&gt;
  30.  * http://www.akkadia.org/drepper/SHA-crypt.txt
  31.  * </p>
  32.  * <p>
  33.  * Conversion to Kotlin and from there to Java in 2012 by Christian Hammers &lt;ch@lathspell.de&gt; and likewise put
  34.  * into the Public Domain.
  35.  * </p>
  36.  * <p>
  37.  * This class is immutable and thread-safe.
  38.  * </p>
  39.  *
  40.  * @since 1.7
  41.  */
  42. public class Sha2Crypt {

  43.     /** Default number of rounds if not explicitly specified. */
  44.     private static final int ROUNDS_DEFAULT = 5000;

  45.     /** Maximum number of rounds. */
  46.     private static final int ROUNDS_MAX = 999_999_999;

  47.     /** Minimum number of rounds. */
  48.     private static final int ROUNDS_MIN = 1000;

  49.     /** Prefix for optional rounds specification. */
  50.     private static final String ROUNDS_PREFIX = "rounds=";

  51.     /** The number of bytes the final hash value will have (SHA-256 variant). */
  52.     private static final int SHA256_BLOCKSIZE = 32;

  53.     /** The prefixes that can be used to identify this crypt() variant (SHA-256). */
  54.     static final String SHA256_PREFIX = "$5$";

  55.     /** The number of bytes the final hash value will have (SHA-512 variant). */
  56.     private static final int SHA512_BLOCKSIZE = 64;

  57.     /** The prefixes that can be used to identify this crypt() variant (SHA-512). */
  58.     static final String SHA512_PREFIX = "$6$";

  59.     /** The pattern to match valid salt values. */
  60.     private static final Pattern SALT_PATTERN = Pattern
  61.             .compile("^\\$([56])\\$(rounds=(\\d+)\\$)?([\\.\\/a-zA-Z0-9]{1,16}).*");

  62.     /**
  63.      * Generates a libc crypt() compatible "$5$" hash value with random salt.
  64.      * <p>
  65.      * See {@link Crypt#crypt(String, String)} for details.
  66.      * </p>
  67.      * <p>
  68.      * A salt is generated for you using {@link SecureRandom}.
  69.      * </p>
  70.      *
  71.      * @param keyBytes
  72.      *            plaintext to hash. Each array element is set to {@code 0} before returning.
  73.      * @return complete hash value
  74.      * @throws IllegalArgumentException
  75.      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
  76.      */
  77.     public static String sha256Crypt(final byte[] keyBytes) {
  78.         return sha256Crypt(keyBytes, null);
  79.     }

  80.     /**
  81.      * Generates a libc6 crypt() compatible "$5$" hash value.
  82.      * <p>
  83.      * See {@link Crypt#crypt(String, String)} for details.
  84.      * </p>
  85.      * @param keyBytes
  86.      *            plaintext to hash. Each array element is set to {@code 0} before returning.
  87.      * @param salt
  88.      *            real salt value without prefix or "rounds=". The salt may be null, in which case a salt
  89.      *            is generated for you using {@link SecureRandom}. If one does not want to use {@link SecureRandom},
  90.      *            you can pass your own {@link Random} in {@link #sha256Crypt(byte[], String, Random)}.
  91.      * @return complete hash value including salt
  92.      * @throws IllegalArgumentException
  93.      *             if the salt does not match the allowed pattern
  94.      * @throws IllegalArgumentException
  95.      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
  96.      */
  97.     public static String sha256Crypt(final byte[] keyBytes, String salt) {
  98.         if (salt == null) {
  99.             salt = SHA256_PREFIX + B64.getRandomSalt(8);
  100.         }
  101.         return sha2Crypt(keyBytes, salt, SHA256_PREFIX, SHA256_BLOCKSIZE, MessageDigestAlgorithms.SHA_256);
  102.     }

  103.     /**
  104.      * Generates a libc6 crypt() compatible "$5$" hash value.
  105.      * <p>
  106.      * See {@link Crypt#crypt(String, String)} for details.
  107.      * </p>
  108.      * @param keyBytes
  109.      *            plaintext to hash. Each array element is set to {@code 0} before returning.
  110.      * @param salt
  111.      *            real salt value without prefix or "rounds=".
  112.      * @param random
  113.      *            the instance of {@link Random} to use for generating the salt.
  114.      *            Consider using {@link SecureRandom} for more secure salts.
  115.      * @return complete hash value including salt
  116.      * @throws IllegalArgumentException
  117.      *             if the salt does not match the allowed pattern
  118.      * @throws IllegalArgumentException
  119.      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
  120.      * @since 1.12
  121.      */
  122.     public static String sha256Crypt(final byte[] keyBytes, String salt, final Random random) {
  123.         if (salt == null) {
  124.             salt = SHA256_PREFIX + B64.getRandomSalt(8, random);
  125.         }
  126.         return sha2Crypt(keyBytes, salt, SHA256_PREFIX, SHA256_BLOCKSIZE, MessageDigestAlgorithms.SHA_256);
  127.     }

  128.     /**
  129.      * Generates a libc6 crypt() compatible "$5$" or "$6$" SHA2 based hash value.
  130.      * <p>
  131.      * This is a nearly line by line conversion of the original C function. The numbered comments are from the algorithm
  132.      * description, the short C-style ones from the original C code and the ones with "Remark" from me.
  133.      * </p>
  134.      * <p>
  135.      * See {@link Crypt#crypt(String, String)} for details.
  136.      * </p>
  137.      *
  138.      * @param keyBytes
  139.      *            plaintext to hash. Each array element is set to {@code 0} before returning.
  140.      * @param salt
  141.      *            real salt value without prefix or "rounds="; may not be null
  142.      * @param saltPrefix
  143.      *            either $5$ or $6$
  144.      * @param blocksize
  145.      *            a value that differs between $5$ and $6$
  146.      * @param algorithm
  147.      *            {@link MessageDigest} algorithm identifier string
  148.      * @return complete hash value including prefix and salt
  149.      * @throws IllegalArgumentException
  150.      *             if the given salt is {@code null} or does not match the allowed pattern
  151.      * @throws IllegalArgumentException
  152.      *             when a {@link NoSuchAlgorithmException} is caught
  153.      * @see MessageDigestAlgorithms
  154.      */
  155.     private static String sha2Crypt(final byte[] keyBytes, final String salt, final String saltPrefix,
  156.             final int blocksize, final String algorithm) {

  157.         final int keyLen = keyBytes.length;

  158.         // Extracts effective salt and the number of rounds from the given salt.
  159.         int rounds = ROUNDS_DEFAULT;
  160.         boolean roundsCustom = false;
  161.         if (salt == null) {
  162.             throw new IllegalArgumentException("Salt must not be null");
  163.         }

  164.         final Matcher m = SALT_PATTERN.matcher(salt);
  165.         if (!m.find()) {
  166.             throw new IllegalArgumentException("Invalid salt value: " + salt);
  167.         }
  168.         if (m.group(3) != null) {
  169.             rounds = Integer.parseInt(m.group(3));
  170.             rounds = Math.max(ROUNDS_MIN, Math.min(ROUNDS_MAX, rounds));
  171.             roundsCustom = true;
  172.         }
  173.         final String saltString = m.group(4);
  174.         final byte[] saltBytes = saltString.getBytes(StandardCharsets.UTF_8);
  175.         final int saltLen = saltBytes.length;

  176.         // 1. start digest A
  177.         // Prepare for the real work.
  178.         MessageDigest ctx = DigestUtils.getDigest(algorithm);

  179.         // 2. the password string is added to digest A
  180.         /*
  181.          * Add the key string.
  182.          */
  183.         ctx.update(keyBytes);

  184.         // 3. the salt string is added to digest A. This is just the salt string
  185.         // itself without the enclosing '$', without the magic salt_prefix $5$ and
  186.         // $6$ respectively and without the rounds=<N> specification.
  187.         //
  188.         // NB: the MD5 algorithm did add the $1$ salt_prefix. This is not deemed
  189.         // necessary since it is a constant string and does not add security
  190.         // and /possibly/ allows a plain text attack. Since the rounds=<N>
  191.         // specification should never be added this would also create an
  192.         // inconsistency.
  193.         /*
  194.          * The last part is the salt string. This must be at most 16 characters and it ends at the first `$' character
  195.          * (for compatibility with existing implementations).
  196.          */
  197.         ctx.update(saltBytes);

  198.         // 4. start digest B
  199.         /*
  200.          * Compute alternate sha512 sum with input KEY, SALT, and KEY. The final result will be added to the first
  201.          * context.
  202.          */
  203.         MessageDigest altCtx = DigestUtils.getDigest(algorithm);

  204.         // 5. add the password to digest B
  205.         /*
  206.          * Add key.
  207.          */
  208.         altCtx.update(keyBytes);

  209.         // 6. add the salt string to digest B
  210.         /*
  211.          * Add salt.
  212.          */
  213.         altCtx.update(saltBytes);

  214.         // 7. add the password again to digest B
  215.         /*
  216.          * Add key again.
  217.          */
  218.         altCtx.update(keyBytes);

  219.         // 8. finish digest B
  220.         /*
  221.          * Now get result of this (32 bytes) and add it to the other context.
  222.          */
  223.         byte[] altResult = altCtx.digest();

  224.         // 9. For each block of 32 or 64 bytes in the password string (excluding
  225.         // the terminating NUL in the C representation), add digest B to digest A
  226.         /*
  227.          * Add for any character in the key one byte of the alternate sum.
  228.          */
  229.         /*
  230.          * (Remark: the C code comment seems wrong for key length > 32!)
  231.          */
  232.         int cnt = keyBytes.length;
  233.         while (cnt > blocksize) {
  234.             ctx.update(altResult, 0, blocksize);
  235.             cnt -= blocksize;
  236.         }

  237.         // 10. For the remaining N bytes of the password string add the first
  238.         // N bytes of digest B to digest A
  239.         ctx.update(altResult, 0, cnt);

  240.         // 11. For each bit of the binary representation of the length of the
  241.         // password string up to and including the highest 1-digit, starting
  242.         // from to the lowest bit position (numeric value 1):
  243.         //
  244.         // a) for a 1-digit add digest B to digest A
  245.         //
  246.         // b) for a 0-digit add the password string
  247.         //
  248.         // NB: this step differs significantly from the MD5 algorithm. It
  249.         // adds more randomness.
  250.         /*
  251.          * Take the binary representation of the length of the key and for every 1 add the alternate sum, for every 0
  252.          * the key.
  253.          */
  254.         cnt = keyBytes.length;
  255.         while (cnt > 0) {
  256.             if ((cnt & 1) != 0) {
  257.                 ctx.update(altResult, 0, blocksize);
  258.             } else {
  259.                 ctx.update(keyBytes);
  260.             }
  261.             cnt >>= 1;
  262.         }

  263.         // 12. finish digest A
  264.         /*
  265.          * Create intermediate result.
  266.          */
  267.         altResult = ctx.digest();

  268.         // 13. start digest DP
  269.         /*
  270.          * Start computation of P byte sequence.
  271.          */
  272.         altCtx = DigestUtils.getDigest(algorithm);

  273.         // 14. for every byte in the password (excluding the terminating NUL byte
  274.         // in the C representation of the string)
  275.         //
  276.         // add the password to digest DP
  277.         /*
  278.          * For every character in the password add the entire password.
  279.          */
  280.         for (int i = 1; i <= keyLen; i++) {
  281.             altCtx.update(keyBytes);
  282.         }

  283.         // 15. finish digest DP
  284.         /*
  285.          * Finish the digest.
  286.          */
  287.         byte[] tempResult = altCtx.digest();

  288.         // 16. produce byte sequence P of the same length as the password where
  289.         //
  290.         // a) for each block of 32 or 64 bytes of length of the password string
  291.         // the entire digest DP is used
  292.         //
  293.         // b) for the remaining N (up to 31 or 63) bytes use the first N
  294.         // bytes of digest DP
  295.         /*
  296.          * Create byte sequence P.
  297.          */
  298.         final byte[] pBytes = new byte[keyLen];
  299.         int cp = 0;
  300.         while (cp < keyLen - blocksize) {
  301.             System.arraycopy(tempResult, 0, pBytes, cp, blocksize);
  302.             cp += blocksize;
  303.         }
  304.         System.arraycopy(tempResult, 0, pBytes, cp, keyLen - cp);

  305.         // 17. start digest DS
  306.         /*
  307.          * Start computation of S byte sequence.
  308.          */
  309.         altCtx = DigestUtils.getDigest(algorithm);

  310.         // 18. repeat the following 16+A[0] times, where A[0] represents the first
  311.         // byte in digest A interpreted as an 8-bit unsigned value
  312.         //
  313.         // add the salt to digest DS
  314.         /*
  315.          * For every character in the password add the entire password.
  316.          */
  317.         for (int i = 1; i <= 16 + (altResult[0] & 0xff); i++) {
  318.             altCtx.update(saltBytes);
  319.         }

  320.         // 19. finish digest DS
  321.         /*
  322.          * Finish the digest.
  323.          */
  324.         tempResult = altCtx.digest();

  325.         // 20. produce byte sequence S of the same length as the salt string where
  326.         //
  327.         // a) for each block of 32 or 64 bytes of length of the salt string
  328.         // the entire digest DS is used
  329.         //
  330.         // b) for the remaining N (up to 31 or 63) bytes use the first N
  331.         // bytes of digest DS
  332.         /*
  333.          * Create byte sequence S.
  334.          */
  335.         // Remark: The salt is limited to 16 chars, how does this make sense?
  336.         final byte[] sBytes = new byte[saltLen];
  337.         cp = 0;
  338.         while (cp < saltLen - blocksize) {
  339.             System.arraycopy(tempResult, 0, sBytes, cp, blocksize);
  340.             cp += blocksize;
  341.         }
  342.         System.arraycopy(tempResult, 0, sBytes, cp, saltLen - cp);

  343.         // 21. repeat a loop according to the number specified in the rounds=<N>
  344.         // specification in the salt (or the default value if none is
  345.         // present). Each round is numbered, starting with 0 and up to N-1.
  346.         //
  347.         // The loop uses a digest as input. In the first round it is the
  348.         // digest produced in step 12. In the latter steps it is the digest
  349.         // produced in step 21.h. The following text uses the notation
  350.         // "digest A/C" to describe this behavior.
  351.         /*
  352.          * Repeatedly run the collected hash value through sha512 to burn CPU cycles.
  353.          */
  354.         for (int i = 0; i <= rounds - 1; i++) {
  355.             // a) start digest C
  356.             /*
  357.              * New context.
  358.              */
  359.             ctx = DigestUtils.getDigest(algorithm);

  360.             // b) for odd round numbers add the byte sequence P to digest C
  361.             // c) for even round numbers add digest A/C
  362.             /*
  363.              * Add key or last result.
  364.              */
  365.             if ((i & 1) != 0) {
  366.                 ctx.update(pBytes, 0, keyLen);
  367.             } else {
  368.                 ctx.update(altResult, 0, blocksize);
  369.             }

  370.             // d) for all round numbers not divisible by 3 add the byte sequence S
  371.             /*
  372.              * Add salt for numbers not divisible by 3.
  373.              */
  374.             if (i % 3 != 0) {
  375.                 ctx.update(sBytes, 0, saltLen);
  376.             }

  377.             // e) for all round numbers not divisible by 7 add the byte sequence P
  378.             /*
  379.              * Add key for numbers not divisible by 7.
  380.              */
  381.             if (i % 7 != 0) {
  382.                 ctx.update(pBytes, 0, keyLen);
  383.             }

  384.             // f) for odd round numbers add digest A/C
  385.             // g) for even round numbers add the byte sequence P
  386.             /*
  387.              * Add key or last result.
  388.              */
  389.             if ((i & 1) != 0) {
  390.                 ctx.update(altResult, 0, blocksize);
  391.             } else {
  392.                 ctx.update(pBytes, 0, keyLen);
  393.             }

  394.             // h) finish digest C.
  395.             /*
  396.              * Create intermediate result.
  397.              */
  398.             altResult = ctx.digest();
  399.         }

  400.         // 22. Produce the output string. This is an ASCII string of the maximum
  401.         // size specified above, consisting of multiple pieces:
  402.         //
  403.         // a) the salt salt_prefix, $5$ or $6$ respectively
  404.         //
  405.         // b) the rounds=<N> specification, if one was present in the input
  406.         // salt string. A trailing '$' is added in this case to separate
  407.         // the rounds specification from the following text.
  408.         //
  409.         // c) the salt string truncated to 16 characters
  410.         //
  411.         // d) a '$' character
  412.         /*
  413.          * Now we can construct the result string. It consists of three parts.
  414.          */
  415.         final StringBuilder buffer = new StringBuilder(saltPrefix);
  416.         if (roundsCustom) {
  417.             buffer.append(ROUNDS_PREFIX);
  418.             buffer.append(rounds);
  419.             buffer.append("$");
  420.         }
  421.         buffer.append(saltString);
  422.         buffer.append("$");

  423.         // e) the base-64 encoded final C digest. The encoding used is as
  424.         // follows:
  425.         // [...]
  426.         //
  427.         // Each group of three bytes from the digest produces four
  428.         // characters as output:
  429.         //
  430.         // 1. character: the six low bits of the first byte
  431.         // 2. character: the two high bits of the first byte and the
  432.         // four low bytes from the second byte
  433.         // 3. character: the four high bytes from the second byte and
  434.         // the two low bits from the third byte
  435.         // 4. character: the six high bits from the third byte
  436.         //
  437.         // The groups of three bytes are as follows (in this sequence).
  438.         // These are the indices into the byte array containing the
  439.         // digest, starting with index 0. For the last group there are
  440.         // not enough bytes left in the digest and the value zero is used
  441.         // in its place. This group also produces only three or two
  442.         // characters as output for SHA-512 and SHA-512 respectively.

  443.         // This was just a safeguard in the C implementation:
  444.         // int buflen = salt_prefix.length() - 1 + ROUNDS_PREFIX.length() + 9 + 1 + salt_string.length() + 1 + 86 + 1;

  445.         if (blocksize == 32) {
  446.             B64.b64from24bit(altResult[0], altResult[10], altResult[20], 4, buffer);
  447.             B64.b64from24bit(altResult[21], altResult[1], altResult[11], 4, buffer);
  448.             B64.b64from24bit(altResult[12], altResult[22], altResult[2], 4, buffer);
  449.             B64.b64from24bit(altResult[3], altResult[13], altResult[23], 4, buffer);
  450.             B64.b64from24bit(altResult[24], altResult[4], altResult[14], 4, buffer);
  451.             B64.b64from24bit(altResult[15], altResult[25], altResult[5], 4, buffer);
  452.             B64.b64from24bit(altResult[6], altResult[16], altResult[26], 4, buffer);
  453.             B64.b64from24bit(altResult[27], altResult[7], altResult[17], 4, buffer);
  454.             B64.b64from24bit(altResult[18], altResult[28], altResult[8], 4, buffer);
  455.             B64.b64from24bit(altResult[9], altResult[19], altResult[29], 4, buffer);
  456.             B64.b64from24bit((byte) 0, altResult[31], altResult[30], 3, buffer);
  457.         } else {
  458.             B64.b64from24bit(altResult[0], altResult[21], altResult[42], 4, buffer);
  459.             B64.b64from24bit(altResult[22], altResult[43], altResult[1], 4, buffer);
  460.             B64.b64from24bit(altResult[44], altResult[2], altResult[23], 4, buffer);
  461.             B64.b64from24bit(altResult[3], altResult[24], altResult[45], 4, buffer);
  462.             B64.b64from24bit(altResult[25], altResult[46], altResult[4], 4, buffer);
  463.             B64.b64from24bit(altResult[47], altResult[5], altResult[26], 4, buffer);
  464.             B64.b64from24bit(altResult[6], altResult[27], altResult[48], 4, buffer);
  465.             B64.b64from24bit(altResult[28], altResult[49], altResult[7], 4, buffer);
  466.             B64.b64from24bit(altResult[50], altResult[8], altResult[29], 4, buffer);
  467.             B64.b64from24bit(altResult[9], altResult[30], altResult[51], 4, buffer);
  468.             B64.b64from24bit(altResult[31], altResult[52], altResult[10], 4, buffer);
  469.             B64.b64from24bit(altResult[53], altResult[11], altResult[32], 4, buffer);
  470.             B64.b64from24bit(altResult[12], altResult[33], altResult[54], 4, buffer);
  471.             B64.b64from24bit(altResult[34], altResult[55], altResult[13], 4, buffer);
  472.             B64.b64from24bit(altResult[56], altResult[14], altResult[35], 4, buffer);
  473.             B64.b64from24bit(altResult[15], altResult[36], altResult[57], 4, buffer);
  474.             B64.b64from24bit(altResult[37], altResult[58], altResult[16], 4, buffer);
  475.             B64.b64from24bit(altResult[59], altResult[17], altResult[38], 4, buffer);
  476.             B64.b64from24bit(altResult[18], altResult[39], altResult[60], 4, buffer);
  477.             B64.b64from24bit(altResult[40], altResult[61], altResult[19], 4, buffer);
  478.             B64.b64from24bit(altResult[62], altResult[20], altResult[41], 4, buffer);
  479.             B64.b64from24bit((byte) 0, (byte) 0, altResult[63], 2, buffer);
  480.         }

  481.         /*
  482.          * Clear the buffer for the intermediate result so that people attaching to processes or reading core dumps
  483.          * cannot get any information.
  484.          */
  485.         // Is there a better way to do this with the JVM?
  486.         Arrays.fill(tempResult, (byte) 0);
  487.         Arrays.fill(pBytes, (byte) 0);
  488.         Arrays.fill(sBytes, (byte) 0);
  489.         ctx.reset();
  490.         altCtx.reset();
  491.         Arrays.fill(keyBytes, (byte) 0);
  492.         Arrays.fill(saltBytes, (byte) 0);

  493.         return buffer.toString();
  494.     }

  495.     /**
  496.      * Generates a libc crypt() compatible "$6$" hash value with random salt.
  497.      * <p>
  498.      * See {@link Crypt#crypt(String, String)} for details.
  499.      * </p>
  500.      * <p>
  501.      * A salt is generated for you using {@link SecureRandom}
  502.      * </p>
  503.      *
  504.      * @param keyBytes
  505.      *            plaintext to hash. Each array element is set to {@code 0} before returning.
  506.      * @return complete hash value
  507.      * @throws IllegalArgumentException
  508.      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
  509.      */
  510.     public static String sha512Crypt(final byte[] keyBytes) {
  511.         return sha512Crypt(keyBytes, null);
  512.     }

  513.     /**
  514.      * Generates a libc6 crypt() compatible "$6$" hash value.
  515.      * <p>
  516.      * See {@link Crypt#crypt(String, String)} for details.
  517.      * </p>
  518.      * @param keyBytes
  519.      *            plaintext to hash. Each array element is set to {@code 0} before returning.
  520.      * @param salt
  521.      *            real salt value without prefix or "rounds=". The salt may be null, in which case a salt is generated
  522.      *            for you using {@link SecureRandom}; if you want to use a {@link Random} object other than
  523.      *            {@link SecureRandom} then we suggest you provide it using
  524.      *            {@link #sha512Crypt(byte[], String, Random)}.
  525.      * @return complete hash value including salt
  526.      * @throws IllegalArgumentException
  527.      *             if the salt does not match the allowed pattern
  528.      * @throws IllegalArgumentException
  529.      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
  530.      */
  531.     public static String sha512Crypt(final byte[] keyBytes, String salt) {
  532.         if (salt == null) {
  533.             salt = SHA512_PREFIX + B64.getRandomSalt(8);
  534.         }
  535.         return sha2Crypt(keyBytes, salt, SHA512_PREFIX, SHA512_BLOCKSIZE, MessageDigestAlgorithms.SHA_512);
  536.     }

  537.     /**
  538.      * Generates a libc6 crypt() compatible "$6$" hash value.
  539.      * <p>
  540.      * See {@link Crypt#crypt(String, String)} for details.
  541.      * </p>
  542.      * @param keyBytes
  543.      *            plaintext to hash. Each array element is set to {@code 0} before returning.
  544.      * @param salt
  545.      *            real salt value without prefix or "rounds=". The salt may be null, in which case a salt
  546.      *            is generated for you using {@link SecureRandom}.
  547.      * @param random
  548.      *            the instance of {@link Random} to use for generating the salt.
  549.      *            Consider using {@link SecureRandom} for more secure salts.
  550.      * @return complete hash value including salt
  551.      * @throws IllegalArgumentException
  552.      *             if the salt does not match the allowed pattern
  553.      * @throws IllegalArgumentException
  554.      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
  555.      * @since 1.12
  556.      */
  557.     public static String sha512Crypt(final byte[] keyBytes, String salt, final Random random) {
  558.         if (salt == null) {
  559.             salt = SHA512_PREFIX + B64.getRandomSalt(8, random);
  560.         }
  561.         return sha2Crypt(keyBytes, salt, SHA512_PREFIX, SHA512_BLOCKSIZE, MessageDigestAlgorithms.SHA_512);
  562.     }

  563.     /**
  564.      * Consider private.
  565.      *
  566.      * @deprecated Will be private in the next major version.
  567.      */
  568.     @Deprecated
  569.     public Sha2Crypt() {
  570.         // empty
  571.     }
  572. }