71 lines
1.0 KiB
Go
71 lines
1.0 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
import "crypto/sha256"
|
|
import "io"
|
|
import "log"
|
|
import "os"
|
|
import "path/filepath"
|
|
import "sync"
|
|
|
|
var chan_hash chan string
|
|
var wg sync.WaitGroup
|
|
|
|
func walk(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
} else {
|
|
wg.Add(1)
|
|
go hash(info.Name(), path)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func hash(name string, path string) {
|
|
h, err := sha256File(path)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
chan_hash <- fmt.Sprintf("SHA256 (%s) = %x", name, h)
|
|
}
|
|
|
|
func sha256File(path string) ([]byte, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
h := sha256.New()
|
|
io.Copy(h, f)
|
|
return h.Sum(nil), nil
|
|
}
|
|
|
|
func printer() {
|
|
for {
|
|
select {
|
|
case str := <-chan_hash:
|
|
fmt.Println(str)
|
|
wg.Done()
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
log.Fatalln("Usage shadir file")
|
|
}
|
|
chan_hash = make(chan string)
|
|
|
|
go printer()
|
|
if err := filepath.Walk(os.Args[1], walk); err != nil {
|
|
panic(err)
|
|
}
|
|
wg.Wait()
|
|
}
|