Skip to content

stackb/protoreflecthash

Repository files navigation

CI

protobuf objecthash

protoreflecthash

protoreflecthash is a re-implementation of https://github.com/deepmind/objecthash-proto. That repo is now archived and was never updated for protobuf-apiv2.

Usage

go get github.com/stackb/protoreflecthash@latest
package main

import (
    "github.com/stackb/protoreflecthash"
)

func main() {
    msg := mustGetProtoMessageSomewhere()

    options := []protoreflect.Option{
      // protoreflect.MessageFullnameIdentifier(),
      // protoreflect.FieldNamesAsKeys(),
    }
    hasher := protoreflect.NewHasher(options...)

    hash, err := hasher.HashProto(msg.ProtoReflect())
    if err != nil {
        panic(err.Error())
    }

    fmt.Printf("%x\n", hash)
}

Background

protoreflecthash computes the hash value for a protobuf message by taking a sha256 of the sum the individual component hashes of the message. Special care is taken to account for various semantics of the protobuf format.

This implementation passes all functional unit tests from the original library deepmind/objecthash-proto (excluding badness detection).

Open questions remain about the handling of protobufs with extension fields and the google.protobuf.Any type.

This package is currently experimental; hash values for messages may change without warning until v1.