From 2f439e980d54d46e17de2606dbf911a7cf0c90b0 Mon Sep 17 00:00:00 2001 From: Ben Baron Date: Tue, 24 Oct 2023 17:38:29 -0500 Subject: [PATCH] Port MPIHasher to Swift --- UnitTests/HasherTests.m | 9 +- UnitTests/MPKitAPITests.m | 7 +- UnitTests/MPStateMachineTests.m | 7 +- mParticle-Apple-SDK.xcodeproj/project.pbxproj | 18 +-- mParticle-Apple-SDK/Ecommerce/MPProduct.m | 1 - mParticle-Apple-SDK/Ecommerce/MPPromotion.m | 2 +- .../Identity/FilteredMParticleUser.m | 2 +- mParticle-Apple-SDK/Include/MPIHasher.h | 20 ---- mParticle-Apple-SDK/Include/mParticle.h | 1 - mParticle-Apple-SDK/Kits/MPEventProjection.mm | 2 +- .../Kits/MPKitConfiguration.mm | 2 +- mParticle-Apple-SDK/MPBackendController.m | 1 + mParticle-Apple-SDK/Utils/MPIHasher.m | 109 ------------------ mParticle-Apple-SDK/Utils/MPIHasher.swift | 105 +++++++++++++++++ mParticle-Apple-SDK/Utils/MPIUserDefaults.m | 2 +- mParticle-Apple-SDK/Utils/MPStateMachine.m | 1 + mParticle-Apple-SDK/mParticle.m | 2 +- 17 files changed, 138 insertions(+), 153 deletions(-) delete mode 100644 mParticle-Apple-SDK/Include/MPIHasher.h delete mode 100644 mParticle-Apple-SDK/Utils/MPIHasher.m create mode 100644 mParticle-Apple-SDK/Utils/MPIHasher.swift diff --git a/UnitTests/HasherTests.m b/UnitTests/HasherTests.m index 45d198398..c8785053c 100644 --- a/UnitTests/HasherTests.m +++ b/UnitTests/HasherTests.m @@ -1,8 +1,13 @@ +#ifndef MPARTICLE_LOCATION_DISABLE +@import mParticle_Apple_SDK; +#else +@import mParticle_Apple_SDK_NoLocation; +#endif + #import #import "MPEnums.h" #import "MPEvent.h" #import "MPBaseTestCase.h" -#import "MPIHasher.h" #import "mparticle.h" #import "MPIConstants.h" @@ -33,7 +38,7 @@ - (void)testHashingString { - (void)testHashingPerformance { [self measureBlock:^{ NSString *referenceString = @"The Quick Brown Fox Jumps Over the Lazy Dog."; - [MPIHasher hashString:referenceString]; + (void)[MPIHasher hashString:referenceString]; }]; } diff --git a/UnitTests/MPKitAPITests.m b/UnitTests/MPKitAPITests.m index 77c24edca..c76a99d4d 100644 --- a/UnitTests/MPKitAPITests.m +++ b/UnitTests/MPKitAPITests.m @@ -1,3 +1,9 @@ +#ifndef MPARTICLE_LOCATION_DISABLE +@import mParticle_Apple_SDK; +#else +@import mParticle_Apple_SDK_NoLocation; +#endif + #import #import "MPKitApi.h" #import "MPKitContainer.h" @@ -9,7 +15,6 @@ #import "MPIConstants.h" #import "FilteredMParticleUser.h" #import "MPBaseTestCase.h" -#import "MPIHasher.h" @interface MPKitContainer () diff --git a/UnitTests/MPStateMachineTests.m b/UnitTests/MPStateMachineTests.m index 28af198ea..037bc213c 100644 --- a/UnitTests/MPStateMachineTests.m +++ b/UnitTests/MPStateMachineTests.m @@ -1,7 +1,12 @@ +#ifndef MPARTICLE_LOCATION_DISABLE +@import mParticle_Apple_SDK; +#else +@import mParticle_Apple_SDK_NoLocation; +#endif + #import #import #import "MPStateMachine.h" -#import "MPIHasher.h" #import "MPConsumerInfo.h" #import "MPLaunchInfo.h" #import "MPBaseTestCase.h" diff --git a/mParticle-Apple-SDK.xcodeproj/project.pbxproj b/mParticle-Apple-SDK.xcodeproj/project.pbxproj index 6379d737c..5b0a17a5f 100644 --- a/mParticle-Apple-SDK.xcodeproj/project.pbxproj +++ b/mParticle-Apple-SDK.xcodeproj/project.pbxproj @@ -76,7 +76,6 @@ 534CD2AC29CE2CF1008452B3 /* mParticle_Apple_SDK_NoLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 53A79DC629CE23F700E7489F /* mParticle_Apple_SDK_NoLocation.framework */; }; 534CD2AD29CE2E8F008452B3 /* MPSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C9229CE019F00E7489F /* MPSwiftTests.swift */; }; 53A79A8429CCCD6400E7489F /* mParticle_Apple_SDK.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 53A79A7929CCCD6400E7489F /* mParticle_Apple_SDK.framework */; }; - 53A79B6529CDFB2000E7489F /* MPIHasher.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79A9629CDFB1E00E7489F /* MPIHasher.m */; }; 53A79B6629CDFB2000E7489F /* MPIdentityApiRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79A9829CDFB1E00E7489F /* MPIdentityApiRequest.m */; }; 53A79B6729CDFB2000E7489F /* MPAliasResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79A9929CDFB1E00E7489F /* MPAliasResponse.m */; }; 53A79B6829CDFB2000E7489F /* MParticleUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79A9A29CDFB1E00E7489F /* MParticleUser.m */; }; @@ -248,7 +247,6 @@ 53A79C5729CDFB4800E7489F /* NSArray+MPCaseInsensitive.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3529CDFB4800E7489F /* NSArray+MPCaseInsensitive.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79C5829CDFB4800E7489F /* MPCommerceEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3629CDFB4800E7489F /* MPCommerceEvent.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79C5929CDFB4800E7489F /* MPAliasResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3729CDFB4800E7489F /* MPAliasResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 53A79C5A29CDFB4800E7489F /* MPIHasher.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3829CDFB4800E7489F /* MPIHasher.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79C5B29CDFB4800E7489F /* MPIdentityApiRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3929CDFB4800E7489F /* MPIdentityApiRequest.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79C5C29CDFB4800E7489F /* MPAliasRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3A29CDFB4800E7489F /* MPAliasRequest.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79C5D29CDFB4800E7489F /* MPDateFormatter.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3B29CDFB4800E7489F /* MPDateFormatter.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -352,7 +350,6 @@ 53A79D1A29CE23F700E7489F /* MPKitExecStatus.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3E29CDFB4800E7489F /* MPKitExecStatus.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79D1B29CE23F700E7489F /* MPEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C2329CDFB4800E7489F /* MPEvent.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79D1C29CE23F700E7489F /* MPListenerProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C2A29CDFB4800E7489F /* MPListenerProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 53A79D1D29CE23F700E7489F /* MPIHasher.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3829CDFB4800E7489F /* MPIHasher.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79D1E29CE23F700E7489F /* MPAliasResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3729CDFB4800E7489F /* MPAliasResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79D1F29CE23F700E7489F /* MPConsentState.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C4029CDFB4800E7489F /* MPConsentState.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79D2029CE23F700E7489F /* MParticleUser.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79C3429CDFB4800E7489F /* MParticleUser.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -446,7 +443,6 @@ 53A79D7C29CE23F700E7489F /* MPLaunchInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2929CDFB1F00E7489F /* MPLaunchInfo.m */; }; 53A79D7D29CE23F700E7489F /* MPPromotion.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B4129CDFB1F00E7489F /* MPPromotion.m */; }; 53A79D7E29CE23F700E7489F /* MPSearchAdsAttribution.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2229CDFB1F00E7489F /* MPSearchAdsAttribution.m */; }; - 53A79D7F29CE23F700E7489F /* MPIHasher.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79A9629CDFB1E00E7489F /* MPIHasher.m */; }; 53A79D8129CE23F700E7489F /* MPIdentityApiRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79A9829CDFB1E00E7489F /* MPIdentityApiRequest.m */; }; 53A79D8229CE23F700E7489F /* MPCommerceEvent.mm in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B4029CDFB1F00E7489F /* MPCommerceEvent.mm */; }; 53A79D8329CE23F700E7489F /* MPConsentSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79AF729CDFB1F00E7489F /* MPConsentSerialization.m */; }; @@ -511,6 +507,8 @@ 53A79DBF29CE23F700E7489F /* MPIdentityApiManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79AA229CDFB1E00E7489F /* MPIdentityApiManager.m */; }; 53B33E8A2A4606CD00CC8A19 /* MPSideloadedKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53B33E892A4606CD00CC8A19 /* MPSideloadedKit.swift */; }; 53B33E8B2A4606CD00CC8A19 /* MPSideloadedKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53B33E892A4606CD00CC8A19 /* MPSideloadedKit.swift */; }; + 53FDD1BD2AE871AF003D5FA1 /* MPIHasher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53FDD1BC2AE871AF003D5FA1 /* MPIHasher.swift */; }; + 53FDD1BE2AE871AF003D5FA1 /* MPIHasher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53FDD1BC2AE871AF003D5FA1 /* MPIHasher.swift */; }; D3724C192AE02AF60074CD67 /* mParticle_Apple_SDK_NoLocation.h in Headers */ = {isa = PBXBuildFile; fileRef = D3724C182AE02AF60074CD67 /* mParticle_Apple_SDK_NoLocation.h */; settings = {ATTRIBUTES = (Public, ); }; }; D3B3E2072AE029AE001AB58C /* mParticle_Apple_SDK.h in Headers */ = {isa = PBXBuildFile; fileRef = D3B3E2062AE028EC001AB58C /* mParticle_Apple_SDK.h */; settings = {ATTRIBUTES = (Public, ); }; }; /* End PBXBuildFile section */ @@ -537,7 +535,6 @@ 534CD2AA29CE2CE1008452B3 /* mParticle-Apple-SDK-NoLocationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "mParticle-Apple-SDK-NoLocationTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 53A79A7929CCCD6400E7489F /* mParticle_Apple_SDK.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = mParticle_Apple_SDK.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 53A79A8329CCCD6400E7489F /* mParticle-Apple-SDKTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "mParticle-Apple-SDKTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; - 53A79A9629CDFB1E00E7489F /* MPIHasher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPIHasher.m; sourceTree = ""; }; 53A79A9829CDFB1E00E7489F /* MPIdentityApiRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPIdentityApiRequest.m; sourceTree = ""; }; 53A79A9929CDFB1E00E7489F /* MPAliasResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPAliasResponse.m; sourceTree = ""; }; 53A79A9A29CDFB1E00E7489F /* MParticleUser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MParticleUser.m; sourceTree = ""; }; @@ -709,7 +706,6 @@ 53A79C3529CDFB4800E7489F /* NSArray+MPCaseInsensitive.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+MPCaseInsensitive.h"; sourceTree = ""; }; 53A79C3629CDFB4800E7489F /* MPCommerceEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPCommerceEvent.h; sourceTree = ""; }; 53A79C3729CDFB4800E7489F /* MPAliasResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPAliasResponse.h; sourceTree = ""; }; - 53A79C3829CDFB4800E7489F /* MPIHasher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPIHasher.h; sourceTree = ""; }; 53A79C3929CDFB4800E7489F /* MPIdentityApiRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPIdentityApiRequest.h; sourceTree = ""; }; 53A79C3A29CDFB4800E7489F /* MPAliasRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPAliasRequest.h; sourceTree = ""; }; 53A79C3B29CDFB4800E7489F /* MPDateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPDateFormatter.h; sourceTree = ""; }; @@ -801,6 +797,7 @@ 53A79CFF29CE23D600E7489F /* mParticle-Apple-SDK-NoLocation.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; name = "mParticle-Apple-SDK-NoLocation.modulemap"; path = "Framework/mParticle-Apple-SDK-NoLocation.modulemap"; sourceTree = ""; }; 53A79DC629CE23F700E7489F /* mParticle_Apple_SDK_NoLocation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = mParticle_Apple_SDK_NoLocation.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 53B33E892A4606CD00CC8A19 /* MPSideloadedKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPSideloadedKit.swift; sourceTree = ""; }; + 53FDD1BC2AE871AF003D5FA1 /* MPIHasher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPIHasher.swift; sourceTree = ""; }; D3724C182AE02AF60074CD67 /* mParticle_Apple_SDK_NoLocation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = mParticle_Apple_SDK_NoLocation.h; sourceTree = ""; }; D3B3E2062AE028EC001AB58C /* mParticle_Apple_SDK.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = mParticle_Apple_SDK.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -1016,7 +1013,6 @@ 53A79B0129CDFB1F00E7489F /* Utils */ = { isa = PBXGroup; children = ( - 53A79A9629CDFB1E00E7489F /* MPIHasher.m */, 53A79B0229CDFB1F00E7489F /* MPMessageBuilder.m */, 53A79B0329CDFB1F00E7489F /* MPStateMachine.m */, 53A79B0429CDFB1F00E7489F /* MPIUserDefaults.h */, @@ -1057,6 +1053,7 @@ 53A79B2B29CDFB1F00E7489F /* MPDevice.h */, 53A79B2C29CDFB1F00E7489F /* MPBracket.h */, 53A79B2D29CDFB1F00E7489F /* MPUserIdentityChange.h */, + 53FDD1BC2AE871AF003D5FA1 /* MPIHasher.swift */, ); path = Utils; sourceTree = ""; @@ -1189,7 +1186,6 @@ 53A79C3529CDFB4800E7489F /* NSArray+MPCaseInsensitive.h */, 53A79C3629CDFB4800E7489F /* MPCommerceEvent.h */, 53A79C3729CDFB4800E7489F /* MPAliasResponse.h */, - 53A79C3829CDFB4800E7489F /* MPIHasher.h */, 53A79C3929CDFB4800E7489F /* MPIdentityApiRequest.h */, 53A79C3A29CDFB4800E7489F /* MPAliasRequest.h */, 53A79C3B29CDFB4800E7489F /* MPDateFormatter.h */, @@ -1345,7 +1341,6 @@ 53A79C6029CDFB4800E7489F /* MPKitExecStatus.h in Headers */, 53A79C4529CDFB4800E7489F /* MPEvent.h in Headers */, 53A79C4C29CDFB4800E7489F /* MPListenerProtocol.h in Headers */, - 53A79C5A29CDFB4800E7489F /* MPIHasher.h in Headers */, 53A79C5929CDFB4800E7489F /* MPAliasResponse.h in Headers */, 53A79C6229CDFB4800E7489F /* MPConsentState.h in Headers */, 53A79C5629CDFB4800E7489F /* MParticleUser.h in Headers */, @@ -1452,7 +1447,6 @@ 53A79D1A29CE23F700E7489F /* MPKitExecStatus.h in Headers */, 53A79D1B29CE23F700E7489F /* MPEvent.h in Headers */, 53A79D1C29CE23F700E7489F /* MPListenerProtocol.h in Headers */, - 53A79D1D29CE23F700E7489F /* MPIHasher.h in Headers */, 53A79D1E29CE23F700E7489F /* MPAliasResponse.h in Headers */, 53A79D1F29CE23F700E7489F /* MPConsentState.h in Headers */, 53A79D2029CE23F700E7489F /* MParticleUser.h in Headers */, @@ -1772,7 +1766,6 @@ 53A79BEE29CDFB2000E7489F /* MPLaunchInfo.m in Sources */, 53A79C0229CDFB2100E7489F /* MPPromotion.m in Sources */, 53A79BE729CDFB2000E7489F /* MPSearchAdsAttribution.m in Sources */, - 53A79B6529CDFB2000E7489F /* MPIHasher.m in Sources */, 53A79B6629CDFB2000E7489F /* MPIdentityApiRequest.m in Sources */, 53A79C0129CDFB2100E7489F /* MPCommerceEvent.mm in Sources */, 53A79BBF29CDFB2000E7489F /* MPConsentSerialization.m in Sources */, @@ -1826,6 +1819,7 @@ 53A79BC329CDFB2000E7489F /* MPGDPRConsent.m in Sources */, 53A79C0429CDFB2100E7489F /* MPCommerceEventInstruction.m in Sources */, 53A79B9029CDFB2000E7489F /* MPDataModelAbstract.m in Sources */, + 53FDD1BD2AE871AF003D5FA1 /* MPIHasher.swift in Sources */, 53A79BBD29CDFB2000E7489F /* MPBackendController.m in Sources */, 53A79C1B29CDFB2100E7489F /* MPForwardQueueItem.m in Sources */, 53A79C1629CDFB2100E7489F /* MPEventProjection.mm in Sources */, @@ -1935,7 +1929,6 @@ 53A79D7D29CE23F700E7489F /* MPPromotion.m in Sources */, 534CD25C29CE2877008452B3 /* NSNumber+MPFormatter.swift in Sources */, 53A79D7E29CE23F700E7489F /* MPSearchAdsAttribution.m in Sources */, - 53A79D7F29CE23F700E7489F /* MPIHasher.m in Sources */, 53A79D8129CE23F700E7489F /* MPIdentityApiRequest.m in Sources */, 53A79D8229CE23F700E7489F /* MPCommerceEvent.mm in Sources */, 53A79D8329CE23F700E7489F /* MPConsentSerialization.m in Sources */, @@ -1989,6 +1982,7 @@ 53A79DB229CE23F700E7489F /* MPGDPRConsent.m in Sources */, 53A79DB329CE23F700E7489F /* MPCommerceEventInstruction.m in Sources */, 53A79DB429CE23F700E7489F /* MPDataModelAbstract.m in Sources */, + 53FDD1BE2AE871AF003D5FA1 /* MPIHasher.swift in Sources */, 53A79DB529CE23F700E7489F /* MPBackendController.m in Sources */, 53A79DB629CE23F700E7489F /* MPForwardQueueItem.m in Sources */, 53A79DB729CE23F700E7489F /* MPEventProjection.mm in Sources */, diff --git a/mParticle-Apple-SDK/Ecommerce/MPProduct.m b/mParticle-Apple-SDK/Ecommerce/MPProduct.m index f46326c0e..890db467e 100644 --- a/mParticle-Apple-SDK/Ecommerce/MPProduct.m +++ b/mParticle-Apple-SDK/Ecommerce/MPProduct.m @@ -1,7 +1,6 @@ #import "MPProduct.h" #import "Swift.h" #import "MPIConstants.h" -#import "MPIHasher.h" #import "NSDictionary+MPCaseInsensitive.h" #import "MPILogger.h" #import "mParticle.h" diff --git a/mParticle-Apple-SDK/Ecommerce/MPPromotion.m b/mParticle-Apple-SDK/Ecommerce/MPPromotion.m index 12ed6f732..f747ea4c6 100644 --- a/mParticle-Apple-SDK/Ecommerce/MPPromotion.m +++ b/mParticle-Apple-SDK/Ecommerce/MPPromotion.m @@ -1,7 +1,7 @@ #import "MPPromotion.h" #import "MPIConstants.h" -#import "MPIHasher.h" #import "NSDictionary+MPCaseInsensitive.h" +#import "Swift.h" // Internal keys NSString *const kMPPMAction = @"an"; diff --git a/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m b/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m index 9b4cc0fce..b59473f20 100644 --- a/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m +++ b/mParticle-Apple-SDK/Identity/FilteredMParticleUser.m @@ -2,8 +2,8 @@ #import "mParticle.h" #import "MParticleUser.h" #import "MPKitConfiguration.h" -#import "MPIHasher.h" #import "MPDataPlanFilter.h" +#import "Swift.h" @interface MParticle () diff --git a/mParticle-Apple-SDK/Include/MPIHasher.h b/mParticle-Apple-SDK/Include/MPIHasher.h deleted file mode 100644 index 4c5cc1dd7..000000000 --- a/mParticle-Apple-SDK/Include/MPIHasher.h +++ /dev/null @@ -1,20 +0,0 @@ -#import -#import "MPEnums.h" - -@interface MPIHasher : NSObject - -+ (uint64_t)hashFNV1a:(NSData *)data; -+ (NSString *)hashString:(NSString *)stringToHash; -+ (NSString *)hashStringUTF16:(NSString *)stringToHash; -+ (NSString *)hashEventType:(MPEventType)eventType; -+ (MPEventType)eventTypeForHash:(NSString *)hashString; -+ (NSString *)hashEventType:(MPEventType)eventType eventName:(NSString *)eventName isLogScreen:(BOOL)isLogScreen; -+ (NSString *)hashEventAttributeKey:(MPEventType)eventType eventName:(NSString *)eventName customAttributeName:(NSString *)customAttributeName isLogScreen:(BOOL)isLogScreen; -+ (NSString *)hashUserAttributeKey:(NSString *)userAttributeKey; -+ (NSString *)hashUserAttributeValue:(NSString *)userAttributeValue; -+ (NSString *)hashUserIdentity:(MPUserIdentity)userIdentity; -+ (NSString *)hashConsentPurpose:(NSString *)regulationPrefix purpose:(NSString *)purpose; -+ (NSString *)hashCommerceEventAttribute:(MPEventType)commerceEventType key:(NSString *)key; -+ (NSString *)hashTriggerEventName:(NSString *)eventName eventType:(NSString *)eventType; - -@end diff --git a/mParticle-Apple-SDK/Include/mParticle.h b/mParticle-Apple-SDK/Include/mParticle.h index 5fdb86554..eeb53d868 100644 --- a/mParticle-Apple-SDK/Include/mParticle.h +++ b/mParticle-Apple-SDK/Include/mParticle.h @@ -6,7 +6,6 @@ #import "MPEvent.h" #import "MPExtensionProtocol.h" #import -#import "MPIHasher.h" #import "MPKitExecStatus.h" #import "MPKitRegister.h" #import "MPProduct.h" diff --git a/mParticle-Apple-SDK/Kits/MPEventProjection.mm b/mParticle-Apple-SDK/Kits/MPEventProjection.mm index 1cf9f8a3c..e84b029d8 100644 --- a/mParticle-Apple-SDK/Kits/MPEventProjection.mm +++ b/mParticle-Apple-SDK/Kits/MPEventProjection.mm @@ -1,6 +1,6 @@ #import "MPEventProjection.h" #import "MPAttributeProjection.h" -#import "MPIHasher.h" +#import "Swift.h" #import @implementation MPProjectionMatch diff --git a/mParticle-Apple-SDK/Kits/MPKitConfiguration.mm b/mParticle-Apple-SDK/Kits/MPKitConfiguration.mm index a7e13af3e..9707f71ff 100644 --- a/mParticle-Apple-SDK/Kits/MPKitConfiguration.mm +++ b/mParticle-Apple-SDK/Kits/MPKitConfiguration.mm @@ -1,5 +1,4 @@ #import "MPKitConfiguration.h" -#import "MPIHasher.h" #import "MPIConstants.h" #import "MPEventProjection.h" #import "MPStateMachine.h" @@ -7,6 +6,7 @@ #import "MPConsentSerialization.h" #import "mParticle.h" #import "MPEnums.h" +#import "Swift.h" #import @interface MPKitConfiguration() diff --git a/mParticle-Apple-SDK/MPBackendController.m b/mParticle-Apple-SDK/MPBackendController.m index 997491387..9c95c0f7c 100644 --- a/mParticle-Apple-SDK/MPBackendController.m +++ b/mParticle-Apple-SDK/MPBackendController.m @@ -31,6 +31,7 @@ #import "MPListenerController.h" #import "MParticleWebView.h" #import "MPDevice.h" +#import "Swift.h" #if TARGET_OS_IOS == 1 #ifndef MPARTICLE_LOCATION_DISABLE diff --git a/mParticle-Apple-SDK/Utils/MPIHasher.m b/mParticle-Apple-SDK/Utils/MPIHasher.m deleted file mode 100644 index 2780eb97e..000000000 --- a/mParticle-Apple-SDK/Utils/MPIHasher.m +++ /dev/null @@ -1,109 +0,0 @@ -#import "MPIHasher.h" - -@implementation MPIHasher - -+ (uint64_t)hashFNV1a:(NSData *)data { - // FNV-1a hashing - uint64_t rampHash = 0xcbf29ce484222325; - const uint8_t *bytes = (const uint8_t *)data.bytes; - NSUInteger length = data.length; - - for (int i = 0; i < length; i++) { - rampHash = (rampHash ^ bytes[i]) * 0x100000001B3; - } - return rampHash; -} - -+ (NSString *)hashString:(NSString *)stringToHash { - if (stringToHash.length == 0) { - return @""; - } - - NSString *lowercaseStringToHash = stringToHash.lowercaseString; - NSData *dataToHash = [lowercaseStringToHash dataUsingEncoding:NSUTF8StringEncoding]; - const uint8_t *bytes = (const uint8_t *)dataToHash.bytes; - NSUInteger length = dataToHash.length; - - int32_t hash = 0; - for (int i = 0; i < length; i++) { - uint8_t character = bytes[i]; - hash = ((hash << 5) - hash) + character; - } - - return [NSString stringWithFormat:@"%d", hash]; -} - -+ (NSString *)hashStringUTF16:(NSString *)stringToHash { - NSData *data = [stringToHash dataUsingEncoding:NSUTF16LittleEndianStringEncoding]; - int64_t hash = [self hashFNV1a:data]; - NSString *result = @(hash).stringValue; - return result; -} - -+ (NSString *)hashEventType:(MPEventType)eventType { - return [self hashString:[@(eventType) stringValue]]; -} - -+ (MPEventType)eventTypeForHash:(NSString *)hashString { - for (int i = 1; i <= MPEventTypeImpression; ++i) { - if ([hashString isEqualToString: [MPIHasher hashEventType: (MPEventType)i]]) { - return (MPEventType)i; - } - } - - return MPEventTypeOther; -} - -+ (NSString *)hashEventType:(MPEventType)eventType eventName:(NSString *)eventName isLogScreen:(BOOL)isLogScreen { - NSString *stringToBeHashed; - if (isLogScreen) { - stringToBeHashed = [NSString stringWithFormat:@"%@%@", @"0", eventName]; - - } else { - stringToBeHashed = [NSString stringWithFormat:@"%@%@", [@(eventType) stringValue], eventName]; - } - - return [self hashString:stringToBeHashed]; -} - -+ (NSString *)hashEventAttributeKey:(MPEventType)eventType eventName:(NSString *)eventName customAttributeName:(NSString *)customAttributeName isLogScreen:(BOOL)isLogScreen { - NSString *stringToBeHashed; - if (isLogScreen) { - stringToBeHashed = [NSString stringWithFormat:@"%@%@%@", @"0", eventName, customAttributeName]; - - } else { - stringToBeHashed = [NSString stringWithFormat:@"%@%@%@", [@(eventType) stringValue], eventName, customAttributeName]; - } - return [self hashString:stringToBeHashed]; -} - -+ (NSString *)hashUserAttributeKey:(NSString *)userAttributeKey { - return [self hashString:userAttributeKey]; -} - -+ (NSString *)hashUserAttributeValue:(NSString *)userAttributeValue { - return[self hashString:userAttributeValue]; -} - - // User Identities are not actually hashed, this method is named this way to - // be consistent with the filter class. UserIdentityType is also a number -+ (NSString *)hashUserIdentity:(MPUserIdentity)userIdentity { - return [[NSString alloc] initWithFormat:@"%lu", (unsigned long)userIdentity]; -} - -+ (NSString *)hashConsentPurpose:(NSString *)regulationPrefix purpose:(NSString *)purpose { - NSString *stringToBeHashed = [NSString stringWithFormat:@"%@%@", regulationPrefix, purpose]; - return [self hashString:stringToBeHashed]; -} - -+ (NSString *)hashCommerceEventAttribute:(MPEventType)commerceEventType key:(NSString *)key { - NSString *stringToBeHashed = [NSString stringWithFormat:@"%@%@", [@(commerceEventType) stringValue], key]; - return [self hashString:stringToBeHashed]; -} - -+ (NSString *)hashTriggerEventName:(NSString *)eventName eventType:(NSString *)eventType { - NSString *stringToBeHashed = [NSString stringWithFormat:@"%@%@", eventName, eventType]; - return [self hashString:stringToBeHashed]; -} - -@end diff --git a/mParticle-Apple-SDK/Utils/MPIHasher.swift b/mParticle-Apple-SDK/Utils/MPIHasher.swift new file mode 100644 index 000000000..961b49e98 --- /dev/null +++ b/mParticle-Apple-SDK/Utils/MPIHasher.swift @@ -0,0 +1,105 @@ +// +// MPHasher.swift +// mParticle-Apple-SDK +// +// Created by Ben Baron on 10/24/23. +// + +import Foundation + +@objc public class MPIHasher : NSObject { + + @objc public class func hashFNV1a(_ data: Data) -> Int64 { + var rampHash: UInt64 = 0xcbf29ce484222325 + + for byte in data { + rampHash = (rampHash ^ UInt64(byte)) &* 0x100000001B3 + } + return Int64(bitPattern: rampHash) + } + + @objc public class func hashString(_ stringToHash: String) -> String { + if stringToHash.isEmpty { + return ""; + } + + let lowercaseStringToHash = stringToHash.lowercased() + guard let dataToHash = lowercaseStringToHash.data(using: .utf8) else { + return "" + } + + var hash: Int32 = 0 + for byte in dataToHash { + hash = ((hash << 5) &- hash) + Int32(byte); + } + + return String(hash) + } + + @objc public class func hashStringUTF16(_ stringToHash: String) -> String { + guard let data = stringToHash.data(using: .utf16LittleEndian) else { + return "" + } + let hash = hashFNV1a(data) + return String(hash) + } + + @objc public class func hashEventType(_ eventType: MPEventType) -> String { + return hashString(String(eventType.rawValue)) + } + + @objc public class func eventType(forHash hashString: String) -> MPEventType { + for i in 1...MPEventType.impression.rawValue { + if let eventType = MPEventType(rawValue: i), hashString == hashEventType(eventType) { + return eventType + } + } + return .other + } + + @objc public class func hashEventType(_ eventType: MPEventType, eventName: String, isLogScreen: Bool) -> String { + let stringToBeHashed: String + if isLogScreen { + stringToBeHashed = "0\(eventName)" + } else { + stringToBeHashed = "\(eventType.rawValue)\(eventName)" + } + return hashString(stringToBeHashed) + } + + @objc public class func hashEventAttributeKey(_ eventType: MPEventType, eventName: String, customAttributeName: String, isLogScreen: Bool) -> String { + let stringToBeHashed: String + if isLogScreen { + stringToBeHashed = "0\(eventName)\(customAttributeName)" + } else { + stringToBeHashed = "\(eventType.rawValue)\(eventName)\(customAttributeName)" + } + return hashString(stringToBeHashed) + } + + @objc public class func hashUserAttributeKey(_ userAttributeKey: String) -> String { + return hashString(userAttributeKey) + } + + @objc public class func hashUserAttributeValue(_ userAttributeValue: String) -> String { + return hashString(userAttributeValue) + } + + // User Identities are not actually hashed, this method is named this way to + // be consistent with the filter class. UserIdentityType is also a number + @objc public class func hashUserIdentity(_ userIdentity: MPUserIdentity) -> String { + return String(userIdentity.rawValue) + } + + @objc public class func hashConsentPurpose(_ regulationPrefix: String, purpose: String) -> String { + return hashString("\(regulationPrefix)\(purpose)") + } + + @objc public class func hashCommerceEventAttribute(_ commerceEventType: MPEventType, key: String) -> String { + return hashString("\(commerceEventType.rawValue)\(key)") + } + + @objc public class func hashTriggerEventName(_ eventName: String, eventType: String) -> String { + return hashString("\(eventName)\(eventType)") + } +} diff --git a/mParticle-Apple-SDK/Utils/MPIUserDefaults.m b/mParticle-Apple-SDK/Utils/MPIUserDefaults.m index 90f01d17c..8741e5c7f 100644 --- a/mParticle-Apple-SDK/Utils/MPIUserDefaults.m +++ b/mParticle-Apple-SDK/Utils/MPIUserDefaults.m @@ -7,7 +7,7 @@ #import "MPArchivist.h" #import "MPStateMachine.h" #import "MPKitContainer.h" -#import "MPIHasher.h" +#import "Swift.h" @interface MParticle () diff --git a/mParticle-Apple-SDK/Utils/MPStateMachine.m b/mParticle-Apple-SDK/Utils/MPStateMachine.m index 66a414e34..203dcaa0b 100644 --- a/mParticle-Apple-SDK/Utils/MPStateMachine.m +++ b/mParticle-Apple-SDK/Utils/MPStateMachine.m @@ -18,6 +18,7 @@ #import #import "MPForwardQueueParameters.h" #import "MPDataPlanFilter.h" +#import "Swift.h" #if TARGET_OS_IOS == 1 #ifndef MPARTICLE_LOCATION_DISABLE diff --git a/mParticle-Apple-SDK/mParticle.m b/mParticle-Apple-SDK/mParticle.m index ddc2d57d7..c5999683e 100644 --- a/mParticle-Apple-SDK/mParticle.m +++ b/mParticle-Apple-SDK/mParticle.m @@ -19,11 +19,11 @@ #import "MPIUserDefaults.h" #import "MPConvertJS.h" #import "MPIdentityApi.h" -#import "MPIHasher.h" #import "MPApplication.h" #import "MParticleWebView.h" #import "MPDataPlanFilter.h" #import "MPResponseConfig.h" +#import "Swift.h" #if TARGET_OS_IOS == 1 #ifndef MPARTICLE_LOCATION_DISABLE