Here you can find the source of sha256digest(@Nonnull byte[] data)
Parameter | Description |
---|---|
data | value to digest. |
Parameter | Description |
---|
@Nonnull static byte[] sha256digest(@Nonnull byte[] data)
//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); } } }