Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 5 compatibility #209

Merged
merged 3 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ language: objective-c
xcode_workspace: OneTimePassword.xcworkspace
xcode_scheme: OneTimePassword (iOS)

osx_image: xcode10.1
osx_image: xcode10.2

before_install:
- gem install cocoapods -v 1.7

env:
- RUNTIME="iOS 8.4" DEVICE="iPhone 4s"
Expand Down
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