Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Forked from spikebike/bdr_proto.proto
Created April 18, 2012 01:06
Show Gist options
  • Save kylelemons/2410270 to your computer and use it in GitHub Desktop.
Save kylelemons/2410270 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 := &blob_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)
hash := fmt.Sprintf("%x", hash.Sum(nil))
req.Blobs = append(req.Blobs, &blob_proto.RequestBlob{
Hash: &hash,
Size: &size,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment