Skip to content

Commit

Permalink
Address review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
udda1996 committed May 18, 2023
1 parent 84f1854 commit 24409cf
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,9 @@ private static void downloadAndSetupDist(PrintStream printStream, HttpURLConnect
String distPath = getDistributionsPath();
String zipFileLocation = getDistributionsPath() + File.separator + distribution + ".zip";
downloadFile(conn, zipFileLocation, distribution, printStream);
String downloadedDistSha = calcDistSHA(zipFileLocation);
String remoteDistSha = getDistSHA(distribution);
if (downloadedDistSha.equals(remoteDistSha)) {
String downloadedDistHash = generateDistributionHash(zipFileLocation);
String remoteDistHash = getDistHash(distribution);
if (downloadedDistHash.equals(remoteDistHash)) {
unzip(zipFileLocation, distPath);
addExecutablePermissionToFile(new File(distPath + File.separator
+ ToolUtil.getType(distribution) + "-" + distribution + File.separator + "bin"
Expand Down Expand Up @@ -506,7 +506,7 @@ private static void downloadAndSetupDist(PrintStream printStream, HttpURLConnect
}
}

private static String getDistSHA(String distribution) {
private static String getDistHash(String distribution) {
HttpURLConnection conn = null;
String hash = "";
try {
Expand Down Expand Up @@ -553,16 +553,17 @@ private static String getDistSHA(String distribution) {
return hash;
}

private static String calcDistSHA(String zipFileLocation) throws NoSuchAlgorithmException, IOException {
private static String generateDistributionHash(String zipFileLocation) throws NoSuchAlgorithmException, IOException {
File zipFilePath = new File(zipFileLocation);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
InputStream inputStream = new FileInputStream(zipFilePath);
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
n = inputStream.read(buffer);
if (n > 0) {
digest.update(buffer, 0, n);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (InputStream inputStream = new FileInputStream(zipFilePath)) {
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
n = inputStream.read(buffer);
if (n > 0) {
digest.update(buffer, 0, n);
}
}
}
StringBuilder result = new StringBuilder();
Expand Down

0 comments on commit 24409cf

Please sign in to comment.