Skip to content

Commit

Permalink
Replace swift-sodium with TweetNacl
Browse files Browse the repository at this point in the history
- This works around the fact that swift-sodium cannot build correctly on Carthage due to the lack of .xcframework support.
  • Loading branch information
danielrbrowne committed Nov 12, 2020
1 parent bfb385f commit ea39225
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "ashleymills/Reachability.swift" ~> 5.0.0
github "jedisct1/swift-sodium" == 0.8.0
github "bitmark-inc/tweetnacl-swiftwrap" ~> 1.0
github "pusher/NWWebSocket" ~> 0.3.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "ashleymills/Reachability.swift" "v5.0.0"
github "jedisct1/swift-sodium" "0.8.0"
github "bitmark-inc/tweetnacl-swiftwrap" "1.0.2"
github "pusher/NWWebSocket" "0.3.0"
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
}
},
{
"package": "Sodium",
"repositoryURL": "https://github.com/jedisct1/swift-sodium",
"package": "TweetNacl",
"repositoryURL": "https://github.com/bitmark-inc/tweetnacl-swiftwrap",
"state": {
"branch": null,
"revision": "176745ddc4991b27f80c3c2637652eb33b5782eb",
"version": "0.9.0"
"revision": "d1552db4d907f2c5cb3d1bf1336496b2e16c8ecf",
"version": "1.0.2"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/ashleymills/Reachability.swift.git", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/pusher/NWWebSocket.git", .upToNextMajor(from: "0.3.0")),
.package(url: "https://github.com/jedisct1/swift-sodium", .upToNextMajor(from: "0.9.0")),
.package(url: "https://github.com/bitmark-inc/tweetnacl-swiftwrap", .upToNextMajor(from: "1.0.0")),
],
targets: [
.target(
name: "PusherSwiftWithEncryption",
dependencies: [
"Reachability",
"NWWebSocket",
"Sodium",
"TweetNacl",
],
path: "Sources",
exclude: ["PusherSwift-Only"]
Expand Down
3 changes: 2 additions & 1 deletion PusherSwiftWithEncryption.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ Pod::Spec.new do |s|
s.exclude_files = ['Sources/PusherSwift-Only/**/*.swift']

s.dependency 'ReachabilitySwift', '~> 5.0'
s.dependency 'Sodium', '0.8.0'
s.dependency 'TweetNacl', '~> 1.0.0'
s.dependency 'NWWebSocket', '~> 0.3.0'

s.ios.deployment_target = '13.0'
s.osx.deployment_target = '10.15'
s.tvos.deployment_target = '13.0'
end
22 changes: 10 additions & 12 deletions Sources/PusherSwiftWithEncryption-Only/PusherDecryptor.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import Sodium
import TweetNacl

class PusherDecryptor {

Expand All @@ -9,8 +9,6 @@ class PusherDecryptor {

}

private static let sodium = Sodium()

static func decrypt(data: String?, decryptionKey: String?) throws -> String? {
guard let data = data else {
return nil
Expand All @@ -25,9 +23,9 @@ class PusherDecryptor {
let nonce = try self.decodedNonce(fromEncryptedData: encryptedData)
let secretKey = try self.decodedDecryptionKey(fromDecryptionKey: decryptionKey)

guard let decryptedData = self.sodium.secretBox.open(authenticatedCipherText: cipherText,
secretKey: secretKey,
nonce: nonce),
guard let decryptedData = try? NaclSecretBox.open(box: cipherText,
nonce: nonce,
key: secretKey),
let decryptedString = String(bytes: decryptedData, encoding: .utf8) else {
throw PusherEventError.invalidDecryptionKey
}
Expand All @@ -44,28 +42,28 @@ class PusherDecryptor {
return encryptedData
}

private static func decodedCipherText(fromEncryptedData encryptedData: EncryptedData) throws -> Bytes {
private static func decodedCipherText(fromEncryptedData encryptedData: EncryptedData) throws -> Data {
guard let decodedCipherText = Data(base64Encoded: encryptedData.ciphertext) else {
throw PusherEventError.invalidEncryptedData
}

return Bytes(decodedCipherText)
return decodedCipherText
}

private static func decodedNonce(fromEncryptedData encryptedData: EncryptedData) throws -> SecretBox.Nonce {
private static func decodedNonce(fromEncryptedData encryptedData: EncryptedData) throws -> Data {
guard let decodedNonce = Data(base64Encoded: encryptedData.nonce) else {
throw PusherEventError.invalidEncryptedData
}

return SecretBox.Nonce(decodedNonce)
return decodedNonce
}

private static func decodedDecryptionKey(fromDecryptionKey decryptionKey: String) throws -> SecretBox.Key {
private static func decodedDecryptionKey(fromDecryptionKey decryptionKey: String) throws -> Data {
guard let decodedDecryptionKey = Data(base64Encoded: decryptionKey) else {
throw PusherEventError.invalidDecryptionKey
}

return SecretBox.Key(decodedDecryptionKey)
return decodedDecryptionKey
}

static func isDecryptionAvailable() -> Bool {
Expand Down

0 comments on commit ea39225

Please sign in to comment.