Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.rabrg.thor.utility;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public final class FileHashUtility {
- private final MessageDigest digest;
- private FileHashUtility() throws NoSuchAlgorithmException {
- digest = MessageDigest.getInstance("SHA-256");
- }
- public byte[] hash(final String directory) throws IOException {
- try (final RandomAccessFile file = new RandomAccessFile(directory, "r")) {
- final long length = file.length();
- if (length != (int) length) {
- throw new IllegalArgumentException("Specified file is too large");
- }
- final byte[] data = new byte[(int) length];
- file.readFully(data);
- digest.update(data);
- final byte[] hash = digest.digest();
- digest.reset();
- return hash;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement