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

Commit

Permalink
Remove CryptoSwift, port over to SwCrypt's key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jariz committed Jul 12, 2016
1 parent d159492 commit 14d675e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Noti.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
162B919FE4321ED9BC86AD09 /* Pods_Noti.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2C7615AF577BAEAF32B8516 /* Pods_Noti.framework */; };
4901D3CF1D1C121D005F1641 /* StatusMenuController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4901D3CE1D1C121D005F1641 /* StatusMenuController.swift */; };
4901D3D31D23E6AB005F1641 /* IntroViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4901D3D21D23E6AB005F1641 /* IntroViewController.swift */; };
498275F01D34E03200AB7029 /* SwCrypt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 498275EF1D34E03200AB7029 /* SwCrypt.swift */; };
498275F21D35101A00AB7029 /* Crypt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 498275F11D35101A00AB7029 /* Crypt.swift */; };
498275FB1D3567D500AB7029 /* SwCrypt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 498275FA1D3567D500AB7029 /* SwCrypt.swift */; };
498499FE1D1BE46F007FC963 /* PushManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 498499FD1D1BE46F007FC963 /* PushManager.swift */; };
499DD0F61D2ACA0B00BB759D /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = 499DD0F51D2ACA0B00BB759D /* .gitignore */; };
499DD0F81D2D7BBF00BB759D /* Ephemerals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 499DD0F71D2D7BBF00BB759D /* Ephemerals.swift */; };
Expand All @@ -27,9 +27,9 @@
0F7FEC6A353CB04118DDEBF6 /* Pods-Noti.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Noti.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Noti/Pods-Noti.debug.xcconfig"; sourceTree = "<group>"; };
4901D3CE1D1C121D005F1641 /* StatusMenuController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusMenuController.swift; sourceTree = "<group>"; };
4901D3D21D23E6AB005F1641 /* IntroViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntroViewController.swift; sourceTree = "<group>"; };
498275EE1D34E03200AB7029 /* SwCrypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwCrypt.h; sourceTree = "<group>"; };
498275EF1D34E03200AB7029 /* SwCrypt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwCrypt.swift; sourceTree = "<group>"; };
498275F11D35101A00AB7029 /* Crypt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Crypt.swift; sourceTree = "<group>"; };
498275F91D3567D500AB7029 /* SwCrypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwCrypt.h; sourceTree = "<group>"; };
498275FA1D3567D500AB7029 /* SwCrypt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwCrypt.swift; sourceTree = "<group>"; };
498499FD1D1BE46F007FC963 /* PushManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PushManager.swift; sourceTree = "<group>"; };
499DD0F51D2ACA0B00BB759D /* .gitignore */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
499DD0F71D2D7BBF00BB759D /* Ephemerals.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Ephemerals.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,8 +80,8 @@
49B43B0A1D1ADFB200A24585 /* Noti */ = {
isa = PBXGroup;
children = (
498275EE1D34E03200AB7029 /* SwCrypt.h */,
498275EF1D34E03200AB7029 /* SwCrypt.swift */,
498275F91D3567D500AB7029 /* SwCrypt.h */,
498275FA1D3567D500AB7029 /* SwCrypt.swift */,
49B43B0B1D1ADFB200A24585 /* AppDelegate.swift */,
49B43B0D1D1ADFB200A24585 /* AuthViewController.swift */,
49B43B0F1D1ADFB200A24585 /* Assets.xcassets */,
Expand Down Expand Up @@ -238,11 +238,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
498275FB1D3567D500AB7029 /* SwCrypt.swift in Sources */,
4901D3D31D23E6AB005F1641 /* IntroViewController.swift in Sources */,
499DD0FC1D2D8FAC00BB759D /* RoundedImage.swift in Sources */,
498499FE1D1BE46F007FC963 /* PushManager.swift in Sources */,
49B43B0E1D1ADFB200A24585 /* AuthViewController.swift in Sources */,
498275F01D34E03200AB7029 /* SwCrypt.swift in Sources */,
4901D3CF1D1C121D005F1641 /* StatusMenuController.swift in Sources */,
49B43B0C1D1ADFB200A24585 /* AppDelegate.swift in Sources */,
499DD0F81D2D7BBF00BB759D /* Ephemerals.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "Crypt.decryptMessage(String) -&gt; String?:Crypt.swift">
<PersistentStrings>
<PersistentString
value = "tag == res!.1">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "PushManager.userNotificationCenter(NSUserNotificationCenter, didActivateNotification : NSUserNotification) -&gt; ():PushManager.swift">
<PersistentStrings>
Expand Down
23 changes: 10 additions & 13 deletions Noti/Crypt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import CryptoSwift

public class Crypt {
var key: [UInt8];
Expand All @@ -16,34 +15,32 @@ public class Crypt {
self.key = key
}

static func generateKey(password: String, salt: String) -> [UInt8]? {
let password: Array<UInt8> = password.utf8.map {$0}
let salt: Array<UInt8> = salt.utf8.map {$0}
print("Noti is generating a key!")
return try? PKCS5.PBKDF2(password: password, salt: salt, iterations: 30000, keyLength: 32, variant: .sha256).calculate()
static func generateKey(password: String, salt: String) -> NSData? {
print("Generating a new key...")
return try? CC.KeyDerivation.PBKDF2(password, salt: salt.dataUsingEncoding(NSUTF8StringEncoding)!, prf: .sha256, rounds: 30000)
}

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)

// let tag = [UInt8](rawBytes[1...16])
let tag = NSData(bytes: [UInt8](rawBytes[1...16]))
let iv = [UInt8](rawBytes[17...28])
let message = [UInt8](rawBytes[29..<rawBytes.count])

let res = try? CC.GCM.crypt(.decrypt, algorithm: .aes, data: NSData(bytes: message), key: NSData(bytes: key), iv: NSData(bytes: iv), aData: NSData(), tagLength: 16)
if res == nil {
return nil
} else {
//todo tag verification
return String(data: res!.0, encoding: NSUTF8StringEncoding)
//is the resulting tag correct?
if tag == res!.1 {
return String(data: res!.0, encoding: NSUTF8StringEncoding)
} else {
return nil
}
}

// let input = NSData()
// let decrypted = try! input.decrypt(AES(key: key!, iv: iv))
//
// let decoded = NSString(data: decrypted, encoding: NSUTF8StringEncoding)
}

}
2 changes: 1 addition & 1 deletion Noti/PushManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
else if(pwd.stringValue != "") {
let iden = userInfo!["iden"].string!
let key = Crypt.generateKey(pwd.stringValue, salt: iden)
userDefaults.setObject(NSData(bytes: key!), forKey: "secureKey")
userDefaults.setObject(key, forKey: "secureKey")
} else {
userDefaults.removeObjectForKey("secureKey")
}
Expand Down
1 change: 0 additions & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ 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 14d675e

Please sign in to comment.