Skip to content

Commit

Permalink
Removed unnecessary calls to lowercaseString and clarified int size i…
Browse files Browse the repository at this point in the history
…n hasher
  • Loading branch information
einsteinx2 committed Oct 24, 2023
1 parent de273f9 commit b6c84a2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions UnitTests/HasherTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (void)tearDown {

- (void)testHashingString {
NSString *referenceString = @"The Quick Brown Fox Jumps Over the Lazy Dog.";
NSString *hashedString = [MPIHasher hashString:referenceString.lowercaseString];
NSString *hashedString = [MPIHasher hashString:referenceString];
XCTAssertEqualObjects(hashedString, @"-142870245", @"Hasher is not hashing strings properly.");

referenceString = @"";
Expand Down Expand Up @@ -379,7 +379,7 @@ - (void)testHashDifferences {
NSString *key = @"an_extra_key";
commerceEvent.customAttributes = @{key: @"an_extra_value"}; // A commerce event may contain custom key/value pairs

NSString *attributeTohash = [[@(commerceEvent.type) stringValue] stringByAppendingString:key.lowercaseString];
NSString *attributeTohash = [[@(commerceEvent.type) stringValue] stringByAppendingString:key];
int hashValueOldInt = [[MPIHasher hashString:attributeTohash] intValue];

NSString *hashValueNewString = [MPIHasher hashCommerceEventAttribute:commerceEvent.type key:key];
Expand Down
4 changes: 2 additions & 2 deletions mParticle-Apple-SDK/Ecommerce/MPProduct.m
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ - (MPProduct *)copyMatchingHashedProperties:(NSDictionary *)hashedMap {
NSNumber *const zero = @0;

[_beautifiedAttributes enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
hashedKey = [MPIHasher hashString:key.lowercaseString];
hashedKey = [MPIHasher hashString:key];
hashedValue = hashedMap[hashedKey];

if ([hashedValue isEqualToNumber:zero]) {
Expand All @@ -314,7 +314,7 @@ - (MPProduct *)copyMatchingHashedProperties:(NSDictionary *)hashedMap {
}];

[_userDefinedAttributes enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
hashedKey = [MPIHasher hashString:key.lowercaseString];
hashedKey = [MPIHasher hashString:key];
hashedValue = hashedMap[hashedKey];

if ([hashedValue isEqualToNumber:zero]) {
Expand Down
2 changes: 1 addition & 1 deletion mParticle-Apple-SDK/Ecommerce/MPPromotion.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ - (MPPromotion *)copyMatchingHashedProperties:(NSDictionary *)hashedMap {
NSNumber *const zero = @0;

[_beautifiedAttributes enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
NSString *hashedKey = [MPIHasher hashString:key.lowercaseString];
NSString *hashedKey = [MPIHasher hashString:key];
id hashedValue = hashedMap[hashedKey];

if ([hashedValue isEqualToNumber:zero]) {
Expand Down
2 changes: 1 addition & 1 deletion mParticle-Apple-SDK/Identity/FilteredMParticleUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ -(BOOL)isLoggedIn {

for (NSString* key in unfilteredUserAttributes) {
id value = [unfilteredUserAttributes objectForKey:key];
NSString *hashKey = [MPIHasher hashString:[key lowercaseString]];
NSString *hashKey = [MPIHasher hashString:key];
BOOL shouldFilter = NO;

if (self.kitConfiguration) {
Expand Down
14 changes: 7 additions & 7 deletions mParticle-Apple-SDK/Utils/MPIHasher.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ @implementation MPIHasher
+ (uint64_t)hashFNV1a:(NSData *)data {
// FNV-1a hashing
uint64_t rampHash = 0xcbf29ce484222325;
const char *bytes = (const char *)data.bytes;
const uint8_t *bytes = (const uint8_t *)data.bytes;
NSUInteger length = data.length;

for (int i = 0; i < length; i++) {
Expand All @@ -21,7 +21,7 @@ + (NSString *)hashString:(NSString *)stringToHash {

NSString *lowercaseStringToHash = stringToHash.lowercaseString;
NSData *dataToHash = [lowercaseStringToHash dataUsingEncoding:NSUTF8StringEncoding];
const char *bytes = (const char *)dataToHash.bytes;
const uint8_t *bytes = (const uint8_t *)dataToHash.bytes;
NSUInteger length = dataToHash.length;

int32_t hash = 0;
Expand Down Expand Up @@ -57,10 +57,10 @@ + (MPEventType)eventTypeForHash:(NSString *)hashString {
+ (NSString *)hashEventType:(MPEventType)eventType eventName:(NSString *)eventName isLogScreen:(BOOL)isLogScreen {
NSString *stringToBeHashed;
if (isLogScreen) {
stringToBeHashed = [NSString stringWithFormat:@"%@%@", @"0", [eventName lowercaseString]];
stringToBeHashed = [NSString stringWithFormat:@"%@%@", @"0", eventName];

} else {
stringToBeHashed = [NSString stringWithFormat:@"%@%@", [@(eventType) stringValue], [eventName lowercaseString]];
stringToBeHashed = [NSString stringWithFormat:@"%@%@", [@(eventType) stringValue], eventName];
}

return [self hashString:stringToBeHashed];
Expand All @@ -78,11 +78,11 @@ + (NSString *)hashEventAttributeKey:(MPEventType)eventType eventName:(NSString *
}

+ (NSString *)hashUserAttributeKey:(NSString *)userAttributeKey {
return [self hashString:userAttributeKey.lowercaseString];
return [self hashString:userAttributeKey];
}

+ (NSString *)hashUserAttributeValue:(NSString *)userAttributeValue {
return[self hashString:userAttributeValue.lowercaseString];
return[self hashString:userAttributeValue];
}

// User Identities are not actually hashed, this method is named this way to
Expand All @@ -92,7 +92,7 @@ + (NSString *)hashUserIdentity:(MPUserIdentity)userIdentity {
}

+ (NSString *)hashConsentPurpose:(NSString *)regulationPrefix purpose:(NSString *)purpose {
NSString *stringToBeHashed = [NSString stringWithFormat:@"%@%@", regulationPrefix, [purpose lowercaseString]];
NSString *stringToBeHashed = [NSString stringWithFormat:@"%@%@", regulationPrefix, purpose];
return [self hashString:stringToBeHashed];
}

Expand Down

0 comments on commit b6c84a2

Please sign in to comment.