From ae648b8cd68864bb3b827388342d11af07a243c8 Mon Sep 17 00:00:00 2001 From: Lukas Rist Date: Thu, 28 Apr 2022 17:16:51 +0200 Subject: [PATCH] not sure why this works --- ssdeep.go | 37 ++++++++++++++++++++----------------- ssdeep_test.go | 10 ++++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/ssdeep.go b/ssdeep.go index 59c5aee..86a8d83 100644 --- a/ssdeep.go +++ b/ssdeep.go @@ -47,27 +47,29 @@ func (rs *rollingState) rollSum() uint32 { } type ssdeepState struct { - rollingState rollingState - blockSize int - hashString1 string - hashString2 string - blockHash1 uint32 - blockHash2 uint32 + rState rollingState + blockSize int + hashString1 string + hashString2 string + blockHash1 uint32 + blockHash2 uint32 + lasthash uint32 } func newSsdeepState() ssdeepState { return ssdeepState{ blockHash1: hashInit, blockHash2: hashInit, - rollingState: rollingState{ + rState: rollingState{ window: make([]byte, rollingWindow), }, + blockSize: blockMin, } } func (state *ssdeepState) newRollingState() { - state.rollingState = rollingState{} - state.rollingState.window = make([]byte, rollingWindow) + state.rState = rollingState{} + state.rState.window = make([]byte, rollingWindow) } // sumHash based on FNV hash @@ -77,7 +79,7 @@ func sumHash(c byte, h uint32) uint32 { // rollHash based on Adler checksum func (state *ssdeepState) rollHash(c byte) { - rs := &state.rollingState + rs := &state.rState rs.h2 -= rs.h1 rs.h2 += rollingWindow * uint32(c) rs.h1 += uint32(c) @@ -93,18 +95,17 @@ func (state *ssdeepState) rollHash(c byte) { // getBlockSize calculates the block size based on file size func (state *ssdeepState) setBlockSize(n int) { - blockSize := blockMin - for blockSize*spamSumLength < n { - blockSize = blockSize * 2 + for state.blockSize*spamSumLength < n { + state.blockSize = state.blockSize * 2 } - state.blockSize = blockSize } func (state *ssdeepState) processByte(b byte) { + state.rollHash(b) state.blockHash1 = sumHash(b, state.blockHash1) state.blockHash2 = sumHash(b, state.blockHash2) - state.rollHash(b) - rh := int(state.rollingState.rollSum()) + state.lasthash = sumHash(b, state.lasthash) + rh := int(state.rState.rollSum()) if rh%state.blockSize == (state.blockSize - 1) { if len(state.hashString1) < spamSumLength-1 { state.hashString1 += string(b64[state.blockHash1%64]) @@ -177,11 +178,13 @@ func FuzzyReader(f Reader, fileSize int) (string, error) { state.hashString1 = "" state.hashString2 = "" } else { - rh := state.rollingState.rollSum() + rh := state.rState.rollSum() if rh != 0 { // Finalize the hash string with the remaining data state.hashString1 += string(b64[state.blockHash1%64]) state.hashString2 += string(b64[state.blockHash2%64]) + } else { + state.hashString1 += string(b64[state.lasthash%64] - 1) } break } diff --git a/ssdeep_test.go b/ssdeep_test.go index 7567156..087cee2 100644 --- a/ssdeep_test.go +++ b/ssdeep_test.go @@ -9,13 +9,11 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func assertHashEqual(t *testing.T, expected, actual string) { - if expected != actual { - t.Fatalf("Hash mismatch: %s (expected)\n"+ - " != %s (actual)", expected, actual) - } + require.Equal(t, expected, actual, "hash mismatch") } func TestIntegrity(t *testing.T) { @@ -58,12 +56,12 @@ func concatCopyPreAllocate(slices [][]byte) []byte { func TestRollingHash(t *testing.T) { s := ssdeepState{ - rollingState: rollingState{ + rState: rollingState{ window: make([]byte, rollingWindow), }, } s.rollHash(byte('A')) - rh := s.rollingState.rollSum() + rh := s.rState.rollSum() if rh != 585 { t.Fatal("Rolling hash not matching") }