Skip to content

Commit 7e7a8f2

Browse files
author
Ruben Nine
committed
Implementing Hmac generation using CommonCrypt.
Removing dependency on CryptoSwift.
1 parent bda3426 commit 7e7a8f2

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

Cartfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
github "Alamofire/Alamofire" ~> 4.9
2-
github "krzyzanowskim/CryptoSwift" ~> 1.3
3-

Cartfile.resolved

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
github "Alamofire/Alamofire" "4.9.1"
22
github "AliSoftware/OHHTTPStubs" "8.0.0"
3-
github "krzyzanowskim/CryptoSwift" "1.3.1"

FilestackSDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
45513FFA1F0E9739006DB972 /* APIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45513FF91F0E9739006DB972 /* APIService.swift */; };
4343
4551400D1F0E97E2006DB972 /* NetworkingService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4551400C1F0E97E2006DB972 /* NetworkingService.swift */; };
4444
455266311F1618E1009537D9 /* TransformCropMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 455266301F1618E1009537D9 /* TransformCropMode.swift */; };
45-
4566603324B49C6400C3EAF0 /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4566603224B49C6400C3EAF0 /* CryptoSwift.framework */; };
46-
4566603724B49CB600C3EAF0 /* CryptoSwift.framework in Copy Embedded Frameworks */ = {isa = PBXBuildFile; fileRef = 4566603224B49C6400C3EAF0 /* CryptoSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
4745
4566603924B49FD200C3EAF0 /* DataExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4566603824B49FD200C3EAF0 /* DataExtensionsTests.swift */; };
4846
45690CD222A65FE800285F62 /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 45690CCC22A65FC500285F62 /* Alamofire.framework */; };
4947
45690CD422A6600400285F62 /* OHHTTPStubs.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 45690CCD22A65FC500285F62 /* OHHTTPStubs.framework */; };
@@ -70,6 +68,7 @@
7068
459E61EE1F31BBEF00237EA1 /* SubmitChunkUploadOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 459E61ED1F31BBEF00237EA1 /* SubmitChunkUploadOperation.swift */; };
7169
459F230524ABB373000D6363 /* UploadDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 459F230424ABB373000D6363 /* UploadDescriptor.swift */; };
7270
45A07B3122B91A52003128B0 /* DocumentDetectionTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45A07B3022B91A52003128B0 /* DocumentDetectionTransform.swift */; };
71+
45A6103C24B5C45800225738 /* String+Hmac.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45A6103B24B5C45800225738 /* String+Hmac.swift */; };
7372
45B63B0423279E690040BFBB /* Uploadable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45B63B0323279E690040BFBB /* Uploadable.swift */; };
7473
45B63B0623279EB10040BFBB /* Uploadable+reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45B63B0523279EB10040BFBB /* Uploadable+reader.swift */; };
7574
45B63B0823279ED10040BFBB /* UploadableReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45B63B0723279ED10040BFBB /* UploadableReader.swift */; };
@@ -181,7 +180,6 @@
181180
dstPath = "";
182181
dstSubfolderSpec = 10;
183182
files = (
184-
4566603724B49CB600C3EAF0 /* CryptoSwift.framework in Copy Embedded Frameworks */,
185183
45E07AB022AFC15300CD4573 /* Alamofire.framework in Copy Embedded Frameworks */,
186184
45E07AB222AFC15300CD4573 /* OHHTTPStubs.framework in Copy Embedded Frameworks */,
187185
);
@@ -226,7 +224,6 @@
226224
45513FF91F0E9739006DB972 /* APIService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIService.swift; sourceTree = "<group>"; };
227225
4551400C1F0E97E2006DB972 /* NetworkingService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkingService.swift; sourceTree = "<group>"; };
228226
455266301F1618E1009537D9 /* TransformCropMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransformCropMode.swift; sourceTree = "<group>"; };
229-
4566603224B49C6400C3EAF0 /* CryptoSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CryptoSwift.framework; path = Carthage/Build/iOS/CryptoSwift.framework; sourceTree = "<group>"; };
230227
4566603424B49C9500C3EAF0 /* CryptoKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CryptoKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/CryptoKit.framework; sourceTree = DEVELOPER_DIR; };
231228
4566603824B49FD200C3EAF0 /* DataExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataExtensionsTests.swift; sourceTree = "<group>"; };
232229
45690CCC22A65FC500285F62 /* Alamofire.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Alamofire.framework; path = Carthage/Build/iOS/Alamofire.framework; sourceTree = "<group>"; };
@@ -262,6 +259,7 @@
262259
459E61ED1F31BBEF00237EA1 /* SubmitChunkUploadOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubmitChunkUploadOperation.swift; sourceTree = "<group>"; };
263260
459F230424ABB373000D6363 /* UploadDescriptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadDescriptor.swift; sourceTree = "<group>"; };
264261
45A07B3022B91A52003128B0 /* DocumentDetectionTransform.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentDetectionTransform.swift; sourceTree = "<group>"; };
262+
45A6103B24B5C45800225738 /* String+Hmac.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Hmac.swift"; sourceTree = "<group>"; };
265263
45B63B0323279E690040BFBB /* Uploadable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Uploadable.swift; sourceTree = "<group>"; };
266264
45B63B0523279EB10040BFBB /* Uploadable+reader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Uploadable+reader.swift"; sourceTree = "<group>"; };
267265
45B63B0723279ED10040BFBB /* UploadableReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadableReader.swift; sourceTree = "<group>"; };
@@ -363,7 +361,6 @@
363361
isa = PBXFrameworksBuildPhase;
364362
buildActionMask = 2147483647;
365363
files = (
366-
4566603324B49C6400C3EAF0 /* CryptoSwift.framework in Frameworks */,
367364
45690CD222A65FE800285F62 /* Alamofire.framework in Frameworks */,
368365
);
369366
runOnlyForDeploymentPostprocessing = 0;
@@ -469,6 +466,7 @@
469466
452C639523D0703A0057656C /* StorageOptions+MultipartFormData.swift */,
470467
45D99F2E1F14EDD400CE709F /* UIColor+hexString.swift */,
471468
45B63B0523279EB10040BFBB /* Uploadable+reader.swift */,
469+
45A6103B24B5C45800225738 /* String+Hmac.swift */,
472470
);
473471
path = Extensions;
474472
sourceTree = "<group>";
@@ -653,7 +651,6 @@
653651
children = (
654652
45B830CA1F03BB6D0053742A /* Cartfile */,
655653
45690CCC22A65FC500285F62 /* Alamofire.framework */,
656-
4566603224B49C6400C3EAF0 /* CryptoSwift.framework */,
657654
45690CCD22A65FC500285F62 /* OHHTTPStubs.framework */,
658655
457B17A5232826C80033BDB1 /* Cartfile.private */,
659656
457B17A6232826C80033BDB1 /* Cartfile.resolved */,
@@ -926,6 +923,7 @@
926923
45DC974224ACC0840014ECE3 /* Loggers.swift in Sources */,
927924
45B63B272328078A0040BFBB /* NetworkingServiceWithBaseURL+buildURL.swift in Sources */,
928925
4528C7461F2F24FB00A4BEF6 /* CommitPartUploadOperation.swift in Sources */,
926+
45A6103C24B5C45800225738 /* String+Hmac.swift in Sources */,
929927
BC8832D420D181A700B88EAF /* BlackAndWhiteTransform.swift in Sources */,
930928
4502F7311F1F4C3A000875D6 /* StartUploadOperation.swift in Sources */,
931929
45B63B0423279E690040BFBB /* Uploadable.swift in Sources */,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// String+Hmac.swift
3+
// FilestackSDK
4+
//
5+
// Created by Ruben Nine on 08/07/2020.
6+
// Copyright © 2020 Filestack. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import CommonCrypto
11+
12+
enum HmacAlgorithm {
13+
case sha1, md5, sha256, sha384, sha512, sha224
14+
15+
var algorithm: CCHmacAlgorithm {
16+
switch self {
17+
case .sha1: return CCHmacAlgorithm(kCCHmacAlgSHA1)
18+
case .md5: return CCHmacAlgorithm(kCCHmacAlgMD5)
19+
case .sha256: return CCHmacAlgorithm(kCCHmacAlgSHA256)
20+
case .sha384: return CCHmacAlgorithm(kCCHmacAlgSHA384)
21+
case .sha512: return CCHmacAlgorithm(kCCHmacAlgSHA512)
22+
case .sha224: return CCHmacAlgorithm(kCCHmacAlgSHA224)
23+
}
24+
}
25+
26+
var digestLength: Int32 {
27+
switch self {
28+
case .sha1: return CC_SHA1_DIGEST_LENGTH
29+
case .md5: return CC_MD5_DIGEST_LENGTH
30+
case .sha256: return CC_SHA256_DIGEST_LENGTH
31+
case .sha384: return CC_SHA384_DIGEST_LENGTH
32+
case .sha512: return CC_SHA512_DIGEST_LENGTH
33+
case .sha224: return CC_SHA224_DIGEST_LENGTH
34+
}
35+
}
36+
}
37+
38+
extension String {
39+
func hmac(algorithm: HmacAlgorithm, key: String) -> String {
40+
var digest = [UInt8](repeating: 0, count: Int(algorithm.digestLength))
41+
CCHmac(algorithm.algorithm, key, key.count, self, self.count, &digest)
42+
let data = Data(digest)
43+
44+
return data.map { String(format: "%02hhx", $0) }.joined()
45+
}
46+
}

FilestackSDK/Public/Models/Security.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright © 2017 Filestack. All rights reserved.
77
//
88

9-
import CryptoSwift
109
import Foundation
1110

1211
/// Represents a security object.
@@ -33,10 +32,7 @@ public class Security: NSObject {
3332
@objc public convenience init(policy: Policy, appSecret: String) throws {
3433
let policyJSON: Data = try policy.toJSON()
3534
let encodedPolicy: String = policyJSON.base64EncodedString()
36-
37-
let signature: String = try HMAC(key: appSecret, variant: .sha256)
38-
.authenticate(Array(encodedPolicy.utf8))
39-
.toHexString()
35+
let signature = encodedPolicy.hmac(algorithm: .sha256, key: appSecret)
4036

4137
self.init(encodedPolicy: encodedPolicy, signature: signature)
4238
}

0 commit comments

Comments
 (0)