Skip to content

Commit

Permalink
moved hash.Hash methods and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
glaslos committed Oct 24, 2022
1 parent fa155fa commit 1951c57
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 30 deletions.
27 changes: 27 additions & 0 deletions hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ssdeep

import "hash"

var _ hash.Hash = &ssdeepState{}

func New() *ssdeepState {
s := newSSDEEPState()
return &s
}

func (state *ssdeepState) Sum(b []byte) []byte {
digest, _ := state.digest()
return append(b, digest...)
}

func (state *ssdeepState) BlockSize() int {
return blockMin
}

func (state *ssdeepState) Size() int {
return spamSumLength
}

func (state *ssdeepState) Reset() {
*state = newSSDEEPState()
}
57 changes: 57 additions & 0 deletions hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ssdeep

import (
"fmt"
"hash"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHashinterface(t *testing.T) {
h := New()
var _ hash.Hash = h
t.Log(h.BlockSize())
h.Reset()

b, err := ioutil.ReadFile("ssdeep_results.json")
assert.NoError(t, err)

n, err := h.Write(b)
require.NoError(t, err)

t.Log(n)
t.Log(h.Size())

hashResult := h.Sum(nil)

expectedResult := "1536:74peLhFipssVfuInITTTZzMoW0379xy3u:VVFosEfudTj579k3u"
require.Equal(t, expectedResult, string(hashResult))

t.Log(hashResult)
t.Log(fmt.Sprintf("%x", hashResult[:]))
}

func TestHashWrite(t *testing.T) {
// hash using the hash.Hash interface methods
b, err := ioutil.ReadFile("ssdeep_results.json")
assert.NoError(t, err)

h1 := New()
h1.Write([]byte("1234"))
h1.Write(b)
t.Log(fmt.Sprintf("h1: %x", h1.Sum(nil)))

// hash from read
h2, err := FuzzyBytes(append([]byte("1234"), b...))
require.NoError(t, err)
t.Log(fmt.Sprintf("h2: %s", h2))

// compare hashes
diff := distance(string(h1.Sum(nil)), h2)
if diff != 0 {
t.Errorf("hashes differ by: %d", diff)
}
}
14 changes: 0 additions & 14 deletions ssdeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,3 @@ func FuzzyBytes(buffer []byte) (string, error) {

return result, nil
}

func (state *ssdeepState) Sum(b []byte) []byte {
digest, _ := state.digest()
return append(b, digest...)
}

func New() *ssdeepState {
s := newSSDEEPState()
return &s
}

func (state *ssdeepState) Reset() {
*state = newSSDEEPState()
}
25 changes: 9 additions & 16 deletions ssdeep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ import (
"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)
}
}

func TestIntegrity(t *testing.T) {
rand.Seed(1)
resultsFile, err := ioutil.ReadFile("ssdeep_results.json")
Expand All @@ -41,7 +34,7 @@ func TestIntegrity(t *testing.T) {
assert.NoError(t, err)
result, err := FuzzyBytes(blob)
assert.NoError(t, err)
assertHashEqual(t, originalResults[fmt.Sprint(size)], result)
require.Equal(t, originalResults[fmt.Sprint(size)], result)
})
}
}
Expand Down Expand Up @@ -81,7 +74,7 @@ func TestFuzzyHashOutputsTheRightResult(t *testing.T) {

sum := s.Sum(prepend)

assertHashEqual(t, string(append(prepend, expectedResult...)), string(sum))
require.Equal(t, string(append(prepend, expectedResult...)), string(sum))
}

func TestFuzzyBytesOutputsTheRightResult(t *testing.T) {
Expand All @@ -93,7 +86,7 @@ func TestFuzzyBytesOutputsTheRightResult(t *testing.T) {
assert.NoError(t, err)

expectedResult := "96:PuNQHTo6pYrYJWrYJ6N3w53hpYTdhuNQHTo6pYrYJWrYJ6N3w53hpYTP:+QHTrpYrsWrs6N3g3LaGQHTrpYrsWrsa"
assertHashEqual(t, expectedResult, hashResult)
require.Equal(t, expectedResult, hashResult)
}

func TestFuzzyFileOutputsTheRightResult(t *testing.T) {
Expand All @@ -105,7 +98,7 @@ func TestFuzzyFileOutputsTheRightResult(t *testing.T) {
assert.NoError(t, err)

expectedResult := "1536:74peLhFipssVfuInITTTZzMoW0379xy3u:VVFosEfudTj579k3u"
assertHashEqual(t, expectedResult, hashResult)
require.Equal(t, expectedResult, hashResult)

}

Expand All @@ -115,30 +108,30 @@ func TestFuzzyFileOutputsAnErrorForSmallFiles(t *testing.T) {
defer f.Close()

_, err = FuzzyFile(f)
assert.Error(t, err)
require.Error(t, err)
}

func TestFuzzyFilenameOutputsTheRightResult(t *testing.T) {
hashResult, err := FuzzyFilename("ssdeep_results.json")
assert.NoError(t, err)

expectedResult := "1536:74peLhFipssVfuInITTTZzMoW0379xy3u:VVFosEfudTj579k3u"
assertHashEqual(t, expectedResult, hashResult)
require.Equal(t, expectedResult, hashResult)
}

func TestFuzzyFilenameOutputsErrorWhenFileNotExists(t *testing.T) {
_, err := FuzzyFilename("foo.bar")
assert.Error(t, err)
require.Error(t, err)
}

func TestFuzzyBytesWithLenLessThanMinimumOutputsAnError(t *testing.T) {
_, err := FuzzyBytes([]byte{})
assert.Error(t, err)
require.Error(t, err)
}

func TestFuzzyBytesWithOutputsAnError(t *testing.T) {
_, err := FuzzyBytes(make([]byte, 4096))
assert.Error(t, err)
require.Error(t, err)
}

func BenchmarkRollingHash(b *testing.B) {
Expand Down

0 comments on commit 1951c57

Please sign in to comment.