Skip to content

Commit

Permalink
Refactor HashGenerator to support default hashing implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Dec 24, 2024
1 parent 8f92a70 commit a9f037d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import lombok.Getter;
import lombok.extern.java.Log;

import com.navercorp.scavenger.util.HashGenerator;
import com.navercorp.scavenger.util.HashGenerator.DefaultHash;

@Log
public class MethodRegistry {
Expand All @@ -28,7 +28,7 @@ private String generateHash(String byteBuddySignature) {
if (isLegacyCompatibilityMode) {
signature = signature.replace('$', '.').replace(",", ", ");
}
return HashGenerator.Md5.from(signature);
return DefaultHash.from(signature);
}

static String extractSignature(String byteBuddySignature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import lombok.Value;

import com.navercorp.scavenger.model.CodeBasePublication;
import com.navercorp.scavenger.util.HashGenerator;
import com.navercorp.scavenger.util.HashGenerator.DefaultHash;

@Value
public class Method {
Expand Down Expand Up @@ -111,7 +111,7 @@ public CodeBasePublication.CodeBaseEntry toCodeBaseEntry() {
.setModifiers(defaultIfNull(this.modifiers, ""))
.setPackageName(defaultIfNull(packageName, ""))
.setParameterTypes(defaultIfNull(this.parameterTypes, ""))
.setSignatureHash(HashGenerator.Md5.from(signature))
.setSignatureHash(DefaultHash.from(signature))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import org.junit.jupiter.api.Test;

import com.navercorp.scavenger.util.HashGenerator;
import com.navercorp.scavenger.util.HashGenerator.DefaultHash;

class MethodTest {
@Test
Expand All @@ -19,7 +19,7 @@ void testMethodCreation() {
assertThat(it.getModifiers()).isEqualTo("static");
assertThat(it.getPackageName()).isEqualTo("packageName");
assertThat(it.getSignature()).isEqualTo("name(int a, int wow)");
assertThat(it.getSignatureHash()).isEqualTo(HashGenerator.Md5.from("name(int a, int wow)"));
assertThat(it.getSignatureHash()).isEqualTo(DefaultHash.from("name(int a, int wow)"));
});

Method method2 = new Method(null, Visibility.PRIVATE, null, false, false, "", "int a,int wow", null);
Expand All @@ -30,7 +30,7 @@ void testMethodCreation() {
assertThat(it.getModifiers()).isEqualTo("");
assertThat(it.getPackageName()).isEqualTo("");
assertThat(it.getSignature()).isEqualTo("");
assertThat(it.getSignatureHash()).isEqualTo(HashGenerator.Md5.from(""));
assertThat(it.getSignatureHash()).isEqualTo(DefaultHash.from(""));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.navercorp.scavenger.dto.CommonImportDto
import com.navercorp.scavenger.dto.CommonImportResultDto
import com.navercorp.scavenger.dto.InvocationImportDto
import com.navercorp.scavenger.exception.UnknownPublicationException
import com.navercorp.scavenger.util.HashGenerator
import com.navercorp.scavenger.util.HashGenerator.DefaultHash
import io.codekvast.javaagent.model.v4.CodeBasePublication4
import io.codekvast.javaagent.model.v4.CommonPublicationData4
import io.codekvast.javaagent.model.v4.InvocationDataPublication4
Expand Down Expand Up @@ -117,7 +117,7 @@ sealed class LegacyPublication private constructor(val commonData: CommonPublica
modifiers = it.methodSignature.modifiers,
packageName = it.methodSignature.packageName,
parameterTypes = it.methodSignature.parameterTypes,
signatureHash = HashGenerator.Md5.from(it.signature)
signatureHash = DefaultHash.from(it.signature)
)
}.sortedBy { it.signatureHash }

Expand All @@ -141,7 +141,7 @@ sealed class LegacyPublication private constructor(val commonData: CommonPublica
environmentId = environmentId,
invocations = pub.invocations
.filterNot { syntheticSignaturePattern.matcher(it).matches() }
.map { HashGenerator.Md5.from(it) }
.map { DefaultHash.from(it) }
.sorted(),
invokedAtMillis = pub.recordingIntervalStartedAtMillis,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.navercorp.scavenger.model

import com.navercorp.scavenger.dto.CommonImportResultDto
import com.navercorp.scavenger.model.CodeBasePublication.CodeBaseEntry
import com.navercorp.scavenger.util.HashGenerator.Md5
import com.navercorp.scavenger.util.HashGenerator.DefaultHash
import com.navercorp.scavenger.util.SamplePublications
import io.codekvast.javaagent.model.v4.CodeBaseEntry4
import io.codekvast.javaagent.model.v4.CodeBasePublication4
Expand Down Expand Up @@ -72,14 +72,14 @@ class PublicationTest {
).getCodeBaseImportDto(commonImportResultDto).entries
).hasSize(2)
.extracting("signatureHash")
.isEqualTo(listOf(Md5.from(targetSignature), Md5.from(CodeBaseEntry4.sampleCodeBaseEntry().signature)).sorted())
.isEqualTo(listOf(DefaultHash.from(targetSignature), DefaultHash.from(CodeBaseEntry4.sampleCodeBaseEntry().signature)).sorted())
}
}

@Nested
@DisplayName("if invocation data contains SpringCGLIB generated method")
inner class Invocation {
private val cglibHash = Md5.from(cglibSignature)
private val cglibHash = DefaultHash.from(cglibSignature)

@Test
@DisplayName("it ignores it")
Expand Down Expand Up @@ -117,7 +117,7 @@ class PublicationTest {
)
.build()
).getInvocationImportDto(commonImportResultDto).invocations
).isEqualTo(listOf(Md5.from(targetSignature), Md5.from("signature()")).sorted())
).isEqualTo(listOf(DefaultHash.from(targetSignature), DefaultHash.from("signature()")).sorted())
}
}
}
Expand All @@ -129,10 +129,10 @@ class PublicationTest {

private val codeBaseEntries = listOf(
CodeBaseEntry.newBuilder()
.setSignatureHash((Md5.from("TestClass.method()")))
.setSignatureHash(DefaultHash.from("TestClass.method()"))
.build(),
CodeBaseEntry.newBuilder()
.setSignatureHash((Md5.from("signature()")))
.setSignatureHash(DefaultHash.from("signature()"))
.build()
)

Expand All @@ -149,7 +149,7 @@ class PublicationTest {
.getCodeBaseImportDto(commonImportResultDto).entries
)
.extracting("signatureHash")
.isEqualTo(listOf(Md5.from("TestClass.method()"), Md5.from("signature()")).sorted())
.isEqualTo(listOf(DefaultHash.from("TestClass.method()"), DefaultHash.from("signature()")).sorted())
}

@Test
Expand All @@ -161,17 +161,17 @@ class PublicationTest {
.setCommonData(SamplePublications.commonPublicationData)
.addEntry(
InvocationDataPublication.InvocationDataEntry.newBuilder()
.setHash(Md5.from("signature()"))
.setHash(DefaultHash.from("signature()"))
)
.addEntry(
InvocationDataPublication.InvocationDataEntry.newBuilder()
.setHash(Md5.from("TestClass.method()"))
.setHash(DefaultHash.from("TestClass.method()"))
)
.setRecordingIntervalStartedAtMillis(0)
.build()
).getInvocationImportDto(commonImportResultDto).invocations
)
.isEqualTo(listOf(Md5.from("TestClass.method()"), Md5.from("signature()")).sorted())
.isEqualTo(listOf(DefaultHash.from("TestClass.method()"), DefaultHash.from("signature()")).sorted())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Callable;

public class HashGenerator {
public static class Sha256 {

public static class DefaultHash {
public static String from(String signature) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(signature.getBytes(StandardCharsets.UTF_8));
return String.format("%x", new BigInteger(1, md.digest()));
} catch (NoSuchAlgorithmException ignore) {
// ignore
return null;
}
return Md5.from(signature);
}
}

public static class Md5 {
public static String from(String signature) {
try {
if (signature == null) {
return null;
}
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(signature.getBytes(StandardCharsets.UTF_8));
return String.format("%x", new BigInteger(1, md.digest()));
} catch (NoSuchAlgorithmException ignore) {
// ignore
return null;
}
private static class Sha256 {
private static String from(String signature) {
MessageDigest md = callWithCheckedExceptionWrapping(() -> MessageDigest.getInstance("SHA-256"));
md.update(signature.getBytes(StandardCharsets.UTF_8));
return String.format("%x", new BigInteger(1, md.digest()));
}
}

private static class Md5 {
private static String from(String signature) {
MessageDigest md = callWithCheckedExceptionWrapping(() -> MessageDigest.getInstance("MD5"));
md.update(signature.getBytes(StandardCharsets.UTF_8));
return String.format("%x", new BigInteger(1, md.digest()));
}
}

/**
* Utility method to call a {@link Callable} and rethrow any exceptions as unchecked.
*/
private static <T> T callWithCheckedExceptionWrapping(Callable<T> callable) {
try {
return callable.call();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit a9f037d

Please sign in to comment.