Skip to content

Commit

Permalink
refactor: use Swift CommonCrypto module to calculate md5 hashes (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sherwinski authored Feb 11, 2020
1 parent 0ac0ba3 commit a9c43c2
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 177 deletions.
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

0 comments on commit a9c43c2

Please sign in to comment.