Java SHA256 SHA256(ByteBuffer buf, int off, int length)

Here you can find the source of SHA256(ByteBuffer buf, int off, int length)

Description

SHA

License

Open Source License

Declaration

public static byte[] SHA256(ByteBuffer buf, int off, int length) 

Method Source Code


//package com.java2s;
/*/*from   ww  w.j a  va 2 s  . c o m*/
 * This file is part of BCNode.
 *
 * Copyright (c) Raphfrk 2013 <www.raphfrk.com/bcnode>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class Main {
    private static final ThreadLocal<MessageDigest> localSHA256 = new ThreadLocal<MessageDigest>() {
        @Override
        protected MessageDigest initialValue() {
            try {
                return MessageDigest.getInstance("SHA-256", "BC");
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalStateException("Bouncy castle should support SHA-256", e);
            } catch (NoSuchProviderException e) {
                throw new IllegalStateException("Bouncy castle should be registered as a provider", e);
            }
        }
    };

    public static byte[] SHA256(ByteBuffer buf, int off, int length) {
        int limit = buf.limit();
        int position = buf.position();
        if (off + length > limit) {
            return null;
        }
        buf.position(off);
        buf.limit(off + length);
        try {
            return SHA256(buf, 1);
        } finally {
            buf.limit(limit);
            buf.position(position);
        }
    }

    public static byte[] SHA256(ByteBuffer buf, int n) {
        MessageDigest d = localSHA256.get();
        d.reset();
        d.update(buf);
        byte[] message = d.digest();
        for (int i = 1; i < n; i++) {
            d.reset();
            message = d.digest(message);
        }
        return message;
    }
}

Related

  1. sha256(byte[] data)
  2. sha256(byte[] data)
  3. SHA256(byte[] input)
  4. SHA256(byte[] P)
  5. SHA256(byte[] src)
  6. sha256(final InputStream inputStream)
  7. sha256(final String string)
  8. sha256(InputStream data)
  9. sha256(String base)