Renamed parameter to sync with CryptoKit

This commit is contained in:
Adam Fowler 2020-02-01 11:30:22 +00:00
parent 38ab4efd6c
commit fd86446eff
8 changed files with 39 additions and 62 deletions

View File

@ -28,14 +28,14 @@ public struct HMAC<H: CCHashFunction> {
/// update HMAC calculation with a block of data
public mutating func update<D: DataProtocol>(data: D) {
if let digest = data.withContiguousStorageIfAvailable({ bytes in
return self.update(bytes: .init(bytes))
return self.update(bufferPointer: .init(bytes))
}) {
return digest
} else {
var buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: data.count)
data.copyBytes(to: buffer)
defer { buffer.deallocate() }
self.update(bytes: .init(buffer))
self.update(bufferPointer: .init(buffer))
}
}
}
@ -54,8 +54,8 @@ extension HMAC {
}
/// update HMAC calculation with a buffer
public mutating func update(bytes: UnsafeRawBufferPointer) {
CCHmacUpdate(&context, bytes.baseAddress, bytes.count)
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CCHmacUpdate(&context, bufferPointer.baseAddress, bufferPointer.count)
}
/// finalize HMAC calculation and return authentication code
@ -85,14 +85,14 @@ public struct HMAC<H: OpenSSLHashFunction> {
/// update HMAC calculation with a block of data
public mutating func update<D: DataProtocol>(data: D) {
if let digest = data.withContiguousStorageIfAvailable({ bytes in
return self.update(bytes: bytes)
return self.update(bufferPointer: bytes)
}) {
return digest
} else {
var buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: data.count)
data.copyBytes(to: buffer)
defer { buffer.deallocate() }
self.update(bytes: .init(buffer))
self.update(bufferPointer: .init(buffer))
}
}
}
@ -111,8 +111,8 @@ extension HMAC {
}
/// update HMAC calculation with a buffer
mutating func update(bytes: UnsafeBufferPointer<UInt8>) {
HMAC_Update(context, bytes.baseAddress, bytes.count)
mutating func update(bufferPointer: UnsafeBufferPointer<UInt8>) {
HMAC_Update(context, bufferPointer.baseAddress, bufferPointer.count)
}
/// finalize HMAC calculation and return authentication code

View File

@ -10,13 +10,13 @@ public protocol HashFunction {
associatedtype Digest: AWSCrypto.Digest
/// hash raw buffer
static func hash(bytes: UnsafeRawBufferPointer) -> Self.Digest
static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest
/// initialization
init()
/// update hash function with data
mutating func update(bytes: UnsafeRawBufferPointer)
mutating func update(bufferPointer: UnsafeRawBufferPointer)
/// finalize hash function and return digest
mutating func finalize() -> Self.Digest
}
@ -24,37 +24,37 @@ public protocol HashFunction {
extension HashFunction {
/// default version of hash which call init, update and finalize
public static func hash(bytes: UnsafeRawBufferPointer) -> Self.Digest {
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var function = Self()
function.update(bytes: bytes)
function.update(bufferPointer: bufferPointer)
return function.finalize()
}
/// version of hash that takes data in any form that complies with DataProtocol
public static func hash<D: DataProtocol>(data: D) -> Self.Digest {
if let digest = data.withContiguousStorageIfAvailable({ bytes in
return self.hash(bytes: .init(bytes))
return self.hash(bufferPointer: .init(bytes))
}) {
return digest
} else {
var buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: data.count)
data.copyBytes(to: buffer)
defer { buffer.deallocate() }
return self.hash(bytes: .init(buffer))
return self.hash(bufferPointer: .init(buffer))
}
}
/// version of update that takes data in any form that complies with DataProtocol
public mutating func update<D: DataProtocol>(data: D) {
if let digest = data.withContiguousStorageIfAvailable({ bytes in
return self.update(bytes: .init(bytes))
return self.update(bufferPointer: .init(bytes))
}) {
return digest
} else {
var buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: data.count)
data.copyBytes(to: buffer)
defer { buffer.deallocate() }
self.update(bytes: .init(buffer))
self.update(bufferPointer: .init(buffer))
}
}
}
@ -68,28 +68,6 @@ public protocol CCHashFunction: HashFunction {
static var algorithm: CCHmacAlgorithm { get }
}
/// internal protocol for Common Crypto hash functions that hides implementation details
protocol _CCHashFunction: CCHashFunction where Digest: ByteDigest {
/// Context type that holds the hash function context
associatedtype Context
/// hashing context instance
var context: Context { get set }
/// creates a hashing context
static func createContext() -> Context
/// initialization with context, required by init()
init(context: Context)
/// init hash function
func initFunction(_ :UnsafeMutablePointer<Context>)
/// update hash function
func updateFunction(_ :UnsafeMutablePointer<Context>, _ :UnsafeRawPointer, _ :CC_LONG)
/// finalize hash function
func finalFunction(_ :UnsafeMutablePointer<UInt8>, _ :UnsafeMutablePointer<Context>) -> Int32
}
#else
import CAWSCrypto
@ -118,8 +96,8 @@ extension _OpenSSLHashFunction {
}
/// update hash function with data
public mutating func update(bytes: UnsafeRawBufferPointer) {
EVP_DigestUpdate(context, bytes.baseAddress, bytes.count)
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
EVP_DigestUpdate(context, bufferPointer.baseAddress, bufferPointer.count)
}
/// finalize hash function and return digest

View File

@ -15,9 +15,9 @@ public struct MD5: CCHashFunction {
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgMD5) }
var context: CC_MD5_CTX
public static func hash(bytes: UnsafeRawBufferPointer) -> Self.Digest {
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_MD5(bytes.baseAddress, CC_LONG(bytes.count), &digest)
CC_MD5(bufferPointer.baseAddress, CC_LONG(bufferPointer.count), &digest)
return .init(bytes: digest)
}
@ -26,8 +26,8 @@ public struct MD5: CCHashFunction {
CC_MD5_Init(&context)
}
public mutating func update(bytes: UnsafeRawBufferPointer) {
CC_MD5_Update(&context, bytes.baseAddress, CC_LONG(bytes.count))
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CC_MD5_Update(&context, bufferPointer.baseAddress, CC_LONG(bufferPointer.count))
}
public mutating func finalize() -> Self.Digest {

View File

@ -15,9 +15,9 @@ public struct SHA256: CCHashFunction {
public static var algorithm: CCHmacAlgorithm { return CCHmacAlgorithm(kCCHmacAlgSHA256) }
var context: CC_SHA256_CTX
public static func hash(bytes: UnsafeRawBufferPointer) -> Self.Digest {
public static func hash(bufferPointer: UnsafeRawBufferPointer) -> Self.Digest {
var digest: [UInt8] = .init(repeating: 0, count: Digest.byteCount)
CC_SHA256(bytes.baseAddress, CC_LONG(bytes.count), &digest)
CC_SHA256(bufferPointer.baseAddress, CC_LONG(bufferPointer.count), &digest)
return .init(bytes: digest)
}
@ -26,8 +26,8 @@ public struct SHA256: CCHashFunction {
CC_SHA256_Init(&context)
}
public mutating func update(bytes: UnsafeRawBufferPointer) {
CC_SHA256_Update(&context, bytes.baseAddress, CC_LONG(bytes.count))
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
CC_SHA256_Update(&context, bufferPointer.baseAddress, CC_LONG(bufferPointer.count))
}
public mutating func finalize() -> Self.Digest {

View File

@ -188,7 +188,7 @@ public struct AWSSigner {
case .byteBuffer(let byteBuffer):
let byteBufferView = byteBuffer.readableBytesView
hash = byteBufferView.withContiguousStorageIfAvailable { bytes in
return SHA256.hash(bytes: .init(bytes)).description
return SHA256.hash(data: bytes).description
}
}
if let hash = hash {

View File

@ -42,4 +42,10 @@ final class AWSCryptoTests: XCTestCase {
print(authenticationKey)
XCTAssertEqual(authenticationKey.description, "ddec250211f1b546254bab3fb027af1acc4842898e8af6eeadcdbf8e2c6c1ff5")
}
static var allTests = [
("testMD5", testMD5),
("testSHA256", testSHA256),
("testHMAC", testHMAC),
]
}

View File

@ -1,9 +0,0 @@
import XCTest
#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(AWSSignerTests.allTests),
]
}
#endif

View File

@ -1,7 +1,9 @@
import XCTest
import AWSSignerTests
@testable import AWSSignerTests
@testable import AWSCryptoTests
var tests = [XCTestCaseEntry]()
tests += AWSSignerTests.allTests()
XCTMain(tests)
XCTMain([
testCase(AWSSignerTests.allTests),
testCase(AWSCryptoTests.allTests)
])