protoreflecthash

package module
v0.0.0-...-b7269c7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*hasher)

Option modifies how hashes for protobufs is calculated.

func FieldNamesAsKeys

func FieldNamesAsKeys() Option

FieldNamesAsKeys is an option that uses field names for key hashing rather than the field number.

func MessageFullnameIdentifier

func MessageFullnameIdentifier() Option

MessageFullnameIdentifier is an option that uses the message descriptor fullname rather than a generic map identifier when hashing messages.

type ProtoHasher

type ProtoHasher interface {
	// HashProto returns the object hash of a given protocol buffer message.
	HashProto(msg protoreflect.Message) ([]byte, error)
}

ProtoHasher is an interface for hashers that are capable of returning an ObjectHash for protobufs.

func NewHasher

func NewHasher(options ...Option) ProtoHasher

NewHasher creates a new ProtoHasher with the options specified in the argument.

Directories

Path Synopsis
test_protos

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL