Created
December 29, 2019 13:04
-
-
Save adam-fowler/cb3ff5936dec1467da56ea633494e9a6 to your computer and use it in GitHub Desktop.
Comparing CryptoSwift to OpenSSL functions imported by AWSSDKSwiftCore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AWSSDKSwiftCore | |
import CryptoSwift | |
import Foundation | |
extension Array where Element: FixedWidthInteger { | |
static func random(count: Int) -> [Element] { | |
var array = self.init() | |
for _ in 0..<count { | |
array.append(.random(in: Element.min..<Element.max)) | |
} | |
return array | |
} | |
} | |
//MARK: SHA256 | |
func sha256Performace() -> [Double] { | |
var buffer = [UInt8].random(count: 65536) | |
let startValue = Date() | |
for _ in 1...1000 { | |
_ = sha256(&buffer) | |
} | |
let time1 = -startValue.timeIntervalSinceNow | |
for _ in 1...1000 { | |
let hash = SHA2(variant: .sha256) | |
_ = hash.calculate(for: buffer) | |
} | |
let time2 = (-startValue.timeIntervalSinceNow) - time1 | |
return [time1, time2] | |
} | |
//MARK: MD5 | |
func md5Performace() -> [Double] { | |
let buffer = [UInt8].random(count: 65536) | |
let dataBuffer = Data(buffer) | |
let startValue = Date() | |
for _ in 1...1000 { | |
_ = md5(dataBuffer) | |
} | |
let time1 = -startValue.timeIntervalSinceNow | |
for _ in 1...1000 { | |
let hash = MD5() | |
_ = hash.calculate(for: buffer) | |
} | |
let time2 = (-startValue.timeIntervalSinceNow) - time1 | |
return [time1, time2] | |
} | |
//MARK: HMAC | |
func hmacPerformace() -> [Double] { | |
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
let string = String((0..<65536).map{ _ in letters.randomElement()! }) | |
let buffer = Array(string.utf8) | |
let key = [UInt8].random(count: 32) | |
let startValue = Date() | |
for _ in 1...1000 { | |
_ = hmac(string: string, key: key) | |
} | |
let time1 = -startValue.timeIntervalSinceNow | |
for _ in 1...1000 { | |
let hash = HMAC(key: key, variant: .sha256) | |
_ = try? hash.authenticate(buffer) | |
} | |
let time2 = (-startValue.timeIntervalSinceNow) - time1 | |
return [time1, time2] | |
} | |
var times: [String: [Double]] = [:] | |
times["md5"] = md5Performace() | |
times["sha256"] = sha256Performace() | |
times["hmac"] = hmacPerformace() | |
let output = times.map { "\($0.key):\t\($0.value[0])\t\($0.value[1])" } | |
for o in output { | |
print(o) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment