forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (70 loc) · 1.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package main
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"io"
"os"
"time"
"github.com/codahale/blake2"
humanize "github.com/dustin/go-humanize"
flag "github.com/juju/gnuflag"
"github.com/kch42/buzhash"
)
func main() {
useSHA := flag.String("use-sha", "", "<default>=no hashing, 1=sha1, 256=sha256, 512=sha512, blake=blake2b")
useBH := flag.Bool("use-bh", false, "whether we buzhash the bytes")
flag.Parse(true)
flag.Usage = func() {
fmt.Printf("%s <big-file>\n", os.Args[0])
flag.PrintDefaults()
return
}
if len(flag.Args()) < 1 {
flag.Usage()
return
}
p := flag.Args()[0]
bh := buzhash.NewBuzHash(64 * 8)
f, _ := os.Open(p)
defer f.Close()
t0 := time.Now()
buf := make([]byte, 4*1024)
l := uint64(0)
var h hash.Hash
if *useSHA == "1" {
h = sha1.New()
} else if *useSHA == "256" {
h = sha256.New()
} else if *useSHA == "512" {
h = sha512.New()
} else if *useSHA == "blake" {
h = blake2.NewBlake2B()
}
for {
n, err := f.Read(buf)
l += uint64(n)
if err == io.EOF {
break
}
s := buf[:n]
if h != nil {
h.Write(s)
}
if *useBH {
bh.Write(s)
}
}
t1 := time.Now()
d := t1.Sub(t0)
fmt.Printf("Read %s in %s (%s/s)\n", humanize.Bytes(l), d, humanize.Bytes(uint64(float64(l)/d.Seconds())))
digest := []byte{}
if h != nil {
fmt.Printf("%x\n", h.Sum(digest))
}
}