62 lines
1.0 KiB
Go
62 lines
1.0 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
import "crypto/sha256"
|
|
import "io"
|
|
import "log"
|
|
import "os"
|
|
import "path/filepath"
|
|
import "sync"
|
|
|
|
func sha256File(path string) (string, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer f.Close()
|
|
h := sha256.New()
|
|
io.Copy(h, f)
|
|
return fmt.Sprintf("SHA256 (%s) = %x", path, h.Sum(nil)), nil
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
log.Fatalln("Usage shadir file")
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
chan_hash, chan_path := make(chan string), make(chan string)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
go func() {
|
|
for path := range chan_path {
|
|
if h, err := sha256File(path); err != nil {
|
|
wg.Done()
|
|
log.Print("Error "+path, err)
|
|
} else {
|
|
chan_hash <- h
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
go func() {
|
|
for h := range chan_hash {
|
|
fmt.Println(h)
|
|
wg.Done()
|
|
}
|
|
}()
|
|
|
|
err := filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error {
|
|
if err == nil && !info.IsDir() {
|
|
wg.Add(1)
|
|
chan_path <- path
|
|
}
|
|
return err
|
|
})
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
wg.Wait()
|
|
}
|