Skip to content

Commit

Permalink
Hashed SDK Keys to App IDs (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
sroyal-statsig authored Nov 3, 2023
1 parent 88c3762 commit c6024de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type downloadConfigSpecResponse struct {
IDLists map[string]bool `json:"id_lists"`
DiagnosticsSampleRates map[string]int `json:"diagnostics"`
SDKKeysToAppID map[string]string `json:"sdk_keys_to_app_ids,omitempty"`
HashedSDKKeysToAppID map[string]string `json:"hashed_sdk_keys_to_app_ids,omitempty"`
}

type downloadConfigsInput struct {
Expand All @@ -97,6 +98,7 @@ type store struct {
layerConfigs map[string]configSpec
experimentToLayer map[string]string
sdkKeysToAppID map[string]string
hashedSDKKeysToAppID map[string]string
idLists map[string]*idList
lastSyncTime int64
initialSyncTime int64
Expand Down Expand Up @@ -229,6 +231,9 @@ func (s *store) getExperimentLayer(experimentName string) (string, bool) {
func (s *store) getAppIDForSDKKey(clientKey string) (string, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if appId, ok := s.hashedSDKKeysToAppID[getDJB2Hash(clientKey)]; ok {
return appId, ok
}
appId, ok := s.sdkKeysToAppID[clientKey]
return appId, ok
}
Expand Down Expand Up @@ -365,6 +370,7 @@ func (s *store) setConfigSpecs(specs downloadConfigSpecResponse) bool {
s.layerConfigs = newLayers
s.experimentToLayer = newExperimentToLayer
s.sdkKeysToAppID = specs.SDKKeysToAppID
s.hashedSDKKeysToAppID = specs.HashedSDKKeysToAppID
s.lastSyncTime = specs.Time
s.mu.Unlock()
return true
Expand Down
10 changes: 10 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func getHash(key string) []byte {
return hasher.Sum(nil)
}

func getDJB2Hash(key string) string {
hash := uint64(0)
bytes := []byte(key)
for _, b := range bytes {
hash = ((hash << 5) - hash) + uint64(b)
hash = hash & ((1<<32) - 1);
}
return strconv.FormatUint(hash, 10)
}

func getHashUint64Encoding(key string) uint64 {
hash := getHash(key)
return binary.BigEndian.Uint64(hash)
Expand Down

0 comments on commit c6024de

Please sign in to comment.