Java SHA256 sha256digest(@Nonnull byte[] data)

Here you can find the source of sha256digest(@Nonnull byte[] data)

Description

Simple utility to calculate the SHA-256 digest.

License

Apache License

Parameter

Parameter Description
data value to digest.

Exception

Parameter Description

Return

sha256-digest of data.

Declaration

@Nonnull
static byte[] sha256digest(@Nonnull byte[] data) 

Method Source Code

//package com.java2s;
/*/*  ww w .  j a va 2  s.  c o  m*/
 * Copyright 2014, 2015 Thomas Harning Jr. <harningt@gmail.com>
 *
 * 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.
 */

import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.annotation.Nonnull;

public class Main {
    /**
     * Simple utility to calculate the SHA-256 digest.
     *
     * @param data
     *         value to digest.
     *
     * @return sha256-digest of data.
     *
     * @throws java.lang.Error
     *         if the digest cannot be found (should not happen).
     */
    @Nonnull
    static byte[] sha256digest(@Nonnull byte[] data) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new Error(e);
        }
        return digest.digest(data);
    }

    /**
     * Simple utility to calculate the SHA-256 digest.
     *
     * @param data
     *         value to digest a portion of.
     * @param dataStart
     *         index into data for where to begin digest.
     * @param dataLength
     *         number of bytes to digest.
     * @param output
     *         array to write result into.
     * @param outputStart
     *         index into output to begin writing result.
     *
     * @throws java.lang.Error
     *         if the digest cannot be found or input is bad (should not happen).
     */
    @Nonnull
    static void sha256digest(@Nonnull byte[] data, int dataStart,
            int dataLength, @Nonnull byte[] output, int outputStart) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256");
            digest.update(data, dataStart, dataLength);
            digest.digest(output, outputStart, 256 / 8);
        } catch (GeneralSecurityException e) {
            throw new Error(e);
        }
    }
}

Related

  1. SHA256(String text)
  2. sha256(String text)
  3. SHA256(String texto)
  4. SHA256Binary(String toHash)
  5. SHA256byte(String input)
  6. sha256Digest(byte[] bytes)
  7. sha256Digest(final InputStream data)
  8. sha256Encode(String message)
  9. sha256encrypt(String phrase)