You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.1 KiB
67 lines
1.1 KiB
package main |
|
|
|
import ( |
|
"encoding/hex" |
|
"flag" |
|
"fmt" |
|
"io" |
|
"os" |
|
"runtime" |
|
"sync" |
|
|
|
"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") |
|
par = flag.Int("p", runtime.NumCPU(), "The maximum concurrency") |
|
) |
|
|
|
func main() { |
|
flag.Parse() |
|
if *help || *par < 1 { |
|
flag.Usage() |
|
os.Exit(0) |
|
} |
|
sum := func(file io.ReadCloser) string { |
|
buf := [65536]byte{} |
|
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{}) |
|
return hex.EncodeToString(hash) |
|
} |
|
|
|
if len(flag.Args()) == 0 { |
|
fmt.Println(sum(os.Stdin)) |
|
} else { |
|
q := make(chan struct{}, *par) |
|
wg := &sync.WaitGroup{} |
|
|
|
for _, name := range flag.Args() { |
|
q <- struct{}{} |
|
wg.Add(1) |
|
go func(name string) { |
|
file, err := os.Open(name) |
|
if err != nil { |
|
panic(err) |
|
} |
|
fmt.Print(sum(file)) |
|
fmt.Print(" ") |
|
fmt.Println(name) |
|
<- q |
|
wg.Done() |
|
}(name) |
|
} |
|
wg.Wait() |
|
} |
|
}
|
|
|