Skip to content

Commit

Permalink
[Fix] Replaced MD5 with SHA256
Browse files Browse the repository at this point in the history
  • Loading branch information
eitch committed Nov 30, 2023
1 parent 91a2f56 commit 792221e
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions utils/src/main/java/li/strolch/utils/helper/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package li.strolch.utils.helper;

import static java.util.Collections.emptySet;
import static li.strolch.utils.helper.StringHelper.isEmpty;
import static li.strolch.utils.helper.StringHelper.normalizeLength;
import static li.strolch.utils.helper.TempFileOptions.*;
import li.strolch.utils.DataUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
Expand All @@ -30,9 +29,10 @@
import java.time.format.DateTimeFormatter;
import java.util.Set;

import li.strolch.utils.DataUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.util.Collections.emptySet;
import static li.strolch.utils.helper.StringHelper.isEmpty;
import static li.strolch.utils.helper.StringHelper.normalizeLength;
import static li.strolch.utils.helper.TempFileOptions.*;

/**
* Helper class for dealing with files
Expand Down Expand Up @@ -279,12 +279,12 @@ public static boolean copy(File fromFile, File toFile, boolean checksum) {
outBuffer.flush();

if (checksum) {
String fromFileMD5 = StringHelper.toHexString(FileHelper.hashFileMd5(fromFile));
String toFileMD5 = StringHelper.toHexString(FileHelper.hashFileMd5(toFile));
if (!fromFileMD5.equals(toFileMD5)) {
String fromFileSha256 = StringHelper.toHexString(FileHelper.hashFileSha256(fromFile));
String toFileSha256 = StringHelper.toHexString(FileHelper.hashFileSha256(toFile));
if (!fromFileSha256.equals(toFileSha256)) {
FileHelper.logger.error(
MessageFormat.format("Copying failed, as MD5 sums are not equal: {0} / {1}", fromFileMD5,
toFileMD5));
MessageFormat.format("Copying failed, as MD5 sums are not equal: {0} / {1}", fromFileSha256,
toFileSha256));
if (!toFile.delete())
throw new IllegalStateException("Failed to delete file " + toFile);

Expand Down Expand Up @@ -362,18 +362,6 @@ public static String humanizeFileSize(long fileSize) {
return DataUnit.Bytes.humanizeBytesValue(fileSize);
}

/**
* Creates the MD5 hash of the given file, returning the hash as a byte array. Use
* {@link StringHelper#toHexString(byte[])} to create a HEX string of the bytes
*
* @param file the file to hash
*
* @return the hash as a byte array
*/
public static byte[] hashFileMd5(File file) {
return FileHelper.hashFile(file, "MD5");
}

/**
* Creates the SHA256 hash of the given file, returning the hash as a byte array. Use
* {@link StringHelper#toHexString(byte[])} to create a HEX string of the bytes
Expand Down

0 comments on commit 792221e

Please sign in to comment.