summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sample/file-hasher-requst.json16
-rw-r--r--src/file-hasher/hasher.go52
-rw-r--r--src/file-hasher/main.go18
3 files changed, 62 insertions, 24 deletions
diff --git a/sample/file-hasher-requst.json b/sample/file-hasher-requst.json
index 4ef0497..d2a1446 100644
--- a/sample/file-hasher-requst.json
+++ b/sample/file-hasher-requst.json
@@ -1,10 +1,12 @@
{
"threads": 2,
- "dir": [
- "../../../../Music/Dead Man's Bones - Dead Man's Bones (2009)",
- "../../../../Music/Hozier - Hozier"
- ],
- "m3u": [
- "../../../../Music/Hozier - From Eden/00-hozier-from_eden_ep-web-2014.m3u"
- ]
+ "src": {
+ "dir": [
+ "../../../../Music/Dead Man's Bones - Dead Man's Bones (2009)",
+ "../../../../Music/Hozier - Hozier"
+ ],
+ "m3u": [
+ "../../../../Music/Hozier - From Eden/00-hozier-from_eden_ep-web-2014.m3u"
+ ]
+ }
}
diff --git a/src/file-hasher/hasher.go b/src/file-hasher/hasher.go
index 27574f9..5f84646 100644
--- a/src/file-hasher/hasher.go
+++ b/src/file-hasher/hasher.go
@@ -23,32 +23,32 @@ package main
import (
"encoding/base64"
+ "hash"
"io"
"log"
"os"
"path/filepath"
"sync"
+ "crypto/md5"
+ "crypto/sha1"
+ "crypto/sha256"
+ "crypto/sha512"
"golang.org/x/crypto/blake2b"
)
type FileMap map[string]string
-func (m FileMap) Merge(m2 FileMap) {
- for key, value := range m2 {
- m[key] = value
- }
-}
-
type Hasher struct {
numThreads uint
stdlog *log.Logger
+ newHash func() (hash.Hash, error)
filesMtx sync.Mutex
Files FileMap
}
-func computeHash(path string) (string, error) {
- hash, err := blake2b.New256(nil)
+func (h *Hasher) computeHash(path string) (string, error) {
+ hash, err := h.newHash()
if err != nil {
return "", err
}
@@ -75,7 +75,7 @@ func (h *Hasher) collectHashes(C <-chan string) (wg *sync.WaitGroup) {
if !ok {
return
}
- if hash, err := computeHash(file); err != nil {
+ if hash, err := h.computeHash(file); err != nil {
h.stdlog.Printf(" - skipping (%v): %s", err, filepath.Base(file))
} else {
h.stdlog.Printf(" - hashed: %s", filepath.Base(file))
@@ -103,12 +103,42 @@ func (h *Hasher) ComputeHashes(w Walker) (err error) {
return
}
-func NewHasher(numThreads uint, stdlog *log.Logger) (h *Hasher) {
+func NewHasher(algo string, numThreads uint, stdlog *log.Logger) (h *Hasher) {
h = &Hasher{numThreads: numThreads, stdlog: stdlog}
+
+ switch algo {
+ case "md5":
+ h.newHash = func() (hash.Hash, error) {
+ return md5.New(), nil
+ }
+ case "sha1":
+ h.newHash = func() (hash.Hash, error) {
+ return sha1.New(), nil
+ }
+ case "sha256":
+ h.newHash = func() (hash.Hash, error) {
+ return sha256.New(), nil
+ }
+ case "sha512":
+ h.newHash = func() (hash.Hash, error) {
+ return sha512.New(), nil
+ }
+ case "blake2b":
+ fallthrough
+ case "":
+ algo = "blake2b"
+ h.newHash = func() (hash.Hash, error) {
+ return blake2b.New256(nil)
+ }
+ default:
+ stdlog.Printf("*** invalid hashing algorithm: '%s'", algo)
+ return nil
+ }
+
h.Files = make(map[string]string)
if h.numThreads < 1 {
h.numThreads = 4
}
- stdlog.Printf("*** Created blake2b based hasher (%d threads)", h.numThreads)
+ stdlog.Printf("*** Created hasher (%s, %d threads)", algo, h.numThreads)
return
}
diff --git a/src/file-hasher/main.go b/src/file-hasher/main.go
index f360705..c7718ee 100644
--- a/src/file-hasher/main.go
+++ b/src/file-hasher/main.go
@@ -31,9 +31,12 @@ import (
)
type Request struct {
- NumThreads uint `json:"threads"`
- Directories []string `json:"dir"`
- M3UPlaylists []string `json:"m3u"`
+ NumThreads uint `json:"threads"`
+ HashAlgo string `json:"algo"`
+ Sources struct {
+ Directories []string `json:"dir"`
+ M3UPlaylists []string `json:"m3u"`
+ } `json:"src"`
}
func main() {
@@ -53,11 +56,14 @@ func main() {
C <- syscall.SIGTERM
}()
stdlog.Println("***************************************************************")
- h := NewHasher(req.NumThreads, stdlog)
+ h := NewHasher(req.HashAlgo, req.NumThreads, stdlog)
+ if h == nil {
+ os.Exit(1)
+ }
stdlog.Println("")
stdlog.Println("******************************")
- for _, root := range req.Directories {
+ for _, root := range req.Sources.Directories {
src := NewDir(root, stdlog)
stdlog.Printf("*** hashing all the files in '%s'", root)
stdlog.Println("")
@@ -68,7 +74,7 @@ func main() {
stdlog.Println("")
stdlog.Println("******************************")
}
- for _, list := range req.M3UPlaylists {
+ for _, list := range req.Sources.M3UPlaylists {
src := NewM3u(list, stdlog)
stdlog.Printf("*** hashing all the files from '%s'", list)
stdlog.Println("")