Change from MD5 to Checksum (Adler32) for json content dirty detection

This commit is contained in:
rbygrave
2021-07-29 11:40:05 +12:00
parent 94fb6414fa
commit 52fe3cf3c8
4 changed files with 46 additions and 5 deletions
@@ -7,7 +7,7 @@ import io.ebean.core.type.DataReader;
import io.ebean.core.type.ScalarType;
import io.ebean.text.TextException;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import io.ebeaninternal.server.util.Md5;
import io.ebeaninternal.server.util.Checksum;
import javax.persistence.PersistenceException;
import java.sql.SQLException;
@@ -66,10 +66,14 @@ public class BeanPropertyJsonMapper extends BeanProperty {
private static class Md5MutableHash implements MutableHash {
private final String md5;
private final String hash;
Md5MutableHash(String json) {
md5 = Md5.hash(json);
this.hash = hash(json);
}
private String hash(String json) {
return String.valueOf(Checksum.checksum(json));
}
@Override
@@ -79,7 +83,7 @@ public class BeanPropertyJsonMapper extends BeanProperty {
@Override
public boolean isEqualToJson(String json) {
return Md5.hash(json).equals(md5);
return hash(json).equals(hash);
}
@Override
@@ -0,0 +1,20 @@
package io.ebeaninternal.server.util;
import java.nio.charset.StandardCharsets;
import java.util.zip.Adler32;
/**
* Compute a checksum for String content. Use when we desire cheaper option than MD5.
*/
public final class Checksum {
/**
* Return the checksum for the given String input.
*/
public static long checksum(String input) {
Adler32 adler32 = new Adler32();
final byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
adler32.update(bytes, 0, bytes.length);
return adler32.getValue();
}
}
@@ -21,7 +21,6 @@ public final class Md5 {
* Convert the digest into a hex value.
*/
private static String digestToHex(byte[] digest) {
StringBuilder sb = new StringBuilder();
for (byte aDigest : digest) {
sb.append(Integer.toString((aDigest & 0xff) + 0x100, 16).substring(1));