Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INLONG-9987][Agent] Fix the issue of deleting the first digit when the first digit in MD5 is 0 #9988

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading