SHA sum dir

This commit is contained in:
Meutel 2017-07-08 11:13:37 +02:00
parent fd47df36b7
commit 5093daa9f6
2 changed files with 49 additions and 0 deletions

1
.gitignore vendored
View File

@ -36,3 +36,4 @@ statecsv/statecsv
go-financial/go-financial
my-fnv/my-fnv
my-md5/my-md5
shadir/shadir

48
shadir/main.go Normal file
View File

@ -0,0 +1,48 @@
package main
import "fmt"
import "crypto/sha256"
import "io"
import "log"
import "os"
import "path/filepath"
func walk(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatalln(err)
return err
}
if info.IsDir() {
return nil
} else {
h, err := sha256File(path)
if err != nil {
return err
}
fmt.Printf("SHA256 (%s) = %x\n", info.Name(), h)
}
return nil
}
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 main() {
if len(os.Args) < 2 {
log.Fatalln("Usage shadir file")
}
if err := filepath.Walk(os.Args[1], walk); err != nil {
panic(err)
}
}