b3sum/main.go

47 lines
787 B
Go

package main
import (
"encoding/hex"
"flag"
"fmt"
"io"
"os"
"github.com/zeebo/blake3"
)
var (
help = flag.Bool("h", false, "Prints help information")
length = flag.Int("l", 32, "The number of output bytes, prior to hex encoding (default 32)")
)
func main() {
flag.Parse()
if *help || len(flag.Args()) == 0 {
flag.Usage()
os.Exit(0)
}
buf := [65536]byte{}
for _, name := range flag.Args() {
file, err := os.Open(name)
if err != nil {
panic(err)
}
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)
}
}