/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.common.hash;

import org.elasticsearch.ElasticsearchException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

public class MessageDigests {

    private static ThreadLocal<MessageDigest> createThreadLocalMessageDigest(String digest) {
        return ThreadLocal.withInitial(() -> {
            try {
                return MessageDigest.getInstance(digest);
            } catch (NoSuchAlgorithmException e) {
                throw new ElasticsearchException("unexpected exception creating MessageDigest instance for [" + digest + "]", e);
            }
        });
    }

    private static final ThreadLocal<MessageDigest> MD5_DIGEST = createThreadLocalMessageDigest("MD5");
    private static final ThreadLocal<MessageDigest> SHA_1_DIGEST = createThreadLocalMessageDigest("SHA-1");
    private static final ThreadLocal<MessageDigest> SHA_256_DIGEST = createThreadLocalMessageDigest("SHA-256");

    public static MessageDigest md5() {
        return get(MD5_DIGEST);
    }

    public static MessageDigest sha1() {
        return get(SHA_1_DIGEST);
    }

    public static MessageDigest sha256() {
        return get(SHA_256_DIGEST);
    }

    private static MessageDigest get(ThreadLocal<MessageDigest> messageDigest) {
        MessageDigest instance = messageDigest.get();
        instance.reset();
        return instance;
    }

    private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();

    public static String toHexString(byte[] bytes) {
        Objects.requireNonNull(bytes);
        StringBuilder sb = new StringBuilder(2 * bytes.length);

        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            sb.append(HEX_DIGITS[b >> 4 & 0xf]).append(HEX_DIGITS[b & 0xf]);
        }

        return sb.toString();
    }

}

@@ -983,22 +983,204 @@
 jects;%0A%0A
-public
+/**%0A * This MessageDigests class provides convenience methods for obtaining%0A * thread local %7B@link MessageDigest%7D instances for MD5, SHA-1, and%0A * SHA-256 message digests.%0A */%0Apublic final
  class M
@@ -1490,29 +1490,28 @@
 row new 
-Elasticsearch
+IllegalState
 Exceptio
@@ -1959,164 +1959,1094 @@
     
-public static MessageDigest md5() %7B%0A        return get(MD5_DIGEST);%0A    %7D%0A%0A    public static MessageDigest sha1() %7B%0A        return get(SHA_1_DIGEST);%0A    %7D%0A
+/**%0A     * Returns a %7B@link MessageDigest%7D instance for MD5 digests; note%0A     * that the instance returned is thread local and must not be%0A     * shared amongst threads.%0A     *%0A     * @return a thread local %7B@link MessageDigest%7D instance that%0A     * provides MD5 message digest functionality.%0A     */%0A    public static MessageDigest md5() %7B%0A        return get(MD5_DIGEST);%0A    %7D%0A%0A    /**%0A     * Returns a %7B@link MessageDigest%7D instance for SHA-1 digests; note%0A     * that the instance returned is thread local and must not be%0A     * shared amongst threads.%0A     *%0A     * @return a thread local %7B@link MessageDigest%7D instance that%0A     * provides SHA-1 message digest functionality.%0A     */%0A    public static MessageDigest sha1() %7B%0A        return get(SHA_1_DIGEST);%0A    %7D%0A%0A    /**%0A     * Returns a %7B@link MessageDigest%7D instance for SHA-256 digests;%0A     * note that the instance returned is thread local and must not be%0A     * shared amongst threads.%0A     *%0A     * @return a thread local %7B@link MessageDigest%7D instance that%0A     * provides SHA-256 message digest functionality.%0A     */
 %0A   
@@ -3397,24 +3397,209 @@
 arArray();%0A%0A
+    /**%0A     * Format a byte array as a hex string.%0A     *%0A     * @param bytes the input to be represented as hex.%0A     * @return a hex representation of the input as a String.%0A     */%0A
     public s

Add Javadocs for o.e.c.h.MessageDigests