Skip to content

Commit

Permalink
[INLONG-9987][Agent] Fix the issue of deleting the first digit when t…
Browse files Browse the repository at this point in the history
…he first digit in MD5 is 0 (#9988)
  • Loading branch information
justinwwhuang authored Apr 15, 2024
1 parent 0fee4a1 commit 5b559ee
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -46,7 +47,6 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -445,9 +445,11 @@ private boolean downloadModule(ModuleConfig module) {

private boolean isPackageDownloaded(ModuleConfig module) {
String path = module.getPackageConfig().getStoragePath() + "/" + module.getPackageConfig().getFileName();
if (calcFileMd5(path).equals(module.getPackageConfig().getMd5())) {
String fileMd5 = calcFileMd5(path);
if (fileMd5.equals(module.getPackageConfig().getMd5())) {
return true;
} else {
LOGGER.error("md5 not match! fileMd5 {} moduleMd5 {}", fileMd5, module.getPackageConfig().getMd5());
return false;
}
}
Expand All @@ -474,23 +476,20 @@ public void stop() throws Exception {
}

private static String calcFileMd5(String path) {
BigInteger bi = null;
byte[] buffer = new byte[DOWNLOAD_PACKAGE_READ_BUFF_SIZE];
int len = 0;
try (FileInputStream fileInputStream = new FileInputStream(path)) {
MessageDigest md = MessageDigest.getInstance("MD5");
while ((len = fileInputStream.read(buffer)) != -1) {
md.update(buffer, 0, len);
}
byte[] b = md.digest();
bi = new BigInteger(1, b);
return new String(Hex.encodeHex(md.digest()));
} catch (NoSuchAlgorithmException e) {
LOGGER.error("calc file md5 NoSuchAlgorithmException", e);
return "";
} catch (IOException e) {
LOGGER.error("calc file md5 IOException", e);
return "";
}
return bi.toString(16);
}
}

0 comments on commit 5b559ee

Please sign in to comment.