File tree 1 file changed +17
-2
lines changed
ruby/src/main/java/com/google/protobuf/jruby
1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change 41
41
import org .jruby .runtime .builtin .IRubyObject ;
42
42
import org .jruby .util .ByteList ;
43
43
44
+ import java .security .MessageDigest ;
45
+ import java .security .NoSuchAlgorithmException ;
44
46
import java .util .HashMap ;
45
47
import java .util .Map ;
46
48
@@ -164,8 +166,21 @@ public IRubyObject inspect() {
164
166
*/
165
167
@ JRubyMethod
166
168
public IRubyObject hash (ThreadContext context ) {
167
- int hashCode = System .identityHashCode (this );
168
- return context .runtime .newFixnum (hashCode );
169
+ try {
170
+ MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
171
+ for (RubyMap map : maps .values ()) {
172
+ digest .update ((byte ) map .hashCode ());
173
+ }
174
+ for (RubyRepeatedField repeatedField : repeatedFields .values ()) {
175
+ digest .update ((byte ) repeatedFields .hashCode ());
176
+ }
177
+ for (IRubyObject field : fields .values ()) {
178
+ digest .update ((byte ) field .hashCode ());
179
+ }
180
+ return context .runtime .newString (new ByteList (digest .digest ()));
181
+ } catch (NoSuchAlgorithmException ignore ) {
182
+ return context .runtime .newFixnum (System .identityHashCode (this ));
183
+ }
169
184
}
170
185
171
186
/*
You can’t perform that action at this time.
0 commit comments