-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from hearchco/anon-and-hashed-queries
fix(log,cache): anonymous and hashed queries
- Loading branch information
Showing
26 changed files
with
307 additions
and
79 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,18 @@ | ||
package anonymize | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/base64" | ||
) | ||
|
||
func HashToSHA256B64(orig string) string { | ||
// hash string with sha256 which returns binary | ||
hasher := sha256.New() | ||
hasher.Write([]byte(orig)) | ||
hashedBinary := hasher.Sum(nil) | ||
|
||
// encode binary hash to base64 string | ||
hashedString := base64.URLEncoding.EncodeToString(hashedBinary) | ||
|
||
return hashedString | ||
} |
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,24 @@ | ||
package anonymize_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hearchco/hearchco/src/anonymize" | ||
) | ||
|
||
func TestHashToSHA256B64(t *testing.T) { | ||
// original string, expected hash (sha256 returns binary and is encoded to base64) | ||
tests := []testPair{ | ||
{"", "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU="}, | ||
{"banana death", "e8kN64XJ4Icr6Tl9VYrBRj50UJCPlyillODm3vVNk2g="}, | ||
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "LYwvbZeMohcStfbeNsnTH6jpak-l2P-LAYjfuefBcbs="}, | ||
{"Ćao hrčko!! 🐹", "_Y3KWzrx2UkeTp8b--48L6OFgv51JWPlZArjoFOrmbw="}, | ||
} | ||
|
||
for _, test := range tests { | ||
hash := anonymize.HashToSHA256B64(test.orig) | ||
if hash != test.expected { | ||
t.Errorf("HashToSHA256B64(%q) = %q, want %q", test.orig, hash, test.expected) | ||
} | ||
} | ||
} |
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,58 @@ | ||
package anonymize | ||
|
||
import ( | ||
"math/rand" | ||
"sort" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// remove duplicate characters from string | ||
func Deduplicate(orig string) string { | ||
dedupStr := "" | ||
encountered := make(map[rune]bool) | ||
|
||
for _, char := range orig { | ||
if !encountered[char] { | ||
encountered[char] = true | ||
dedupStr += string(char) | ||
} | ||
} | ||
|
||
return dedupStr | ||
} | ||
|
||
// sort string characters lexicographically | ||
func SortString(orig string) string { | ||
// Convert the string to a slice of characters | ||
characters := strings.Split(orig, "") | ||
|
||
// Sort the slice | ||
sort.Strings(characters) | ||
|
||
// Join the sorted slice back into a string | ||
return strings.Join(characters, "") | ||
} | ||
|
||
// shuffle string because deduplicate retains the order of letters | ||
func Shuffle(orig string) string { | ||
inRune := []rune(orig) | ||
|
||
// WARNING: in year 2262, this will break | ||
rng := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
rng.Shuffle(len(inRune), func(i, j int) { | ||
inRune[i], inRune[j] = inRune[j], inRune[i] | ||
}) | ||
|
||
return string(inRune) | ||
} | ||
|
||
// anonymize string | ||
func String(orig string) string { | ||
return Shuffle(Deduplicate(orig)) | ||
} | ||
|
||
// anonymize substring of string | ||
func Substring(orig string, ssToAnon string) string { | ||
return strings.ReplaceAll(orig, ssToAnon, String(ssToAnon)) | ||
} |
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,67 @@ | ||
package anonymize_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hearchco/hearchco/src/anonymize" | ||
) | ||
|
||
func TestDeduplicate(t *testing.T) { | ||
// original string, expected deduplicated string | ||
tests := []testPair{ | ||
{"", ""}, | ||
{"gmail", "gmail"}, | ||
{"banana death", "ban deth"}, | ||
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "Lorem ipsudlta,cngbq.UvxDhfE"}, | ||
} | ||
|
||
for _, test := range tests { | ||
deduplicated := anonymize.Deduplicate(test.orig) | ||
if deduplicated != test.expected { | ||
t.Errorf("deduplicate(%q) = %q, want %q", test.orig, deduplicated, test.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestSortString(t *testing.T) { | ||
// original string, sorted string | ||
tests := []testPair{ | ||
{"", ""}, | ||
{"gmail", "agilm"}, | ||
{"banana death", " aaaabdehnnt"}, | ||
{ | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", | ||
" ,,.Laaaaaaabccccddddddddeeeeeeeeeeeggiiiiiiiiiiilllllmmmmmmnnnnnoooooooooopppqrrrrrrsssssstttttttttuuuuuu", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
sorted := anonymize.SortString(test.orig) | ||
|
||
if sorted != test.expected { | ||
t.Errorf("SortString(%q) = %q, want %q", test.orig, sorted, test.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestShuffle(t *testing.T) { | ||
// original string, sorted string | ||
tests := []testPair{ | ||
{"", ""}, | ||
{"gmail", "agilm"}, | ||
{"banana death", " aaaabdehnnt"}, | ||
{ | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", | ||
" ,,.Laaaaaaabccccddddddddeeeeeeeeeeeggiiiiiiiiiiilllllmmmmmmnnnnnoooooooooopppqrrrrrrsssssstttttttttuuuuuu", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
shuffled := anonymize.Shuffle(test.orig) | ||
shuffledSorted := anonymize.SortString(shuffled) | ||
|
||
if shuffledSorted != test.expected { | ||
t.Errorf("SortString(Shuffle(%q)) = %q, want %q", test.orig, shuffledSorted, test.expected) | ||
} | ||
} | ||
} |
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,6 @@ | ||
package anonymize_test | ||
|
||
type testPair struct { | ||
orig string | ||
expected string | ||
} |
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
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
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
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
Oops, something went wrong.