Description
Calculate the SHA-256 hash for a given file efficiently.
License
Open Source License
Parameter
Parameter | Description |
---|
file | a parameter |
Exception
Parameter | Description |
---|
IOException | an exception |
Return
Byte array containing the hash
Declaration
public static byte[] sha256Hash(File file) throws IOException
Method Source Code
//package com.java2s;
/*//from ww w. jav a 2s.c o m
* --------------------------
* | Ring Machine 2 |
* | |
* | /---\ |
* | | | |
* | \---/ |
* | |
* | The Crowdsourced CDN |
* --------------------------
*
* Copyright (C) 2012 Eric Goodwin
*
* 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.io.*;
import java.security.*;
public class Main {
/**
* Calculate the SHA-256 hash for a given file efficiently.
*
* @param file
* @return Byte array containing the hash
* @throws IOException
*/
public static byte[] sha256Hash(File file) throws IOException {
FileInputStream in = new FileInputStream(file);
return sha256Hash(in);
}
public static byte[] sha256Hash(InputStream in) throws IOException {
MessageDigest sha256 = null;
try {
sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
// This should not happen, ever
return new byte[0];
}
DigestInputStream din = new DigestInputStream(in, sha256);
din.on(true);
byte[] buffer = new byte[8192];
while (din.read(buffer) != -1)
// Read everything into the digest input
;
return sha256.digest();
}
}
Related
- sha256Digest(final InputStream data)
- sha256Encode(String message)
- sha256encrypt(String phrase)
- sha256Hash()
- sha256Hash(byte[] data)
- sha256Hash(final String tohash)
- sha256HashHex(String data)
- SHA256Sum(final @Nonnull byte[] bytes)