Skip to content

Commit

Permalink
Use the JRE for hashing, not guava (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 1, 2020
1 parent dd13fea commit a7b4e46
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
1 change: 0 additions & 1 deletion private/rules/maven_project_jar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ single artifact that other teams can download and use.
executable = True,
cfg = "host",
default = "//private/tools/java/rules/jvm/external/jar:MergeJars",
allow_files = True,
),
"_java_toolchain": attr.label(
default = "@bazel_tools//tools/jdk:current_java_toolchain",
Expand Down
1 change: 1 addition & 0 deletions private/tools/java/rules/jvm/external/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ java_library(
srcs = ["ByteStreams.java"],
visibility = [
"//private/tools/java/rules/jvm/external:__subpackages__",
"//tests/com/jvm/external:__subpackages__",
]
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package rules.jvm.external.jar;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import rules.jvm.external.ByteStreams;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
Expand All @@ -34,12 +33,10 @@ public void resolve(Path current, InputStream inputStreamForDuplicate) {
IS_ERROR("are-errors") {
@Override
public void resolve(Path current, InputStream inputStreamForDuplicate) throws IOException {
HashFunction hashFunction = Hashing.goodFastHash(64);
byte[] first = hash(Files.readAllBytes(current));
byte[] second = hash(ByteStreams.toByteArray(inputStreamForDuplicate));

HashCode first = hashFunction.hashBytes(Files.readAllBytes(current));
HashCode second = hashFunction.hashBytes(ByteStreams.toByteArray(inputStreamForDuplicate));

if (first.equals(second)) {
if (Arrays.equals(first, second)) {
return;
}
throw new IOException("Attempt to write different duplicate file for: " + current);
Expand Down Expand Up @@ -69,4 +66,14 @@ public String toString() {
}

public abstract void resolve(Path current, InputStream inputStreamForDuplicate) throws IOException;

protected byte[] hash(byte[] bytes) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(bytes);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
1 change: 1 addition & 0 deletions tests/com/jvm/external/jar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ java_test(
test_class = "com.jvm.external.jar.MergeJarsTest",
srcs = ["MergeJarsTest.java"],
deps = [
"//private/tools/java/rules/jvm/external:byte-streams",
"//private/tools/java/rules/jvm/external/jar:MergeJars",
"//private/tools/java/rules/jvm/external/zip",
artifact("com.google.guava:guava"),
Expand Down

0 comments on commit a7b4e46

Please sign in to comment.