Skip to content

Commit a750395

Browse files
committed
added email hashing;
raw email shouldn't be sent to server
1 parent 7ca50b5 commit a750395

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// HashingHelper.swift
3+
// UserReportSDK
4+
//
5+
// Created by Maksym Binkovskyi on 27.02.2020.
6+
// Copyright © 2020 UserReport. All rights reserved.
7+
//
8+
import Foundation
9+
import CommonCrypto
10+
11+
// Defines types of hash string outputs available
12+
public enum HashOutputType {
13+
// standard hex string output
14+
case hex
15+
// base 64 encoded string output
16+
case base64
17+
}
18+
19+
// Defines types of hash algorithms available
20+
public enum HashType {
21+
case md5
22+
case sha1
23+
case sha256
24+
25+
var length: Int32 {
26+
switch self {
27+
case .md5: return CC_MD5_DIGEST_LENGTH
28+
case .sha1: return CC_SHA1_DIGEST_LENGTH
29+
case .sha256: return CC_SHA256_DIGEST_LENGTH
30+
}
31+
}
32+
}
33+
34+
internal extension String {
35+
36+
/// Hashing algorithm for hashing a string instance.
37+
///
38+
/// - Parameters:
39+
/// - type: The type of hash to use.
40+
/// - output: The type of output desired, defaults to .hex.
41+
/// - Returns: The requested hash output or nil if failure.
42+
func hashed(_ type: HashType, output: HashOutputType = .hex) -> String? {
43+
44+
// convert string to utf8 encoded data
45+
guard let message = data(using: .utf8) else { return nil }
46+
return message.hashed(type, output: output)
47+
}
48+
}
49+
50+
internal extension Data {
51+
52+
/// Hashing algorithm for hashing a Data instance.
53+
///
54+
/// - Parameters:
55+
/// - type: The type of hash to use.
56+
/// - output: The type of hash output desired, defaults to .hex.
57+
/// - Returns: The requested hash output or nil if failure.
58+
func hashed(_ type: HashType, output: HashOutputType = .hex) -> String? {
59+
60+
// setup data variable to hold hashed value
61+
var digest = Data(count: Int(type.length))
62+
63+
_ = digest.withUnsafeMutableBytes{ digestBytes -> UInt8 in
64+
self.withUnsafeBytes { messageBytes -> UInt8 in
65+
if let mb = messageBytes.baseAddress, let db = digestBytes.bindMemory(to: UInt8.self).baseAddress {
66+
let length = CC_LONG(self.count)
67+
switch type {
68+
case .md5: CC_MD5(mb, length, db)
69+
case .sha1: CC_SHA1(mb, length, db)
70+
case .sha256: CC_SHA256(mb, length, db)
71+
}
72+
}
73+
return 0
74+
}
75+
}
76+
77+
// return the value based on the specified output type.
78+
switch output {
79+
case .hex: return digest.map { String(format: "%02hhx", $0) }.joined()
80+
case .base64: return digest.base64EncodedString()
81+
}
82+
}
83+
}

UserReport/UserReport/Models/User.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ public class User: NSObject {
1111
// MARK: - Property
1212

1313
internal var idfa: String
14-
@objc public var email: String?
14+
private var _email : String?
15+
@objc public var email: String? {
16+
set {
17+
_email = newValue?.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
18+
emailMd5 = _email?.hashed(.md5)
19+
emailSha1 = _email?.hashed(.sha1)
20+
emailSha256 = _email?.hashed(.sha256)
21+
}
22+
get {
23+
return _email
24+
}
25+
}
1526
@objc public var emailMd5: String?
1627
@objc public var emailSha1: String?
1728
@objc public var emailSha256: String?
@@ -42,7 +53,6 @@ public class User: NSObject {
4253
*/
4354
internal func dictObject() -> [String: Any?] {
4455
return ["idfa": self.idfa,
45-
"email": self.email,
4656
"emailMd5": self.emailMd5,
4757
"emailSha1": self.emailSha1,
4858
"emailSha256": self.emailSha256,

UserReport/UserReportSDK.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
11C71E9A1F6966040081D9B4 /* Session.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11C71E991F6966040081D9B4 /* Session.swift */; };
3434
8A4CF05A23D5D2860081C2D8 /* QuarantineRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4CF05923D5D2860081C2D8 /* QuarantineRequest.swift */; };
3535
8A4CF05C23D5D2960081C2D8 /* QuarantineResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A4CF05B23D5D2960081C2D8 /* QuarantineResponse.swift */; };
36+
8AE372B32408057700ED3AE0 /* HashingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AE372B22408057700ED3AE0 /* HashingHelper.swift */; };
3637
/* End PBXBuildFile section */
3738

3839
/* Begin PBXContainerItemProxy section */
@@ -76,6 +77,7 @@
7677
11C71E991F6966040081D9B4 /* Session.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Session.swift; sourceTree = "<group>"; };
7778
8A4CF05923D5D2860081C2D8 /* QuarantineRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuarantineRequest.swift; sourceTree = "<group>"; };
7879
8A4CF05B23D5D2960081C2D8 /* QuarantineResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuarantineResponse.swift; sourceTree = "<group>"; };
80+
8AE372B22408057700ED3AE0 /* HashingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashingHelper.swift; sourceTree = "<group>"; };
7981
/* End PBXFileReference section */
8082

8183
/* Begin PBXFrameworksBuildPhase section */
@@ -123,6 +125,7 @@
123125
110746C21F44921900150955 /* UserReport */ = {
124126
isa = PBXGroup;
125127
children = (
128+
8AE372B424080FD500ED3AE0 /* Helpers */,
126129
1107473F1F46FD1100150955 /* Logger */,
127130
110747441F4AF96A00150955 /* Models */,
128131
110747351F46FA6800150955 /* Network */,
@@ -217,6 +220,14 @@
217220
path = Session;
218221
sourceTree = "<group>";
219222
};
223+
8AE372B424080FD500ED3AE0 /* Helpers */ = {
224+
isa = PBXGroup;
225+
children = (
226+
8AE372B22408057700ED3AE0 /* HashingHelper.swift */,
227+
);
228+
path = Helpers;
229+
sourceTree = "<group>";
230+
};
220231
/* End PBXGroup section */
221232

222233
/* Begin PBXHeadersBuildPhase section */
@@ -329,6 +340,7 @@
329340
isa = PBXSourcesBuildPhase;
330341
buildActionMask = 2147483647;
331342
files = (
343+
8AE372B32408057700ED3AE0 /* HashingHelper.swift in Sources */,
332344
1107473B1F46FA6800150955 /* Result.swift in Sources */,
333345
110747411F46FD1900150955 /* Logger.swift in Sources */,
334346
110747541F4C766E00150955 /* Invitation.swift in Sources */,

0 commit comments

Comments
 (0)