Remove some uses of MD5.
RELNOTES: None.
PiperOrigin-RevId: 274029065
diff --git a/src/main/java/com/google/devtools/build/lib/unix/NativePosixFiles.java b/src/main/java/com/google/devtools/build/lib/unix/NativePosixFiles.java
index 3647f11..3442349 100644
--- a/src/main/java/com/google/devtools/build/lib/unix/NativePosixFiles.java
+++ b/src/main/java/com/google/devtools/build/lib/unix/NativePosixFiles.java
@@ -16,7 +16,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.flogger.GoogleLogger;
-import com.google.common.hash.HashCode;
import com.google.devtools.build.lib.UnixJniLoader;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -348,26 +347,6 @@
throws IOException;
/**
- * Returns the MD5 digest of the specified file, following symbolic links.
- *
- * @param path the file whose MD5 digest is required.
- * @return the MD5 digest, as a 16-byte array.
- * @throws IOException if the call failed for any reason.
- */
- static native byte[] md5sumAsBytes(String path) throws IOException;
-
- /**
- * Returns the MD5 digest of the specified file, following symbolic links.
- *
- * @param path the file whose MD5 digest is required.
- * @return the MD5 digest, as a {@link HashCode}
- * @throws IOException if the call failed for any reason.
- */
- public static HashCode md5sum(String path) throws IOException {
- return HashCode.fromBytes(md5sumAsBytes(path));
- }
-
- /**
* Deletes all directory trees recursively beneath the given path, which is expected to be a
* directory. Does not remove the top directory.
*
diff --git a/src/main/java/com/google/devtools/build/lib/unix/UnixFileSystem.java b/src/main/java/com/google/devtools/build/lib/unix/UnixFileSystem.java
index 0fca64c..bfc0133 100644
--- a/src/main/java/com/google/devtools/build/lib/unix/UnixFileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/unix/UnixFileSystem.java
@@ -408,9 +408,6 @@
String name = path.toString();
long startTime = Profiler.nanoTimeMaybe();
try {
- if (getDigestFunction() == DigestHashFunction.MD5) {
- return NativePosixFiles.md5sum(name).asBytes();
- }
return super.getDigest(path);
} finally {
profiler.logSimpleTask(startTime, ProfilerTask.VFS_MD5, name);
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/Root.java b/src/main/java/com/google/devtools/build/lib/vfs/Root.java
index 8d4311e..8d7f16d 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/Root.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/Root.java
@@ -90,7 +90,7 @@
this.fingerprint =
new BigInteger(
1,
- DigestHashFunction.MD5
+ DigestHashFunction.SHA256
.cloneOrCreateMessageDigest()
.digest(path.getPathString().getBytes(StandardCharsets.UTF_8)));
}
diff --git a/src/main/native/unix_jni.cc b/src/main/native/unix_jni.cc
index d5df2f8..21d9f42 100644
--- a/src/main/native/unix_jni.cc
+++ b/src/main/native/unix_jni.cc
@@ -33,7 +33,6 @@
#include <string>
#include <vector>
-#include "src/main/cpp/util/md5.h"
#include "src/main/cpp/util/port.h"
#include "src/main/native/latin1_jni_path.h"
#include "src/main/native/macros.h"
@@ -44,8 +43,6 @@
#define PORTABLE_O_DIRECTORY 0
#endif
-using blaze_util::Md5Digest;
-
// See unix_jni.h.
void PostException(JNIEnv *env, int error_number, const std::string& message) {
// Keep consistent with package-info.html!
@@ -1076,59 +1073,6 @@
free(buf);
}
-// Computes MD5 digest of "file", writes result in "result", which
-// must be of length Md5Digest::kDigestLength. Returns zero on success, or
-// -1 (and sets errno) otherwise.
-static int md5sumAsBytes(const char *file,
- jbyte result[Md5Digest::kDigestLength]) {
- Md5Digest digest;
- // OPT: Using a 32k buffer would give marginally better performance,
- // but what is the stack size here?
- jbyte buf[8192];
- int fd;
- while ((fd = open(file, O_RDONLY)) == -1 && errno == EINTR) { }
- if (fd == -1) {
- return -1;
- }
- for (ssize_t len = read(fd, buf, arraysize(buf));
- len != 0;
- len = read(fd, buf, arraysize(buf))) {
- if (len == -1) {
- if (errno == EINTR) {
- continue;
- } else {
- int read_errno = errno;
- close(fd); // prefer read() errors over close().
- errno = read_errno;
- return -1;
- }
- }
- digest.Update(buf, len);
- }
- if (close(fd) < 0 && errno != EINTR) {
- return -1;
- }
- digest.Finish(reinterpret_cast<unsigned char*>(result));
- return 0;
-}
-
-
-extern "C" JNIEXPORT jbyteArray JNICALL
-Java_com_google_devtools_build_lib_unix_NativePosixFiles_md5sumAsBytes(
- JNIEnv *env, jclass clazz, jstring path) {
- const char *path_chars = GetStringLatin1Chars(env, path);
- jbyte value[Md5Digest::kDigestLength];
- jbyteArray result = NULL;
- if (md5sumAsBytes(path_chars, value) == 0) {
- result = env->NewByteArray(Md5Digest::kDigestLength);
- env->SetByteArrayRegion(result, 0, Md5Digest::kDigestLength, value);
- } else {
- ::PostFileException(env, errno, path_chars);
- }
- ReleaseStringLatin1Chars(path_chars);
- return result;
-}
-
extern "C" JNIEXPORT jlong JNICALL
Java_com_google_devtools_build_lib_unix_NativePosixSystem_sysctlbynameGetLong(
JNIEnv *env, jclass clazz, jstring name) {
diff --git a/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java b/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java
index dc870a4..c74c41c 100644
--- a/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java
@@ -110,7 +110,8 @@
public void testCalculationConcurrency() throws Exception {
final int small = DigestUtils.MULTI_THREADED_DIGEST_MAX_FILE_SIZE;
final int large = DigestUtils.MULTI_THREADED_DIGEST_MAX_FILE_SIZE + 1;
- for (DigestHashFunction hf : Arrays.asList(DigestHashFunction.MD5, DigestHashFunction.SHA1)) {
+ for (DigestHashFunction hf :
+ Arrays.asList(DigestHashFunction.SHA256, DigestHashFunction.SHA1)) {
assertDigestCalculationConcurrency(true, true, small, small, hf);
assertDigestCalculationConcurrency(true, true, large, large, hf);
assertDigestCalculationConcurrency(true, false, small, small, hf);
diff --git a/src/test/java/com/google/devtools/build/lib/collect/nestedset/DigestMapTest.java b/src/test/java/com/google/devtools/build/lib/collect/nestedset/DigestMapTest.java
index 67d9b86..b7e31ea 100644
--- a/src/test/java/com/google/devtools/build/lib/collect/nestedset/DigestMapTest.java
+++ b/src/test/java/com/google/devtools/build/lib/collect/nestedset/DigestMapTest.java
@@ -36,7 +36,7 @@
@Parameters(name = "Hash: {0}")
public static Iterable<Object[]> hashFunction() {
return ImmutableList.of(
- new Object[] {DigestHashFunction.MD5}, new Object[] {DigestHashFunction.SHA256});
+ new Object[] {DigestHashFunction.SHA1}, new Object[] {DigestHashFunction.SHA256});
}
@Parameter public DigestHashFunction digestHashFunction;
diff --git a/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java b/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java
index 311864d..8b32295 100644
--- a/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java
+++ b/src/test/java/com/google/devtools/build/lib/unix/NativePosixFilesTest.java
@@ -19,14 +19,10 @@
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.hash.HashCode;
import com.google.devtools.build.lib.testutil.TestUtils;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
-import com.google.devtools.build.lib.vfs.FileAccessException;
import com.google.devtools.build.lib.vfs.FileSystem;
-import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import java.io.File;
import java.io.FileNotFoundException;
@@ -51,50 +47,11 @@
testFile = workingDir.getRelative("test");
}
- /**
- * This test validates that the md5sum() method returns hashes that match the official test
- * vectors specified in RFC 1321, The MD5 Message-Digest Algorithm.
- *
- * @throws Exception
- */
- @Test
- public void testValidateMd5Sum() throws Exception {
- ImmutableMap<String, String> testVectors = ImmutableMap.<String, String>builder()
- .put("", "d41d8cd98f00b204e9800998ecf8427e")
- .put("a", "0cc175b9c0f1b6a831c399e269772661")
- .put("abc", "900150983cd24fb0d6963f7d28e17f72")
- .put("message digest", "f96b697d7cb7938d525a2f31aaf161d0")
- .put("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b")
- .put("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
- "d174ab98d277d9f5a5611c2c9f419d9f")
- .put(
- "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
- "57edf4a22be3c955ac49da2e2107b67a")
- .build();
-
- for (String testInput : testVectors.keySet()) {
- FileSystemUtils.writeContentAsLatin1(testFile, testInput);
- HashCode result = NativePosixFiles.md5sum(testFile.getPathString());
- assertThat(testVectors).containsEntry(testInput, result.toString());
- }
- }
-
- @Test
- public void throwsFileAccessException() throws Exception {
- FileSystemUtils.createEmptyFile(testFile);
- NativePosixFiles.chmod(testFile.getPathString(), 0200);
-
- FileAccessException e =
- assertThrows(
- FileAccessException.class, () -> NativePosixFiles.md5sum(testFile.getPathString()));
- assertThat(e).hasMessageThat().isEqualTo(testFile + " (Permission denied)");
- }
-
@Test
public void throwsFileNotFoundException() throws Exception {
FileNotFoundException e =
assertThrows(
- FileNotFoundException.class, () -> NativePosixFiles.md5sum(testFile.getPathString()));
+ FileNotFoundException.class, () -> NativePosixFiles.stat(testFile.getPathString()));
assertThat(e).hasMessageThat().isEqualTo(testFile + " (No such file or directory)");
}
diff --git a/src/test/java/com/google/devtools/build/lib/vfs/DigestHashFunctionGlobalsTest.java b/src/test/java/com/google/devtools/build/lib/vfs/DigestHashFunctionGlobalsTest.java
index 3a55fc8..0d4881c 100644
--- a/src/test/java/com/google/devtools/build/lib/vfs/DigestHashFunctionGlobalsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/vfs/DigestHashFunctionGlobalsTest.java
@@ -55,9 +55,6 @@
assertThat(converter.convert("sha-1")).isSameInstanceAs(DigestHashFunction.SHA1);
assertThat(converter.convert("SHA1")).isSameInstanceAs(DigestHashFunction.SHA1);
assertThat(converter.convert("sha1")).isSameInstanceAs(DigestHashFunction.SHA1);
-
- assertThat(converter.convert("MD5")).isSameInstanceAs(DigestHashFunction.MD5);
- assertThat(converter.convert("md5")).isSameInstanceAs(DigestHashFunction.MD5);
}
@Test
@@ -97,7 +94,7 @@
@Test
public void cannotSetDefaultMultipleTimes() throws Exception {
- DigestHashFunction.setDefault(DigestHashFunction.MD5);
+ DigestHashFunction.setDefault(DigestHashFunction.SHA256);
assertThrows(
DigestHashFunction.DefaultAlreadySetException.class,
() -> DigestHashFunction.setDefault(DigestHashFunction.SHA1));
diff --git a/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java
index 09656ff..a7215fe 100644
--- a/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java
+++ b/src/tools/package_printer/java/com/google/devtools/build/packageprinter/BazelPackagePrinter.java
@@ -42,7 +42,7 @@
+ "package/to/print");
System.exit(1);
}
- FileSystem fileSystem = new JavaIoFileSystem(DigestHashFunction.MD5);
+ FileSystem fileSystem = new JavaIoFileSystem(DigestHashFunction.SHA256);
PackageLoader loader =
newPackageLoader(
Root.fromPath(fileSystem.getPath(getAbsPathFlag(args[0], "--workspace_root="))),