Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use Swift CommonCrypto module to calculate md5 hashes #22

Merged
merged 4 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/ImgixClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//

import Foundation
import var CommonCrypto.CC_MD5_DIGEST_LENGTH
import func CommonCrypto.CC_MD5
import typealias CommonCrypto.CC_LONG

@objc open class ImgixClient: NSObject {
@objc static public let VERSION = "1.0.0"
Expand Down Expand Up @@ -150,7 +153,7 @@ import Foundation
signatureBase += "?" + queryString
}

let signature = signatureBase.ixMd5
let signature = signatureBase.ixMd5()

return URLQueryItem.init(name: "s", value: signature)
}
Expand Down
2 changes: 0 additions & 2 deletions Sources/ImgixSwift-Umbrella-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
// Created by Paul Straw on 7/5/16.
//
//

#import "NSString+ImgixSwiftMd5.h"
sherwinski marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 0 additions & 15 deletions Sources/NSString+ImgixSwiftMd5.h

This file was deleted.

30 changes: 0 additions & 30 deletions Sources/NSString+ImgixSwiftMd5.m

This file was deleted.

24 changes: 24 additions & 0 deletions Sources/String+ImgixSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//

import Foundation
import var CommonCrypto.CC_MD5_DIGEST_LENGTH
import func CommonCrypto.CC_MD5
import typealias CommonCrypto.CC_LONG

extension String {
static var ixEncodeUriComponentCharSet: CharacterSet = {
Expand All @@ -32,4 +35,25 @@ extension String {
func ixEncodeUriComponent() -> String {
return self.addingPercentEncoding(withAllowedCharacters: String.ixEncodeUriComponentCharSet)!
}

func ixMd5() -> String {
return MD5(string: self)
}

func MD5(string: String) -> String {
let length = Int(CC_MD5_DIGEST_LENGTH)
let messageData = string.data(using:.utf8)!
var digestData = Data(count: length)

_ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
messageData.withUnsafeBytes { messageBytes -> UInt8 in
if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
let messageLength = CC_LONG(messageData.count)
CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
}
return 0
}
}
return digestData.map { String(format: "%02hhx", $0) }.joined()
}
}
41 changes: 15 additions & 26 deletions imgix-swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
580B383523E4DFA100FB9DF3 /* ReconstructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FB9ABCC1DCC037B00D2FD6B /* ReconstructTests.swift */; };
580B383623E4DFEA00FB9DF3 /* ReconstructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FB9ABCC1DCC037B00D2FD6B /* ReconstructTests.swift */; };
580B383A23E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 580B383823E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h */; settings = {ATTRIBUTES = (Private, ); }; };
580B383B23E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 580B383823E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h */; settings = {ATTRIBUTES = (Private, ); }; };
580B383C23E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 580B383823E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h */; settings = {ATTRIBUTES = (Private, ); }; };
8F06ECF11D2C8AA8001CEC88 /* ImgixSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F06ECCC1D2C8832001CEC88 /* ImgixSwift.framework */; };
8F06ED001D2C8ABA001CEC88 /* ImgixSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F06ECA51D2C8805001CEC88 /* ImgixSwift.framework */; };
8F06ED0F1D2C8AD2001CEC88 /* ImgixSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F06ECBF1D2C8828001CEC88 /* ImgixSwift.framework */; };
Expand All @@ -25,15 +30,6 @@
8F12BC2E1D2EF2FE00B56C8A /* String+ImgixSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F12BC2D1D2EF2FE00B56C8A /* String+ImgixSwift.swift */; };
8F12BC2F1D2EF2FE00B56C8A /* String+ImgixSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F12BC2D1D2EF2FE00B56C8A /* String+ImgixSwift.swift */; };
8F12BC301D2EF2FE00B56C8A /* String+ImgixSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F12BC2D1D2EF2FE00B56C8A /* String+ImgixSwift.swift */; };
8F7467631D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F7467621D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m */; };
8F7467641D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F7467621D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m */; };
8F7467651D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F7467621D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m */; };
8F7467671D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h in Headers */ = {isa = PBXBuildFile; fileRef = 8F7467661D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h */; settings = {ATTRIBUTES = (Public, ); }; };
8F7467681D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h in Headers */ = {isa = PBXBuildFile; fileRef = 8F7467661D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h */; settings = {ATTRIBUTES = (Public, ); }; };
8F7467691D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h in Headers */ = {isa = PBXBuildFile; fileRef = 8F7467661D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h */; settings = {ATTRIBUTES = (Public, ); }; };
8F74676B1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 8F74676A1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h */; settings = {ATTRIBUTES = (Public, ); }; };
8F74676C1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 8F74676A1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h */; settings = {ATTRIBUTES = (Public, ); }; };
8F74676D1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 8F74676A1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h */; settings = {ATTRIBUTES = (Public, ); }; };
8F91A77B1D2C94EB0010384D /* ImgixClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F91A7751D2C94EB0010384D /* ImgixClient.swift */; };
8F91A77C1D2C94EB0010384D /* ImgixClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F91A7751D2C94EB0010384D /* ImgixClient.swift */; };
8F91A77D1D2C94EB0010384D /* ImgixClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F91A7751D2C94EB0010384D /* ImgixClient.swift */; };
Expand Down Expand Up @@ -65,17 +61,15 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
580B383823E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "ImgixSwift-Umbrella-Header.h"; path = "Sources/ImgixSwift-Umbrella-Header.h"; sourceTree = "<group>"; };
580B383923E4ED5F00FB9DF3 /* ImgixSwift.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; name = ImgixSwift.modulemap; path = Sources/ImgixSwift.modulemap; sourceTree = "<group>"; };
8F06ECA51D2C8805001CEC88 /* ImgixSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ImgixSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8F06ECBF1D2C8828001CEC88 /* ImgixSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ImgixSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8F06ECCC1D2C8832001CEC88 /* ImgixSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ImgixSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8F06ECEC1D2C8AA8001CEC88 /* ImgixSwiftTests-macOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ImgixSwiftTests-macOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
8F06ECFB1D2C8ABA001CEC88 /* ImgixSwiftTests-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ImgixSwiftTests-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
8F06ED0A1D2C8AD2001CEC88 /* ImgixSwiftTests-tvOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ImgixSwiftTests-tvOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
8F12BC2D1D2EF2FE00B56C8A /* String+ImgixSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "String+ImgixSwift.swift"; path = "Sources/String+ImgixSwift.swift"; sourceTree = "<group>"; };
8F7467621D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSString+ImgixSwiftMd5.m"; path = "Sources/NSString+ImgixSwiftMd5.m"; sourceTree = "<group>"; };
8F7467661D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSString+ImgixSwiftMd5.h"; path = "Sources/NSString+ImgixSwiftMd5.h"; sourceTree = "<group>"; };
8F74676A1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "ImgixSwift-Umbrella-Header.h"; path = "Sources/ImgixSwift-Umbrella-Header.h"; sourceTree = "<group>"; };
8F74676F1D2CC15000E1CE15 /* ImgixSwift.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; name = ImgixSwift.modulemap; path = Sources/ImgixSwift.modulemap; sourceTree = "<group>"; };
8F9052181D2C34A000B0C1B6 /* SignatureTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignatureTests.swift; sourceTree = "<group>"; };
8F90521A1D2C3D0B00B0C1B6 /* WebProxyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebProxyTests.swift; sourceTree = "<group>"; };
8F91A76E1D2C94CD0010384D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Sources/Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -163,10 +157,8 @@
8F91A76E1D2C94CD0010384D /* Info.plist */,
8F91A7751D2C94EB0010384D /* ImgixClient.swift */,
8F12BC2D1D2EF2FE00B56C8A /* String+ImgixSwift.swift */,
8F7467621D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m */,
8F7467661D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h */,
8F74676A1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h */,
8F74676F1D2CC15000E1CE15 /* ImgixSwift.modulemap */,
580B383823E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h */,
580B383923E4ED5F00FB9DF3 /* ImgixSwift.modulemap */,
);
name = Sources;
sourceTree = "<group>";
Expand All @@ -191,26 +183,23 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
8F74676B1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h in Headers */,
8F7467671D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h in Headers */,
580B383A23E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
8F06ECBC1D2C8828001CEC88 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
8F74676C1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h in Headers */,
8F7467681D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h in Headers */,
580B383B23E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
8F06ECC91D2C8832001CEC88 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
8F74676D1D2CBF9600E1CE15 /* ImgixSwift-Umbrella-Header.h in Headers */,
8F7467691D2CBD1A00E1CE15 /* NSString+ImgixSwiftMd5.h in Headers */,
580B383C23E4ED5F00FB9DF3 /* ImgixSwift-Umbrella-Header.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -365,6 +354,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 8F82BE501D25EDDD00551F76;
Expand Down Expand Up @@ -433,7 +423,6 @@
buildActionMask = 2147483647;
files = (
8F91A77B1D2C94EB0010384D /* ImgixClient.swift in Sources */,
8F7467631D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m in Sources */,
8F12BC2E1D2EF2FE00B56C8A /* String+ImgixSwift.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -443,7 +432,6 @@
buildActionMask = 2147483647;
files = (
8F91A77C1D2C94EB0010384D /* ImgixClient.swift in Sources */,
8F7467641D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m in Sources */,
8F12BC2F1D2EF2FE00B56C8A /* String+ImgixSwift.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -453,7 +441,6 @@
buildActionMask = 2147483647;
files = (
8F91A77D1D2C94EB0010384D /* ImgixClient.swift in Sources */,
8F7467651D2CBD0900E1CE15 /* NSString+ImgixSwiftMd5.m in Sources */,
8F12BC301D2EF2FE00B56C8A /* String+ImgixSwift.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -474,6 +461,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
580B383623E4DFEA00FB9DF3 /* ReconstructTests.swift in Sources */,
8F06ED1B1D2C8AE1001CEC88 /* SignatureTests.swift in Sources */,
8F06ED191D2C8AE1001CEC88 /* BuildUrlTests.swift in Sources */,
8F06ED1A1D2C8AE1001CEC88 /* InitializationTests.swift in Sources */,
Expand All @@ -485,6 +473,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
580B383523E4DFA100FB9DF3 /* ReconstructTests.swift in Sources */,
8F06ED1F1D2C8AE1001CEC88 /* SignatureTests.swift in Sources */,
8F06ED1D1D2C8AE1001CEC88 /* BuildUrlTests.swift in Sources */,
8F06ED1E1D2C8AE1001CEC88 /* InitializationTests.swift in Sources */,
Expand Down