Skip to content

Commit 4ca2fb1

Browse files
Merge pull request #22 from AppcentMobile/feature/encrypted-requests
🎷 [FEATURE] Encryption added for parameter, query item and header.
2 parents 3350304 + 1fe0c5c commit 4ca2fb1

File tree

9 files changed

+150
-16
lines changed

9 files changed

+150
-16
lines changed

ACMNetworking.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = "ACMNetworking"
3-
spec.version = "1.1.7"
3+
spec.version = "1.1.8"
44
spec.summary = "ACMNetworking iOS Library"
55
spec.description = <<-DESC
66
ACMNetworking is a package that help developers to make requests easily.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ let endpoint = ACMEndpoint()
3939
```bash
4040
network.request(to: endpoint) { (response: ProductResponse) in
4141
print(response)
42+
} onProgress: { model in {
43+
print(model.formattedPercentage())
4244
} onError: { error in
4345
print(error)
4446
}

Sources/ACMNetworking/ACMNetworking.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ public class ACMNetworking: NSObject {
2727
super.init()
2828
}
2929

30-
/// Public destroy function
31-
deinit {
32-
print(ACMNetworkConstants.managerDeinitMessage)
33-
}
34-
3530
/// Cancels the current network request
3631
public func cancel() {
3732
cancelRequestTask()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// ACMEncryptType.swift
3+
//
4+
//
5+
// Created by burak on 28.12.2023.
6+
//
7+
8+
public enum ACMEncryptType {
9+
case plain
10+
case md5
11+
case base64
12+
case sha1
13+
case sha256
14+
case sha384
15+
case sha512
16+
}

Sources/ACMNetworking/Library/Manager/ACMEndpoint.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ public final class ACMEndpoint {
1111
/// Base init
1212
///
1313
/// Init method for creating new object
14-
public init() {}
14+
public init() {
15+
encryption = ACMEncryptionUtils()
16+
}
1517

18+
var encryption: ACMEncryptionUtils?
1619
var mainEndpoint: ACMBaseEndpoint?
1720
var config: ACMPlistModel?
1821
var configOverride: Bool = false
@@ -150,11 +153,17 @@ public final class ACMEndpoint {
150153
/// - Self
151154
public func add(header: ACMHeaderModel) -> Self {
152155
var headerList = [ACMHeaderModel]()
156+
157+
var headerFiltered = header
158+
if let encVal = encryption?.encrypt(value: header.value, type: header.type) as? String {
159+
headerFiltered.value = encVal
160+
}
161+
153162
if let list = headersModel, list.count > 0 {
154163
headerList = list
155-
headerList.append(header)
164+
headerList.append(headerFiltered)
156165
} else {
157-
headerList.append(header)
166+
headerList.append(headerFiltered)
158167
}
159168

160169
headersModel = headerList
@@ -195,11 +204,17 @@ public final class ACMEndpoint {
195204
/// - Self
196205
public func add(queryItem: ACMQueryModel) -> Self {
197206
var queryList = [ACMQueryModel]()
207+
208+
var queryFiltered = queryItem
209+
if let encVal = encryption?.encrypt(value: queryFiltered.value, type: queryItem.type) as? String {
210+
queryFiltered.value = encVal
211+
}
212+
198213
if let list = queryItemsModel, list.count > 0 {
199214
queryList = list
200-
queryList.append(queryItem)
215+
queryList.append(queryFiltered)
201216
} else {
202-
queryList.append(queryItem)
217+
queryList.append(queryFiltered)
203218
}
204219

205220
queryItemsModel = queryList
@@ -228,11 +243,17 @@ public final class ACMEndpoint {
228243
/// - Self
229244
public func add(param: ACMBodyModel) -> Self {
230245
var paramList = [ACMBodyModel]()
246+
247+
var paramFiltered = param
248+
if let encVal = encryption?.encrypt(value: param.value, type: param.type) {
249+
paramFiltered.value = encVal
250+
}
251+
231252
if let list = paramsModel, list.count > 0 {
232253
paramList = list
233-
paramList.append(param)
254+
paramList.append(paramFiltered)
234255
} else {
235-
paramList.append(param)
256+
paramList.append(paramFiltered)
236257
}
237258

238259
paramsModel = paramList

Sources/ACMNetworking/Library/Models/ACMBodyModel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public struct ACMBodyModel {
99
var key: String
1010
var value: Any
11+
var type: ACMEncryptType
1112

1213
/// Public Init function
1314
/// For creating body payload
@@ -16,8 +17,9 @@ public struct ACMBodyModel {
1617
/// - value: Body payload value
1718
/// - Returns
1819
/// - Void
19-
public init(key: String, value: Any) {
20+
public init(key: String, value: Any, type: ACMEncryptType = .plain) {
2021
self.key = key
2122
self.value = value
23+
self.type = type
2224
}
2325
}

Sources/ACMNetworking/Library/Models/ACMHeaderModel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public struct ACMHeaderModel {
99
var field: String
1010
var value: String
11+
var type: ACMEncryptType
1112

1213
/// Public Init function
1314
/// For creating header
@@ -16,8 +17,9 @@ public struct ACMHeaderModel {
1617
/// - value: Header value
1718
/// - Returns
1819
/// - Void
19-
public init(field: String, value: String) {
20+
public init(field: String, value: String, type: ACMEncryptType = .plain) {
2021
self.field = field
2122
self.value = value
23+
self.type = type
2224
}
2325
}

Sources/ACMNetworking/Library/Models/ACMQueryModel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public struct ACMQueryModel {
99
var name: String
1010
var value: String
11+
var type: ACMEncryptType
1112

1213
/// Public Init function
1314
/// For creating query
@@ -16,8 +17,9 @@ public struct ACMQueryModel {
1617
/// - value: Query value
1718
/// - Returns
1819
/// - Void
19-
public init(name: String, value: String) {
20+
public init(name: String, value: String, type: ACMEncryptType = .plain) {
2021
self.name = name
2122
self.value = value
23+
self.type = type
2224
}
2325
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// ACMEncryptionUtils.swift
3+
//
4+
//
5+
// Created by burak on 28.12.2023.
6+
//
7+
8+
import CommonCrypto
9+
import Foundation
10+
11+
final class ACMEncryptionUtils {
12+
func encrypt(value: Any, type: ACMEncryptType) -> Any {
13+
let stringRaw = String(describing: value)
14+
15+
switch type {
16+
case .plain:
17+
return value
18+
case .md5:
19+
return md5(with: stringRaw)
20+
case .base64:
21+
return base64(with: stringRaw)
22+
case .sha1:
23+
return sha1(with: stringRaw)
24+
case .sha256:
25+
return sha256(with: stringRaw)
26+
case .sha384:
27+
return sha384(with: stringRaw)
28+
case .sha512:
29+
return sha512(with: stringRaw)
30+
}
31+
}
32+
33+
private func sha1(with string: String) -> String {
34+
if let data = string.data(using: .utf8) {
35+
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
36+
_ = data.withUnsafeBytes {
37+
CC_SHA1($0.baseAddress, CC_LONG(data.count), &digest)
38+
}
39+
return digest.map { String(format: "%02x", $0) }.joined()
40+
}
41+
return ""
42+
}
43+
44+
private func sha256(with string: String) -> String {
45+
if let data = string.data(using: .utf8) {
46+
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
47+
_ = data.withUnsafeBytes {
48+
CC_SHA256($0.baseAddress, CC_LONG(data.count), &digest)
49+
}
50+
return digest.map { String(format: "%02x", $0) }.joined()
51+
}
52+
return ""
53+
}
54+
55+
private func sha384(with string: String) -> String {
56+
if let data = string.data(using: .utf8) {
57+
var digest = [UInt8](repeating: 0, count: Int(CC_SHA384_DIGEST_LENGTH))
58+
_ = data.withUnsafeBytes {
59+
CC_SHA384($0.baseAddress, CC_LONG(data.count), &digest)
60+
}
61+
return digest.map { String(format: "%02x", $0) }.joined()
62+
}
63+
return ""
64+
}
65+
66+
private func sha512(with string: String) -> String {
67+
if let data = string.data(using: .utf8) {
68+
var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
69+
_ = data.withUnsafeBytes {
70+
CC_SHA512($0.baseAddress, CC_LONG(data.count), &digest)
71+
}
72+
return digest.map { String(format: "%02x", $0) }.joined()
73+
}
74+
return ""
75+
}
76+
77+
private func md5(with string: String) -> String {
78+
if let data = string.data(using: .utf8) {
79+
var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
80+
_ = data.withUnsafeBytes {
81+
CC_MD5($0.baseAddress, CC_LONG(data.count), &digest)
82+
}
83+
return digest.map { String(format: "%02x", $0) }.joined()
84+
}
85+
return ""
86+
}
87+
88+
private func base64(with string: String) -> String {
89+
if let data = string.data(using: .utf8) {
90+
return data.base64EncodedString()
91+
}
92+
return ""
93+
}
94+
}

0 commit comments

Comments
 (0)