Skip to content

chore: replace md5 with sha-256 for file data validation #2199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ private static void assertStreamEqualStream(
final byte[] expectedDigest;
final byte[] actualDigest;
try {
expectedDigest = calculateMD5Digest(expectedInputStream);
actualDigest = calculateMD5Digest(actualInputStream);
expectedDigest = calculateDigest(expectedInputStream);
actualDigest = calculateDigest(actualInputStream);
Assert.assertArrayEquals(expectedDigest, actualDigest);
} finally {
expectedInputStream.close();
Expand All @@ -83,24 +83,24 @@ private static void assertStreamEqualStream(
}

@SuppressWarnings("MagicNumber") // Buffer size
private static byte[] calculateMD5Digest(InputStream stream) {
private static byte[] calculateDigest(InputStream stream) {
final byte[] buffer = new byte[2_048];
final MessageDigest md5;
final MessageDigest digest;
try {
md5 = MessageDigest.getInstance("MD5");
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException noMd5AvailableError) {
throw new RuntimeException("No MD5 algorithm available to use for hashing.", noMd5AvailableError);
}

int bytesRead;
try {
while ((bytesRead = stream.read(buffer)) != -1) {
md5.update(buffer, 0, bytesRead);
digest.update(buffer, 0, bytesRead);
}
} catch (IOException readError) {
throw new RuntimeException("Failed to read input stream.", readError);
}

return md5.digest();
return digest.digest();
}
}