Skip to content

Commit

Permalink
chore: change event hash code calculation method to sha256 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei authored Sep 15, 2023
1 parent 0786eef commit b20086b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ class AnalyticsClient: AnalyticsClientBehaviour {
}
}
event.setUserAttribute(userAttributes)
let objId = ObjectIdentifier(event)
event.hashCode = objId.hashValue
try eventRecorder.save(event)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import Foundation
private typealias Limit = Event.Limit
typealias JsonObject = [String: Any]

class ClickstreamEvent: AnalyticsPropertiesModel, Hashable {
var hashCode: Int!
class ClickstreamEvent: AnalyticsPropertiesModel {
let eventId: String
let appId: String
let uniqueId: String
Expand Down Expand Up @@ -65,14 +64,9 @@ class ClickstreamEvent: AnalyticsPropertiesModel, Hashable {
attributes[key]
}

func toJson(forHashCode: Bool = false) -> String {
func toJson() -> String {
var event = JsonObject()
if !forHashCode {
if hashCode == nil {
hashCode = hashValue
}
event["hashCode"] = String(format: "%08X", hashCode)
}
event["hashCode"] = ""
event["unique_id"] = uniqueId
event["event_type"] = eventType
event["event_id"] = eventId
Expand Down Expand Up @@ -100,6 +94,8 @@ class ClickstreamEvent: AnalyticsPropertiesModel, Hashable {
event["app_title"] = systemInfo.appTitle
event["user"] = userAttributes
event["attributes"] = getAttributeObject(from: attributes)
let eventJson = getJsonStringFromObject(jsonObject: event)
event["hashCode"] = eventJson.hashCode()
return getJsonStringFromObject(jsonObject: event)
}

Expand Down Expand Up @@ -134,10 +130,6 @@ class ClickstreamEvent: AnalyticsPropertiesModel, Hashable {
return attribute
}

func hash(into hasher: inout Hasher) {
hasher.combine(toJson(forHashCode: true))
}

static func == (lhs: ClickstreamEvent, rhs: ClickstreamEvent) -> Bool {
lhs.toJson() == rhs.toJson()
}
Expand Down
20 changes: 20 additions & 0 deletions Sources/Clickstream/Support/Extension/String+HashCode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import CryptoKit
import Foundation

extension String {
func hashCode() -> String {
if let data = data(using: .utf8) {
let hashed = SHA256.hash(data: data)
let hashString = hashed.compactMap { String(format: "%02x", $0) }.joined()
return String(hashString.prefix(8))
}
return ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ClickstreamEventTest: XCTestCase {
}

func testEventEqualsFail() {
let event1 = clickstreamEvent
let event1 = clickstreamEvent!
let event2 = ClickstreamEvent(eventType: "testEvent",
appId: testAppId,
uniqueId: UUID().uuidString,
Expand Down
30 changes: 30 additions & 0 deletions Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,36 @@ class EventRecorderTest: XCTestCase {
activityTracker.callback?(.runningInBackground)
XCTAssertTrue(eventRecorder.queue.operationCount > 0)
}

func testGetEventHashCodeTwice() {
let eventJson = clickstreamEvent.toJson()
let hashCode1 = eventJson.hashCode()
let hashCode2 = eventJson.hashCode()
XCTAssertEqual(hashCode1, hashCode2)
}

func testEventModified() throws {
let originJson = clickstreamEvent.toJson()
let originJsonData = originJson.data(using: .utf8)!
var jsonObject = try JSONSerialization.jsonObject(with: originJsonData, options: []) as! [String: Any]
let originHashCode = jsonObject["hashCode"] as! String
jsonObject["hashCode"] = ""
jsonObject["event_type"] = "testEvent1"
let jsonWithoutHashCode = clickstreamEvent.getJsonStringFromObject(jsonObject: jsonObject)
let computedHashCode = jsonWithoutHashCode.hashCode()
XCTAssertNotEqual(originHashCode, computedHashCode)
}

func testEventNotModified() throws {
let originJson = clickstreamEvent.toJson()
let originJsonData = originJson.data(using: .utf8)!
var jsonObject = try JSONSerialization.jsonObject(with: originJsonData, options: []) as! [String: Any]
let originHashCode = jsonObject["hashCode"] as! String
jsonObject["hashCode"] = ""
let jsonWithoutHashCode = clickstreamEvent.getJsonStringFromObject(jsonObject: jsonObject)
let computedHashCode = jsonWithoutHashCode.hashCode()
XCTAssertEqual(originHashCode, computedHashCode)
}
}

extension String {
Expand Down

0 comments on commit b20086b

Please sign in to comment.