Created
June 28, 2017 12:57
-
-
Save nak3/7cbd3f4cf32d05239b339ecb58a8be7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/sha256" | |
"fmt" | |
"io" | |
"math/rand" | |
c "github.com/restic/chunker" | |
) | |
func main() { | |
ExampleChunker() | |
} | |
func ExampleChunker() { | |
// generate 32MiB of deterministic pseudo-random data | |
data := getRandom(23, 32*1024*1024) | |
// create a chunker | |
chunker := c.New(bytes.NewReader(data), c.Pol(0x3DA3358B4DC173)) | |
// reuse this buffer | |
buf := make([]byte, 8*1024*1024) | |
for { | |
chunk, err := chunker.Next(buf) | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("%d %02x\n", chunk.Length, sha256.Sum256(chunk.Data)) | |
} | |
// Output: | |
// 2163460 4b94cb2cf293855ea43bf766731c74969b91aa6bf3c078719aabdd19860d590d | |
// 643703 5727a63c0964f365ab8ed2ccf604912f2ea7be29759a2b53ede4d6841e397407 | |
// 1528956 a73759636a1e7a2758767791c69e81b69fb49236c6929e5d1b654e06e37674ba | |
// 1955808 c955fb059409b25f07e5ae09defbbc2aadf117c97a3724e06ad4abd2787e6824 | |
// 2222372 6ba5e9f7e1b310722be3627716cf469be941f7f3e39a4c3bcefea492ec31ee56 | |
// ... | |
} | |
func getRandom(seed int64, count int) []byte { | |
buf := make([]byte, count) | |
rnd := rand.New(rand.NewSource(seed)) | |
for i := 0; i < count; i += 4 { | |
r := rnd.Uint32() | |
buf[i] = byte(r) | |
buf[i+1] = byte(r >> 8) | |
buf[i+2] = byte(r >> 16) | |
buf[i+3] = byte(r >> 24) | |
} | |
return buf | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment