From f32558514a3fd097ba6d8fdf8e6b054099332cc2 Mon Sep 17 00:00:00 2001 From: Crack Duck Date: Wed, 27 Feb 2019 11:58:12 +0100 Subject: [PATCH] added quick hash function --- sneakerhash.go | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/sneakerhash.go b/sneakerhash.go index e752108..ba8bba6 100755 --- a/sneakerhash.go +++ b/sneakerhash.go @@ -1,8 +1,7 @@ package main import ( - "database/sql" - //"github.com/kalafut/imohash" + "database/sql" _ "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,10 +55,29 @@ 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("Size: %d\n", info.Size()) - fmt.Printf("Time: %s\n\n", info.ModTime().Format(time.RFC3339)) + 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 )"+ "VALUES ('"+hashSum+"','"+info.Name()+"','"+strconv.FormatInt(info.Size(),10)+"','"+path+"','"+info.ModTime().Format(time.RFC3339)+"');"