-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashing.go
54 lines (45 loc) · 1.1 KB
/
hashing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"crypto/sha256"
"io"
"log"
"os"
"fmt"
"encoding/hex"
// "context"
)
// FileHashPair Type for pairs of filenames and hashes
type FileHashPair struct {
FileName string ;
FileHash string
}
// ListMessage Type for pairs of filenames and hashes
type ListMessage struct {
Size int32;
DirHashes []FileHashPair
}
func hashFile(f *os.File,res chan<- FileHashPair) {
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
var fhp FileHashPair
fhp.FileName = f.Name()
fhp.FileHash = hex.EncodeToString(h.Sum(nil))
res <- fhp
}
func hashDirectory(path string,files []os.FileInfo ) {
hashPairs := make(chan FileHashPair,5)
for _, file := range files {
fd, _ := os.Open(path+file.Name())
defer fd.Close()
go hashFile(fd,hashPairs)
}
results := make([]FileHashPair, len(files))
for i := range results {
results[i] = <-hashPairs
}
for i := range results {
fmt.Printf("Hash pair{%s,%s}\n",results[i].FileName,results[i].FileHash)
}
}