Skip to content

Instantly share code, notes, and snippets.

@spikebike
Forked from kylelemons/bdr_proto.proto
Created April 18, 2012 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spikebike/2410407 to your computer and use it in GitHub Desktop.
Save spikebike/2410407 to your computer and use it in GitHub Desktop.
example for protobuf, go, and the repeated protobuf entry
package bdr_proto;
message request {
message blob {
required string hash = 1; // the sha256 checksum of the blob
required int32 size = 2; // the size of the blob
}
repeated blob blobs = 1;
}
package main
import (
"./bdr_proto"
"crypto/sha256"
"fmt"
)
//import M "math/rand"
import C "crypto/rand"
func main() {
req := &bdr_proto.Request{}
for i := 0; i < 32; i++ {
// read in 16 bytes from /dev/urandom to sha256
randBytes := make([]byte, 16)
C.Read(randBytes)
// get its size
size := int32(len(randBytes))
// create a new hash, and do a crypty hash of the random bytes.
sha := sha256.New()
sha.Write(randBytes)
strhash := fmt.Sprintf("%x", sha.Sum(nil))
fmt.Printf("i=%d sha=%x size=%d\n", i,strhash,size)
req.Blobarray = append(req.Blobarray, &bdr_proto.RequestBlob{Sha256: &strhash, Bsize: &size})
}
fmt.Printf("%#V %T\n", req, req)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment