Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
juerg committed Dec 6, 2023
1 parent 1bf8dfc commit e036c32
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* +-----------------------+
* | encrypted file data | n bytes
* +-----------------------+
* <pre/>
* </pre>
*/
public class FileEncryptor {

Expand Down
53 changes: 48 additions & 5 deletions src/main/java/com/github/jlangch/venice/util/crypt/FileHasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* <ul>
* <li>MD5</li>
* <li>SHA-1</li>
* <li>SHA-512</li>
* </ul>
*
* 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
Expand All @@ -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);
Expand All @@ -95,4 +137,5 @@ public static String encodeBase64(final byte[] data) {
public static byte[] decodeBase64(final String data) {
return Base64.getDecoder().decode(data);
}

}

0 comments on commit e036c32

Please sign in to comment.