From b6c84a239676e27921ccbbd9daa0c6ceab5044cd Mon Sep 17 00:00:00 2001 From: Ben Baron Date: Tue, 24 Oct 2023 16:40:49 -0500 Subject: [PATCH] Removed unnecessary calls to lowercaseString and clarified int size in hasher --- UnitTests/HasherTests.m | 4 ++-- mParticle-Apple-SDK/Ecommerce/MPProduct.m | 4 ++-- mParticle-Apple-SDK/Ecommerce/MPPromotion.m | 2 +- .../Identity/FilteredMParticleUser.m | 2 +- mParticle-Apple-SDK/Utils/MPIHasher.m | 14 +++++++------- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/UnitTests/HasherTests.m b/UnitTests/HasherTests.m index 139f3ef6..45d19839 100644 --- a/UnitTests/HasherTests.m +++ b/UnitTests/HasherTests.m @@ -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 = @""; @@ -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]; diff --git a/mParticle-Apple-SDK/Ecommerce/MPProduct.m b/mParticle-Apple-SDK/Ecommerce/MPProduct.m index dde149ba..f46326c0 100644 --- a/mParticle-Apple-SDK/Ecommerce/MPProduct.m +++ b/mParticle-Apple-SDK/Ecommerce/MPProduct.m @@ -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]) { @@ -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]) { diff --git a/mParticle-Apple-SDK/Ecommerce/MPPromotion.m b/mParticle-Apple-SDK/Ecommerce/MPPromotion.m index 8e052bbb..12ed6f73 100644 --- a/mParticle-Apple-SDK/Ecommerce/MPPromotion.m +++ b/mParticle-Apple-SDK/Ecommerce/MPPromotion.m @@ -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]) { diff --git a/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m b/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m index 391de3c1..9b4cc0fc 100644 --- a/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m +++ b/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m @@ -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) { diff --git a/mParticle-Apple-SDK/Utils/MPIHasher.m b/mParticle-Apple-SDK/Utils/MPIHasher.m index 1cd7aa49..2780eb97 100644 --- a/mParticle-Apple-SDK/Utils/MPIHasher.m +++ b/mParticle-Apple-SDK/Utils/MPIHasher.m @@ -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++) { @@ -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; @@ -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]; @@ -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 @@ -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]; }