b3sum/main.go

59 lines
1021 B
Go
Raw Normal View History

2020-04-02 12:02:23 -04:00
package main
import (
"encoding/hex"
"flag"
"fmt"
"io"
"os"
2020-04-02 14:06:11 -04:00
"sync"
2020-04-02 12:02:23 -04:00
"github.com/zeebo/blake3"
)
var (
help = flag.Bool("h", false, "Prints help information")
2020-04-02 14:06:11 -04:00
length = flag.Int("l", 32, "The number of output bytes, prior to hex encoding")
par = flag.Int("p", 4, "The maximum concurrency")
2020-04-02 12:02:23 -04:00
)
func main() {
flag.Parse()
2020-04-02 14:06:11 -04:00
if *help || len(flag.Args()) == 0 || *par < 1 {
2020-04-02 12:02:23 -04:00
flag.Usage()
os.Exit(0)
}
2020-04-02 14:06:11 -04:00
q := make(chan struct{}, *par)
wg := &sync.WaitGroup{}
2020-04-02 12:02:23 -04:00
for _, name := range flag.Args() {
2020-04-02 14:06:11 -04:00
q <- struct{}{}
wg.Add(1)
go func(name string) {
buf := [65536]byte{}
file, err := os.Open(name)
if err != nil {
2020-04-02 12:02:23 -04:00
panic(err)
}
2020-04-02 14:06:11 -04:00
hasher := blake3.NewSized(*length)
for {
n, err := file.Read(buf[:])
hasher.Write(buf[:n])
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
file.Close()
hash := hasher.Sum([]byte{})
fmt.Print(hex.EncodeToString(hash))
fmt.Print(" ")
fmt.Println(name)
<- q
wg.Done()
}(name)
2020-04-02 12:02:23 -04:00
}
2020-04-02 14:06:11 -04:00
wg.Wait()
2020-04-02 12:02:23 -04:00
}