-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved hash.Hash methods and added test
- Loading branch information
Showing
4 changed files
with
93 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters