代码拉取完成,页面将自动刷新
同步操作将从 TavenLi/FileHashGen 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。