-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix anonymize function and add tests (#177)
- Loading branch information
Showing
2 changed files
with
50 additions
and
4 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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package telemetrysender | ||
|
||
import "crypto/sha256" | ||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
) | ||
|
||
const salt = "jTfYPbjfq9VlhTDfV9lZEyBI29QYqPqn" | ||
|
||
func Anonymize(str string) string { | ||
hasher := sha256.New() | ||
hashBytes := hasher.Sum([]byte(salt + str)) | ||
return string(hashBytes) | ||
return fmt.Sprintf("%x", sha256.Sum256([]byte(salt+str))) | ||
} |
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,45 @@ | ||
package telemetrysender | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"github.com/stretchr/testify/suite" | ||
"testing" | ||
) | ||
|
||
type TelemetrySenderTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (t *TelemetrySenderTestSuite) TestAnonymizeNotContainsOriginalStr() { | ||
someStr := "This is a test string" | ||
hash := Anonymize(someStr) | ||
t.Require().NotContains(someStr, hash) | ||
t.Require().NotContains(hash, someStr) | ||
} | ||
|
||
func (t *TelemetrySenderTestSuite) TestAnonymizeDeterministic() { | ||
someStr := "This is a test string" | ||
hash := Anonymize(someStr) | ||
hash2 := Anonymize(someStr) | ||
t.Require().Equal(hash, hash2) | ||
} | ||
|
||
func (t *TelemetrySenderTestSuite) TestAnonymizeDifferentForDifferentInputs() { | ||
someStr := "This is a test string" | ||
someStr2 := "This is a completely different test string" | ||
hash := Anonymize(someStr) | ||
hash2 := Anonymize(someStr2) | ||
t.Require().NotEqual(hash, hash2) | ||
} | ||
|
||
func (t *TelemetrySenderTestSuite) TestAnonymizeSalted() { | ||
someStr := "This is a test string" | ||
hash := Anonymize(someStr) | ||
hash2 := fmt.Sprintf("%x", sha256.Sum256([]byte(someStr))) | ||
t.Require().NotEqual(hash, hash2) | ||
} | ||
|
||
func TestTelemetrySenderTestSuite(t *testing.T) { | ||
suite.Run(t, &TelemetrySenderTestSuite{}) | ||
} |