Commit 05aa0df5 authored by Brendan Ribera's avatar Brendan Ribera

Fix hash computation for JRuby's RubyMessage

`System.identityHashCode` returns a hash that does not consider a
Message's values. This means two Messages with identical values will not
have identical hashCodes.

This patch uses the pattern from RubyMap to combine the hashCodes from
all values in a given message and produce a unique, consistent,
value-based hash.
parent 7b00595d
......@@ -41,6 +41,8 @@ import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
......@@ -164,8 +166,21 @@ public class RubyMessage extends RubyObject {
*/
@JRubyMethod
public IRubyObject hash(ThreadContext context) {
int hashCode = System.identityHashCode(this);
return context.runtime.newFixnum(hashCode);
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
for (RubyMap map : maps.values()) {
digest.update((byte) map.hashCode());
}
for (RubyRepeatedField repeatedField : repeatedFields.values()) {
digest.update((byte) repeatedFields.hashCode());
}
for (IRubyObject field : fields.values()) {
digest.update((byte) field.hashCode());
}
return context.runtime.newString(new ByteList(digest.digest()));
} catch (NoSuchAlgorithmException ignore) {
return context.runtime.newFixnum(System.identityHashCode(this));
}
}
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment