Skip to content

Commit

Permalink
[improve] Added event fingerprint stages
Browse files Browse the repository at this point in the history
  • Loading branch information
gboddin committed May 19, 2021
1 parent e948a59 commit b5095cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
12 changes: 9 additions & 3 deletions l9helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

var fingerPrintLength = 32

var hashLength = 4 // fnv.New32
var prefixLength = fingerPrintLength-hashLength
func (event *L9Event) UpdateFingerprint() error {
hasher := fnv.New32()
summaryScanner := bufio.NewScanner(strings.NewReader(event.Summary))
Expand Down Expand Up @@ -39,14 +40,19 @@ func (event *L9Event) UpdateFingerprint() error {
return errors.New("event hashing error")
}
fullHash = append(fullHash, hasher.Sum([]byte{})...)
if len(fullHash) >= fingerPrintLength {
if len(fullHash) >= prefixLength {
break
}
}
// Pad our hash if we're out of data
for len(fullHash) < fingerPrintLength {
for len(fullHash) < prefixLength {
fullHash = append(fullHash, hasher.Sum([]byte{})...)
}
n, err = hasher.Write([]byte(event.Summary))
if err != nil || n != len(event.Summary) {
return errors.New("event hashing error")
}
fullHash = append(fullHash, hasher.Sum([]byte{})...)
// Final check
if len(fullHash) != fingerPrintLength {
return errors.New("event hashing error, blame the author")
Expand Down
31 changes: 31 additions & 0 deletions l9helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package l9format

import (
"strings"
"testing"
)

var TestEventsByFingerprint = map[string]L9Event{
"bc2c0be9cd8fd9c886876c74fe77280819bd01b4a72ac148e2c94bf475b0fd34": {EventSource: "test",Summary: strings.Repeat("test\n",300)},
"bc2c0be9cd8fd9c886876c74fe77280819bd01b4a72ac148e2c94bf431f15032": {EventSource: "test",Summary: strings.Repeat("test\n",299)},
"5a2c67df7b758a1eb5b5f87eed5cadde5abff03ee094719ed1bbd7fe1282b070": {EventSource: "testing",Summary: strings.Repeat("test\n",299)},
"5a2c67df7b758a1eb5b5f87eed5caddeed5caddeed5caddeed5caddefe9efc5e": {EventSource: "testing",Summary: strings.Repeat("test\n",2)},
"5a2c67df7b758a1eb5b5f87eed5cadde5abff03e5abff03e5abff03e988de430": {EventSource: "testing",Summary: strings.Repeat("test\n",3)},
}

func TestL9Event_UpdateFingerprint(t *testing.T) {
for fingerprint, event := range TestEventsByFingerprint {
err := event.UpdateFingerprint()
if err != nil {
t.Error(err)
}
if len(event.EventFingerprint) != fingerPrintLength*2 {
t.Errorf("fingerprint length IS not %d: %s", fingerPrintLength*2, event.EventFingerprint)
}
if fingerprint != event.EventFingerprint {
t.Errorf("fingerprint %s doesn't match base %s", event.EventFingerprint, fingerprint)
}
}

}

0 comments on commit b5095cf

Please sign in to comment.