diff --git a/src/main/java/com/github/jlangch/venice/util/crypt/FileEncryptor.java b/src/main/java/com/github/jlangch/venice/util/crypt/FileEncryptor.java index 2d52dd4d9..e0800d071 100644 --- a/src/main/java/com/github/jlangch/venice/util/crypt/FileEncryptor.java +++ b/src/main/java/com/github/jlangch/venice/util/crypt/FileEncryptor.java @@ -51,7 +51,7 @@ * +-----------------------+ * | encrypted file data | n bytes * +-----------------------+ - *
+ * 
*/ public class FileEncryptor { diff --git a/src/main/java/com/github/jlangch/venice/util/crypt/FileHasher.java b/src/main/java/com/github/jlangch/venice/util/crypt/FileHasher.java index 4f907c9de..ba51bfc7e 100644 --- a/src/main/java/com/github/jlangch/venice/util/crypt/FileHasher.java +++ b/src/main/java/com/github/jlangch/venice/util/crypt/FileHasher.java @@ -31,27 +31,51 @@ * Computes hashes from files and verifies file hashes to detect modified * files. * - * Uses fast MD5 hashes to detect file changes. + * Supported hash algorithms: + * + * + * MD5 is the fastest hash algorithm and precise enough to detect file + * changes. */ public class FileHasher { public static String hashFile( final File inputFile, final String salt + ) throws Exception { + return hashFile(inputFile, salt, "MD5"); + } + + public static String hashFile( + final File inputFile, + final String salt, + final String algorithm ) throws Exception { // Read file data byte[] fileData = Files.readAllBytes(inputFile.toPath()); // Hash - return hashFile(fileData, salt); + return hashFile(fileData, salt, algorithm); } public static String hashFile( final byte[] fileData, final String salt + ) throws Exception { + return hashFile(fileData, salt, "MD5"); + } + + public static String hashFile( + final byte[] fileData, + final String salt, + final String algorithm ) throws Exception { // Init digest - MessageDigest md = MessageDigest.getInstance("MD5"); + MessageDigest md = MessageDigest.getInstance(algorithm); md.reset(); // Supply data @@ -66,21 +90,39 @@ public static boolean verifyFileHash( final File inputFile, final String salt, final String hash + ) throws Exception { + return verifyFileHash(inputFile, salt, hash, "MD5"); + } + + public static boolean verifyFileHash( + final File inputFile, + final String salt, + final String hash, + final String algorithm ) throws Exception { // Read file data byte[] fileData = Files.readAllBytes(inputFile.toPath()); // Verify hash - return verifyFileHash(fileData, salt, hash); + return verifyFileHash(fileData, salt, hash, algorithm); } public static boolean verifyFileHash( final byte[] fileData, final String salt, final String hash + ) throws Exception { + return verifyFileHash(fileData, salt, hash, "MD5"); + } + + public static boolean verifyFileHash( + final byte[] fileData, + final String salt, + final String hash, + final String algorithm ) throws Exception { // Hash file data - String fileDataHash = hashFile(fileData, salt); + String fileDataHash = hashFile(fileData, salt, algorithm); // Verify digest return hash.equals(fileDataHash); @@ -95,4 +137,5 @@ public static String encodeBase64(final byte[] data) { public static byte[] decodeBase64(final String data) { return Base64.getDecoder().decode(data); } + }