Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Messing around with the occasional last hash element #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions ssdeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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])
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions ssdeep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
Expand Down