cs/hash.go

127 lines
2.1 KiB
Go
Raw Normal View History

2016-11-16 01:18:35 -08:00
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"io"
"os"
"sync"
)
// result is a message or error payload
type result struct {
f string
cs string
2016-11-16 01:18:35 -08:00
err error
}
// hashr exists so that we can make a thing that can return valid hash.Hash
// interfaces.
type hashr func() hash.Hash
// hsh figures out which hash algo to use, and distributes the work of hashing
func hsh(files []string) chan result {
var h hashr
switch *algo {
case "sha1", "1":
h = sha1.New
case "sha256", "256":
h = sha256.New
case "sha512", "512":
h = sha512.New
case "md5":
h = md5.New
default:
r := make(chan result)
go func() {
2017-01-16 19:02:41 -08:00
r <- result{err: fmt.Errorf("unsupported algorithm: %v (supported: md5, sha1, sha256, sha512)", *algo)}
2017-01-16 19:00:25 -08:00
close(r)
2016-11-16 01:18:35 -08:00
}()
return r
}
if len(files) == 0 {
2016-11-16 01:27:11 -08:00
r := make(chan result)
go func() {
hsh := h()
_, err := io.Copy(hsh, os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
r <- result{cs: fmt.Sprintf("%x", hsh.Sum(nil)), f: "-"}
2016-11-16 01:27:11 -08:00
close(r)
}()
return r
2016-11-16 01:18:35 -08:00
}
jobs := make(chan checksum)
go func() {
for _, name := range files {
jobs <- checksum{filename: name}
}
close(jobs)
}()
res := []<-chan result{}
for w := 0; w < *ngo; w++ {
res = append(res, compute(h, jobs))
}
return rmerge(res)
}
// compute is the checksumming workhorse
func compute(h hashr, jobs chan checksum) chan result {
hsh := h()
r := make(chan result)
go func() {
for job := range jobs {
f, err := os.Open(job.filename)
if err != nil {
r <- result{err: err}
continue
}
hsh.Reset()
_, err = io.Copy(hsh, f)
f.Close()
if err != nil {
r <- result{err: err}
continue
}
r <- result{f: job.filename, cs: fmt.Sprintf("%x", hsh.Sum(nil))}
2016-11-16 01:18:35 -08:00
}
close(r)
}()
return r
}
// rmerge implements fan-in
func rmerge(cs []<-chan result) chan result {
out := make(chan result)
var wg sync.WaitGroup
output := func(c <-chan result) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(cs))
for _, c := range cs {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}