An open API service indexing awesome lists of open source software.

https://github.com/stackb/protoreflecthash

Reimplementation of https://github.com/deepmind/objecthash-proto for V2 protobuf API
https://github.com/stackb/protoreflecthash

object-hash protobuf

Last synced: 10 months ago
JSON representation

Reimplementation of https://github.com/deepmind/objecthash-proto for V2 protobuf API

Awesome Lists containing this project

README

          

[![CI](https://github.com/stackb/protoreflecthash/actions/workflows/ci.yaml/badge.svg)](https://github.com/stackb/protoreflecthash/actions/workflows/ci.yaml)






protobuf
objecthash

# protoreflecthash

protoreflecthash is a re-implementation of
. That repo is now archived and
was never updated for [protobuf-apiv2](https://go.dev/blog/protobuf-apiv2).

# Usage

```
go get github.com/stackb/protoreflecthash@latest
```

```go
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](https://github.com/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.