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 all 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
2 changes: 1 addition & 1 deletion Sources/ImgixClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ import Foundation
signatureBase += "?" + queryString
}

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

return URLQueryItem.init(name: "s", value: signature)
}
Expand Down
9 changes: 0 additions & 9 deletions Sources/ImgixSwift-Umbrella-Header.h

This file was deleted.

6 changes: 0 additions & 6 deletions Sources/ImgixSwift.modulemap

This file was deleted.

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()
}
}
Loading