Skip to content

Commit

Permalink
Swift 5 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Gereon Steffens committed May 24, 2019
1 parent 2414c60 commit cb145c0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion OneTimePassword.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pod::Spec.new do |s|
s.homepage = "https://github.com/mattrubin/OneTimePassword"
s.license = "MIT"
s.author = "Matt Rubin"
s.swift_version = "4.2"
s.swift_versions = ["4.2", "5.0"]
s.ios.deployment_target = "8.0"
s.watchos.deployment_target = "2.0"
s.source = { :git => "https://github.com/mattrubin/OneTimePassword.git", :tag => s.version }
Expand Down
4 changes: 2 additions & 2 deletions OneTimePassword.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@
buildSettings = {
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
SWIFT_TREAT_WARNINGS_AS_ERRORS = NO;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
name = Debug;
Expand All @@ -695,7 +695,7 @@
buildSettings = {
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
SWIFT_TREAT_WARNINGS_AS_ERRORS = NO;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
name = Release;
Expand Down
8 changes: 8 additions & 0 deletions Sources/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ func HMAC(algorithm: Generator.Algorithm, key: Data, data: Data) -> Data {
macOut.deallocate()
}

#if swift(>=5.0)
key.withUnsafeBytes { keyBytes in
data.withUnsafeBytes { dataBytes in
CCHmac(hashFunction, keyBytes.baseAddress, key.count, dataBytes.baseAddress, data.count, macOut)
}
}
#else
key.withUnsafeBytes { keyBytes in
data.withUnsafeBytes { dataBytes in
CCHmac(hashFunction, keyBytes, key.count, dataBytes, data.count, macOut)
}
}
#endif

return Data(bytes: macOut, count: hashLength)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public struct Generator: Equatable {
let counterData = Data(bytes: &bigCounter, count: MemoryLayout<UInt64>.size)
let hash = HMAC(algorithm: algorithm, key: secret, data: counterData)

#if swift(>=5.0)
var truncatedHash = hash.withUnsafeBytes { ptr -> UInt32 in
// Use the last 4 bits of the hash as an offset (0 <= offset <= 15)
let offset = ptr[hash.count - 1] & 0x0f

// Take 4 bytes from the hash, starting at the given byte offset
let truncatedHashPtr = ptr.baseAddress! + Int(offset)
return truncatedHashPtr.bindMemory(to: UInt32.self, capacity: 1).pointee
}
#else
var truncatedHash = hash.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> UInt32 in
// Use the last 4 bits of the hash as an offset (0 <= offset <= 15)
let offset = ptr[hash.count - 1] & 0x0f
Expand All @@ -96,6 +106,7 @@ public struct Generator: Equatable {
$0.pointee
}
}
#endif

// Ensure the four bytes taken from the hash match the current endian format
truncatedHash = UInt32(bigEndian: truncatedHash)
Expand Down

0 comments on commit cb145c0

Please # to comment.