On Tue, Jul 19, 2016 at 11:46 AM, Alex Shaver <alexsha...@gmail.com> wrote:

> (proto3 question)
>
> Suppose I have some messages Foo and Bar, where Bar represents data for
> objects that are related to some Foo. I send the whole bunch of them in
> some set like so:
> message MyMessage{
>   repeated Foo = 1;
>   repeated Bar = 2;
> }
>
> What I'd like is for Bar to have a structure like:
> message Bar{
>   ...
>   int foo_id = 100;
> }
>
> where foo_id is the hash of the Foo protobuf that is associated with that
> given Bar protobuf.
>
> Now I know I could probably do something like
> bar.set_foo_id(md5(fooProto.SerializeAsString())) where i use md5 to just
> represent some common hashing function that should exist for the several
> languages, and just have it hash the serialized string of data. What I'm
> more wondering is if there's something more simple like
> bar.set_foo_id(fooProto.hash()), that will generate a consistent foo_id
> value between different platforms.
>
> Specifically, I'm doing this in c++ and javascript. I see the hashing
> seems to exist in java. I haven't found a similar function within the c++
> source, but that could just be something I haven't found; and I haven't yet
> looked on the javascript side of things.
>
Such a hash/fingerprint function doesn't exist in C++ and for those
languages who have it, the result will not be the same across different
languages. I suggest you write your own hash function on the proto message
instead of hashing the serialized bytes. The problem is that protobuf
doesn't guarantee the serialized bytes to be canonical and you can end up
with different hash value for the same message in the same binary. We are
improving this with a deterministic serialization support (see beta-4
release notes
<https://github.com/google/protobuf/releases/tag/v3.0.0-beta-4>) but it
still doesn't guarantee a canonical serialization across different
languages.


> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to