-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileHash_test.go
166 lines (126 loc) · 3.19 KB
/
FileHash_test.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
func TestTxtContent(t *testing.T) {
md5HashCoder := new(Md5Coder)
md5HashCoder.Create()
md5HashCoder.ReadFromString("123456")
fmt.Println(md5HashCoder.GenHashHex())
t.Log("ok")
}
func TestDirFiles(t *testing.T) {
//
path, _ := os.Executable()
fmt.Println(path)
_, exec := filepath.Split(path)
fmt.Println(exec)
workPath, _ := os.Getwd()
fmt.Println(workPath)
//默认从当前目录开始遍历
files := _walkDirectory(workPath)
fmt.Println("results count:", len(files))
for _, f := range files {
fmt.Println(f.FullPath)
for _, h := range f.Hashes {
fmt.Println(fmt.Sprint(h.CodeName, ":", h.HashVal))
}
}
t.Log("ok")
}
func _walkDirectory(toWalk string) []*FileResp {
var files []*FileResp
walkErr := filepath.WalkDir(toWalk, func(curFullPath string, curFile os.DirEntry, err error) error {
if err != nil {
return err
}
//fmt.Println(curFullPath)
if curFile.IsDir() {
//fmt.Println(curFile.Name())
} else {
fileInfo, _ := curFile.Info()
//fmt.Println(fileInfo.Name())
fileResp := new(FileResp)
fileResp.FileName = fileInfo.Name()
fileResp.FullPath = curFullPath
genHashFile := &GenHashFile{FullPath: curFullPath}
genHashFile.LoadAllCoders()
results := genHashFile.Generate()
fileResp.Hashes = results
files = append(files, fileResp)
}
return nil
})
if walkErr != nil {
fmt.Println(walkErr.Error())
}
return files
}
func TestSingleFile(t *testing.T) {
defer TimeCost(time.Now())
//filePath := "C:\\Users\\HJKL\\Desktop\\cn_sql_server_2012_enterprise_edition_x86_x64_dvd_813295.iso"
filePath := "C:\\Users\\HJKL\\Desktop\\payload.txt"
genHashFile := &GenHashFile{FullPath: filePath}
genHashFile.LoadAllCoders()
results := genHashFile.Generate()
for _, h := range results {
fmt.Println(h)
}
t.Log("ok")
}
func TestBigFile(t *testing.T) {
defer TimeCost(time.Now())
filePath := "C:\\Users\\HJKL\\Desktop\\cn_sql_server_2012_enterprise_edition_x86_x64_dvd_813295.iso"
//filePath := "C:\\Users\\HJKL\\Desktop\\payload.txt"
/*
file, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
panic(err.Error())
}
fi, _ := file.Stat()
fsize := fi.Size()
*/
//主要是考虑大文件,要并行计算
wg := new(sync.WaitGroup)
md5HashCoder := new(Md5Coder)
md5HashCoder.Create()
md5HashCoder.ReadFromChan(wg)
sha1HashCoder := new(SHA1Coder)
sha1HashCoder.Create()
sha1HashCoder.ReadFromChan(wg)
file, err := os.Open(filePath)
if err != nil {
panic(err.Error())
}
defer file.Close()
data := make([]byte, 4_194_304)
for {
n, err := file.Read(data)
if err != nil && err != io.EOF {
panic(err.Error())
break
}
// 需要复制一份,否则goroutines 共同对 data 对象的操作可能出现问题
tmp := make([]byte, len(data))
copy(tmp, data)
md5HashCoder.WriteToChan(tmp[:n])
sha1HashCoder.WriteToChan(tmp[:n])
if err == io.EOF {
break
}
}
md5HashCoder.CloseChan()
sha1HashCoder.CloseChan()
//需要先关闭通道,然后等待写入全部完成
wg.Wait()
hash := md5HashCoder.GenHashHex()
fmt.Println(hash)
fmt.Println(sha1HashCoder.GenHashHex())
t.Log("ok")
}