Skip to content

Commit

Permalink
only check hash when locally extracting. and include version num
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jul 13, 2024
1 parent a74658c commit dc889e5
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,32 @@ public File findJavaApi() {
}
synchronized (Flags.class) {
Constants.DIR.mkdirs();
Path tmp = Constants.DIR.toPath().resolve("jvmdg-api.jar");
Path tmp = Constants.DIR.toPath().resolve("jvmdg-api- " + jvmdgVersion + " .jar");
File prop = getJavaApiFromSystemProperty();
if (prop != null) {
return prop;
}
URL url = getJavaApiFromShade();
if (Files.exists(tmp)) {
if (url == null) {
foundApi = tmp.toFile();
return tmp.toFile();
} else {
try (InputStream in = url.openStream()) {
try (InputStream in2 = Files.newInputStream(tmp)) {
if (hash(in).equals(hash(in2))) {
foundApi = tmp.toFile();
return tmp.toFile();
}
}
}

}
}
if (url == null && allowMaven) {
url = getJavaApiFromMaven();
}
if (url != null) {
// if (Files.exists(tmp)) {
// try (InputStream in = url.openStream()) {
// try (InputStream in2 = Files.newInputStream(tmp)) {
// if (hash(in).equals(hash(in2))) {
// foundApi = tmp.toFile();
// return tmp.toFile();
// }
// }
// }
// }
try (InputStream in = url.openStream()) {
Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
foundApi = tmp.toFile();
Expand All @@ -160,23 +166,24 @@ public File findJavaApi() {
}
}

// private String hash(InputStream is) {
// MessageDigest digest = null;
// try {
// digest = MessageDigest.getInstance("SHA-1");
// byte[] buffer = new byte[8192];
// int read = 0;
// while ((read = is.read(buffer)) > 0) {
// digest.update(buffer, 0, read);
// }
// byte[] hash = digest.digest();
// StringBuilder sb = new StringBuilder();
// for (byte b : hash) {
// sb.append(String.format("%02x", b));
// }
// return sb.toString();
// } catch (Exception e) {
// throw new RuntimeException("Failed to hash", e);
// }
// }
private String hash(InputStream is) {
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-1");
byte[] buffer = new byte[8192];
int read = 0;
while ((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
byte[] hash = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (Exception e) {
throw new RuntimeException("Failed to hash", e);
}
}

}

0 comments on commit dc889e5

Please sign in to comment.