Skip to content

Commit

Permalink
Merge pull request #114 from adam-fowler/mac-commoncrypto
Browse files Browse the repository at this point in the history
Use CommonCrypto on the mac
  • Loading branch information
adam-fowler authored Sep 11, 2019
2 parents ba5136e + 4f56cb7 commit e415e22
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.build
/Packages
/*.xcodeproj
Package.resolved
61 changes: 0 additions & 61 deletions Package.resolved

This file was deleted.

26 changes: 21 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,39 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "1.11.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "1.3.2"),
.package(url: "https://github.com/apple/swift-nio-ssl-support.git", from: "1.0.0"),
.package(url: "https://github.com/Yasumoto/HypertextApplicationLanguage.git", .upToNextMajor(from: "1.1.0")),
.package(url: "https://github.com/PerfectlySoft/Perfect-INIParser.git", .upToNextMajor(from: "3.0.0")),
.package(url: "https://github.com/swift-aws/HypertextApplicationLanguage.git", .upToNextMajor(from: "1.1.0")),
.package(url: "https://github.com/swift-aws/Perfect-INIParser.git", .upToNextMajor(from: "3.0.0")),
],
targets: [
.target(
name: "AWSSDKSwiftCore",
dependencies: [
"CAWSSDKOpenSSL",
"HypertextApplicationLanguage",
"NIO",
"NIOHTTP1",
"NIOOpenSSL",
"NIOFoundationCompat",
"INIParser"
]),
.target(name: "CAWSSDKOpenSSL"),
.testTarget(name: "AWSSDKSwiftCoreTests", dependencies: ["AWSSDKSwiftCore"])
]
)

// switch for whether to use CAWSSDKOpenSSL to shim between OpenSSL versions
#if os(Linux)
let useAWSSDKOpenSSLShim = true
#else
let useAWSSDKOpenSSLShim = false
#endif

// AWSSDKSwiftCore target
let awsSdkSwiftCoreTarget = package.targets.first(where: {$0.name == "AWSSDKSwiftCore"})

// Decide on where we get our SSL support from. Linux usses NIOSSL to provide SSL. Linux also needs CAWSSDKOpenSSL to shim across different OpenSSL versions for the HMAC functions.
if useAWSSDKOpenSSLShim {
package.targets.append(.target(name: "CAWSSDKOpenSSL"))
awsSdkSwiftCoreTarget?.dependencies.append("CAWSSDKOpenSSL")
package.dependencies.append(.package(url: "https://github.com/apple/swift-nio-ssl-support.git", from: "1.0.0"))
}


25 changes: 23 additions & 2 deletions Sources/AWSSDKSwiftCore/HMAC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,39 @@
//

import Foundation

#if canImport(CAWSSDKOpenSSL)

import CAWSSDKOpenSSL

func hmac(string: String, key: [UInt8]) -> [UInt8] {
let context = AWSSDK_HMAC_CTX_new()
HMAC_Init_ex(context, key, Int32(key.count), EVP_sha256(), nil)

let bytes = Array(string.utf8)
HMAC_Update(context, bytes, bytes.count)
var digest = [UInt8](repeating: 0, count: Int(EVP_MAX_MD_SIZE))
var length: UInt32 = 0
HMAC_Final(context, &digest, &length)
AWSSDK_HMAC_CTX_free(context)

return Array(digest[0..<Int(length)])
}

#elseif canImport(CommonCrypto)

import CommonCrypto

func hmac(string: String, key: [UInt8]) -> [UInt8] {
var context = CCHmacContext()
CCHmacInit(&context, CCHmacAlgorithm(kCCHmacAlgSHA256), key, key.count)

let bytes = Array(string.utf8)
CCHmacUpdate(&context, bytes, bytes.count)
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
CCHmacFinal(&context, &digest)

return digest
}

#endif
62 changes: 58 additions & 4 deletions Sources/AWSSDKSwiftCore/Hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//

import Foundation

#if canImport(CAWSSDKOpenSSL)

import CAWSSDKOpenSSL

public func sha256(_ string: String) -> [UInt8] {
Expand All @@ -21,9 +24,11 @@ public func sha256(_ bytes: inout [UInt8]) -> [UInt8] {
}

public func sha256(_ data: Data) -> [UInt8] {
return data.withUnsafeBytes {(bytes: UnsafePointer<UInt8>) in
return data.withUnsafeBytes { ptr in
var hash = [UInt8](repeating: 0, count: Int(SHA256_DIGEST_LENGTH))
SHA256(bytes, data.count, &hash)
if let bytes = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self) {
SHA256(bytes, data.count, &hash)
}
return hash
}
}
Expand All @@ -39,9 +44,58 @@ public func sha256(_ bytes1: inout [UInt8], _ bytes2: inout [UInt8]) -> [UInt8]
}

public func md5(_ data: Data) -> [UInt8] {
return data.withUnsafeBytes {(bytes: UnsafePointer<UInt8>) in
return data.withUnsafeBytes { ptr in
var hash = [UInt8](repeating: 0, count: Int(MD5_DIGEST_LENGTH))
MD5(bytes, data.count, &hash)
if let bytes = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self) {
MD5(bytes, data.count, &hash)
}
return hash
}
}

#elseif canImport(CommonCrypto)

import CommonCrypto

public func sha256(_ string: String) -> [UInt8] {
var bytes = Array(string.utf8)
return sha256(&bytes)
}

public func sha256(_ bytes: inout [UInt8]) -> [UInt8] {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(&bytes, CC_LONG(bytes.count), &hash)
return hash
}

public func sha256(_ data: Data) -> [UInt8] {
return data.withUnsafeBytes { ptr in
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
if let bytes = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self) {
CC_SHA256(bytes, CC_LONG(data.count), &hash)
}
return hash
}
}

public func sha256(_ bytes1: inout [UInt8], _ bytes2: inout [UInt8]) -> [UInt8] {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
var context = CC_SHA256_CTX()
CC_SHA256_Init(&context)
CC_SHA256_Update(&context, &bytes1, CC_LONG(bytes1.count))
CC_SHA256_Update(&context, &bytes2, CC_LONG(bytes2.count))
CC_SHA256_Final(&hash, &context)
return hash
}

public func md5(_ data: Data) -> [UInt8] {
return data.withUnsafeBytes { ptr in
var hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
if let bytes = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self) {
CC_MD5(bytes, CC_LONG(data.count), &hash)
}
return hash
}
}

#endif

0 comments on commit e415e22

Please sign in to comment.