added quick hash function

master
Crack Duck 6 years ago
parent 74ca89a2e5
commit f32558514a

@ -2,7 +2,6 @@ package main
import (
"database/sql"
//"github.com/kalafut/imohash"
_ "github.com/mattn/go-sqlite3"
"log"
"io"
@ -13,8 +12,26 @@ import (
"path/filepath"
"strconv"
"encoding/hex"
"encoding/binary"
"hash"
"reflect"
)
const sampleSize = 16 * 1024
const sampleThreshold = 48 * 1024
func copyHash(src hash.Hash) hash.Hash {
typ := reflect.TypeOf(src)
val := reflect.ValueOf(src)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
elem := reflect.New(typ).Elem()
elem.Set(val)
return elem.Addr().Interface().(hash.Hash)
}
func hash_directory (searchDir, database string) {
db, dberr := sql.Open("sqlite3", database)
if dberr != nil {
@ -38,8 +55,27 @@ func hash_directory (searchDir, database string) {
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
qh := copyHash(h)
if info.Size() > int64(sampleThreshold) {
qh.Reset()
buffer := make([]byte, sampleSize)
f.Read(buffer)
qh.Write(buffer)
f.Seek(info.Size()/2-sampleSize/2, 0)
f.Read(buffer)
qh.Write(buffer)
f.Seek(int64(-sampleSize), 2)
f.Read(buffer)
qh.Write(buffer)
}
buffer := make([]byte, 8)
binary.PutVarint(buffer, info.Size())
qh.Write(buffer)
quickSum := hex.EncodeToString(qh.Sum(nil))
hashSum := hex.EncodeToString(h.Sum(nil))
fmt.Printf("SHA1: %x\n", hashSum)
fmt.Printf("qSHA1: %x\n", quickSum)
fmt.Printf("Size: %d\n", info.Size())
fmt.Printf("Time: %s\n\n", info.ModTime().Format(time.RFC3339))
sqlStatement := "INSERT INTO hashes (hash_sha1, filename, filesize_bytes, path, changedate )"+

Loading…
Cancel
Save