Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Add ephemeral encryption & add CryptoSwift back (extensions...)
Browse files Browse the repository at this point in the history
  • Loading branch information
jariz committed Jul 12, 2016
1 parent 14d675e commit bfbc352
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "Ephemerals.sendEphemeral([String : AnyObject]) -&gt; ():Ephemerals.swift">
<PersistentStrings>
<PersistentString
value = "json.rawString()">
</PersistentString>
<PersistentString
value = "json[&quot;push&quot;].rawString()">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "PushManager.disconnect() -&gt; ():PushManager.swift">
</ContextState>
Expand Down
33 changes: 30 additions & 3 deletions Noti/Crypt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import CryptoSwift

public class Crypt {
var key: [UInt8];
Expand All @@ -22,8 +23,7 @@ public class Crypt {

func decryptMessage(cipher: String) -> String? {
let rawData = NSData(base64EncodedString: cipher, options: NSDataBase64DecodingOptions(rawValue: 0))
var rawBytes = [UInt8](count: rawData!.length / sizeof(UInt8), repeatedValue: 0)
rawData?.getBytes(&rawBytes, length: rawBytes.count)
var rawBytes = rawData!.toArray()

let tag = NSData(bytes: [UInt8](rawBytes[1...16]))
let iv = [UInt8](rawBytes[17...28])
Expand All @@ -33,14 +33,41 @@ public class Crypt {
if res == nil {
return nil
} else {
//is the resulting tag correct?
//verify the resulting tag...
if tag == res!.1 {
return String(data: res!.0, encoding: NSUTF8StringEncoding)
} else {
return nil
}
}
}

func encryptMessage(message: String) -> String? {
let iv = CC.generateRandom(12)
let messageData = message.dataUsingEncoding(NSUTF8StringEncoding)!
let res = try? CC.GCM.crypt(CC.OpMode.encrypt, algorithm: .aes, data: messageData, key: NSData(bytes: key), iv: iv, aData: NSData(), tagLength: 16)
if res == nil {
return nil
}

let tag = res!.1
var data = [UInt8]()
data.append(49) // 1
data.appendContentsOf(tag.toArray())
data.appendContentsOf(iv.toArray())
data.appendContentsOf(res!.0.toArray())

let out = NSData(bytes: data).base64EncodedStringWithOptions(NSDataBase64EncodingOptions.init(rawValue: 0))

return out
}

}

extension NSData {
func toArray() -> [UInt8] {
var bytes = [UInt8](count: self.length / sizeof(UInt8), repeatedValue: 0)
self.getBytes(&bytes, length: bytes.count)
return bytes
}
}
15 changes: 14 additions & 1 deletion Noti/Ephemerals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,30 @@ import Alamofire
import SwiftyJSON

class Ephemerals: NSObject {
var token:String;
var token:String
var crypt:Crypt?

init(token:String) {
self.token = token
}

internal func sendEphemeral(body: [String: AnyObject]) {
var body = body
let headers = [
"Access-Token": token
];

if crypt != nil && body["type"] as? String == "push" {
print("Encrypting ephemeral...")
let json = JSON.init(body)

let cipher = crypt!.encryptMessage(json["push"].rawString()!)
body["push"] = [
"encrypted": true,
"ciphertext": cipher!
]
}

print("Sending ephemeral...")
print("-------- BODY --------")
debugPrint(body)
Expand Down
7 changes: 3 additions & 4 deletions Noti/PushManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Foundation
import Starscream
import SwiftyJSON
import Alamofire
import CryptoSwift

class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate {
var socket:WebSocket?
Expand Down Expand Up @@ -55,14 +54,14 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
func initCrypt() {
let keyData = userDefaults.objectForKey("secureKey") as? NSData
if keyData != nil {
var key = [UInt8](count: keyData!.length / sizeof(UInt8), repeatedValue: 0)
keyData!.getBytes(&key, length: keyData!.length)
self.crypt = Crypt(key: key)
let key = keyData?.toArray()
self.crypt = Crypt(key: key!)
print("Encryption enabled!")
} else {
self.crypt = nil
print("Encryption not enabled")
}
self.ephemerals.crypt = self.crypt
}

func getUserInfo(callback: (() -> Void)?) {
Expand Down
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ target 'Noti' do
pod 'SwiftyJSON', '~> 2.3.2'
pod 'Alamofire', '~> 3.4.1'
pod 'Sparkle', '~> 1.14.0'
pod 'CryptoSwift', '~> 0.5.1'
end

0 comments on commit bfbc352

Please sign in to comment.