Skip to content

Commit

Permalink
Merge pull request #1 from solinor/feature/update-project
Browse files Browse the repository at this point in the history
Feature/update project to support swift 2.2
  • Loading branch information
ArnoHietanen committed Apr 21, 2016
2 parents d6746c6 + 6d41d96 commit 7a56e6c
Show file tree
Hide file tree
Showing 49 changed files with 2,177 additions and 1,299 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
*.pbxproj binary merge=union
6 changes: 3 additions & 3 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "Alamofire/Alamofire" >= 1.2
github "SwiftyJSON/SwiftyJSON" >= 2.2.1
github "pyrtsa/CryptoSwift" "device-tests"
github "Alamofire/Alamofire" ~> 3.3.0
github "SwiftyJSON/SwiftyJSON" ~> 2.3.1
github "krzyzanowskim/CryptoSwift" ~> 0.3
4 changes: 2 additions & 2 deletions Cartfile.private
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "Quick/Quick" "6c7b90f27c46e2317f56c86a8d82e5881e4015b0" # master branch
github "Quick/Nimble" "b89db8654c5f134d4f68e706b6da4a5581cb2001" # master branch
github "Quick/Quick" ~> 0.9.1
github "Quick/Nimble" ~> 3.2.0
10 changes: 5 additions & 5 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github "Alamofire/Alamofire" "1.3.1"
github "pyrtsa/CryptoSwift" "e28bd0961a4448d3c071b91a029736d5d60deec9"
github "Quick/Nimble" "b89db8654c5f134d4f68e706b6da4a5581cb2001"
github "Quick/Quick" "6c7b90f27c46e2317f56c86a8d82e5881e4015b0"
github "SwiftyJSON/SwiftyJSON" "2.2.1"
github "Alamofire/Alamofire" "3.3.1"
github "krzyzanowskim/CryptoSwift" "0.3.1"
github "Quick/Nimble" "v3.2.0"
github "Quick/Quick" "v0.9.2"
github "SwiftyJSON/SwiftyJSON" "2.3.3"
250 changes: 130 additions & 120 deletions Clutch.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

61 changes: 33 additions & 28 deletions Clutch/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,42 @@ public func getRequestId() -> String {

/// Returns Optional SecKeyRef from given certificate in DER-format.
///
/// :param: DER-formatted certificate
/// :returns: SecKeyRef or Nil.
/// - parameter DER-formatted: certificate
/// - returns: SecKeyRef or Nil.
private func loadDER(publicKeyFileContent: NSData) -> SecKeyRef? {
let certificate = SecCertificateCreateWithData(kCFAllocatorDefault, publicKeyFileContent as CFData).takeUnretainedValue()
let policy = SecPolicyCreateBasicX509().takeUnretainedValue();
var unmanagedTrust : Unmanaged<SecTrust>? = nil
let status = SecTrustCreateWithCertificates(certificate, policy, &unmanagedTrust)
let certificate = SecCertificateCreateWithData(kCFAllocatorDefault, publicKeyFileContent as CFData)
let policy = SecPolicyCreateBasicX509()
var unmanagedTrust : SecTrust? = nil
let status = SecTrustCreateWithCertificates(certificate!, policy, &unmanagedTrust)
if (status != 0) {
println("SecTrustCreateWithCertificates fail. Error Code: \(status)");
print("SecTrustCreateWithCertificates fail. Error Code: \(status)");
return nil
}
let trust = unmanagedTrust!.takeUnretainedValue()
let trust = unmanagedTrust!
let evaluateStatus = SecTrustEvaluate(trust, nil)
if (evaluateStatus != 0) {
println("SecTrustEvaluate fail. Error Code: \(evaluateStatus)");
print("SecTrustEvaluate fail. Error Code: \(evaluateStatus)");
return nil
}
return SecTrustCopyPublicKey(trust).takeUnretainedValue();
return SecTrustCopyPublicKey(trust)
}

private func encryptWithData(content :NSData, publicKey :SecKeyRef) -> NSData? {

let blockSize = Int(SecKeyGetBlockSize(publicKey) - 11)
var encryptedData = NSMutableData()
let encryptedData = NSMutableData()
let blockCount = Int(ceil(Double(content.length) / Double(blockSize)))

for i in 0..<blockCount {
var cipherLen = SecKeyGetBlockSize(publicKey)
var cipher = [UInt8](count: Int(cipherLen), repeatedValue: 0)
let bufferSize = min(blockSize,(content.length - i * blockSize))
var buffer = content.subdataWithRange(NSMakeRange(i*blockSize, bufferSize))
let status = SecKeyEncrypt(publicKey, SecPadding(kSecPaddingOAEP), UnsafePointer<UInt8>(buffer.bytes), buffer.length, &cipher, &cipherLen)
let buffer = content.subdataWithRange(NSMakeRange(i*blockSize, bufferSize))
let status = SecKeyEncrypt(publicKey, SecPadding.OAEP, UnsafePointer<UInt8>(buffer.bytes), buffer.length, &cipher, &cipherLen)
if (status == noErr){
encryptedData.appendBytes(cipher, length: Int(cipherLen))
}else{
println("SecKeyEncrypt fail. Error Code: \(status)")
print("SecKeyEncrypt fail. Error Code: \(status)")
return nil
}
}
Expand All @@ -59,25 +59,30 @@ private func encryptWithData(content :NSData, publicKey :SecKeyRef) -> NSData? {

public func encryptWithRsaAes(data: String, certificateBase64Der: String) -> (encryptedBase64Message: String, encryptedBase64Key: String, iv: String)?
{
let keyAes = Cipher.randomIV(AES.blockSize)
let iv = Cipher.randomIV(AES.blockSize)
let keyAes = AES.randomIV(AES.blockSize)
let iv = AES.randomIV(AES.blockSize)

if let encryptedData = CryptoSwift.AES(key: keyAes, iv: iv, blockMode: CryptoSwift.CipherBlockMode.CBC)?.encrypt([UInt8](data.utf8), padding: PKCS7())
{
if let publicKey = loadDER(NSData(base64EncodedString: certificateBase64Der, options: .allZeros)!)
do{
if let encryptedData: [UInt8] = try AES(key: keyAes, iv: iv, blockMode: CryptoSwift.CipherBlockMode.CBC).encrypt([UInt8](data.utf8), padding: PKCS7())
{
if let encryptedKey = encryptWithData(NSData(bytes: keyAes, length: keyAes.count), publicKey)
if let publicKey = loadDER(NSData(base64EncodedString: certificateBase64Der, options: [])!)
{
let encryptedBase64Message = NSData(bytes: encryptedData, length: encryptedData.count).base64EncodedStringWithOptions(.allZeros)

let encryptedBase64Key = encryptedKey.base64EncodedStringWithOptions(.allZeros)

let base64Iv = NSData(bytes: iv, length: iv.count).base64EncodedStringWithOptions(.allZeros)

return (encryptedBase64Message, encryptedBase64Key, base64Iv)
if let encryptedKey = encryptWithData(NSData(bytes: keyAes, length: keyAes.count), publicKey: publicKey)
{
let encryptedBase64Message = NSData(bytes: encryptedData, length: encryptedData.count).base64EncodedStringWithOptions([])

let encryptedBase64Key = encryptedKey.base64EncodedStringWithOptions([])

let base64Iv = NSData(bytes: iv, length: iv.count).base64EncodedStringWithOptions([])

return (encryptedBase64Message, encryptedBase64Key, base64Iv)
}
}
}

}catch{
//TODO handle errors
}
return nil
return nil
}

36 changes: 18 additions & 18 deletions Clutch/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ public struct Inset {
internal extension String {

/// Get a given substring of a string
/// :param: r The range of the substring wanted
/// :returns: The found substring
/// - parameter r: The range of the substring wanted
/// - returns: The found substring
subscript (r: Range<Int>) -> String {
get {
let startIndex = advance(self.startIndex, r.startIndex)
let endIndex = advance(startIndex, r.endIndex - r.startIndex)
let startIndex = self.startIndex.advancedBy(r.startIndex)
let endIndex = startIndex.advancedBy(r.endIndex - r.startIndex)

return self[Range(start: startIndex, end: endIndex)]
}
}
/// Returns matches for given regexp
/// :param: regex The pattern to evaluate
/// :returns: Found matches as an array
/// - parameter regex: The pattern to evaluate
/// - returns: Found matches as an array
func matchesForRegex(regex: String!) -> [String] {

let regex = NSRegularExpression(pattern: regex,
options: nil, error: nil)!
let regex = try! NSRegularExpression(pattern: regex,
options: [])
let nsString = self as NSString
let results = regex.matchesInString(nsString as String,
options: nil, range: NSMakeRange(0, nsString.length))
as! [NSTextCheckingResult]
options: [], range: NSMakeRange(0, nsString.length))


var strings = [String]()

for result in results {
for var i = 1; i < result.numberOfRanges; i++ {
for i in 1 ..< result.numberOfRanges {
let range = result.rangeAtIndex(i)
strings.append(nsString.substringWithRange(range))
}
Expand All @@ -63,8 +63,8 @@ internal extension String {
/// appends optional trailing string if longer
/// Source: https://gist.github.com/aorcsik/c8210a84f163b1b644c0
func truncate(length: Int, trailing: String? = nil) -> String {
if count(self) > length {
return self.substringToIndex(advance(self.startIndex, length)) + (trailing ?? "")
if self.characters.count > length {
return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "")
} else {
return self
}
Expand All @@ -77,9 +77,9 @@ internal extension String {
internal extension UIColor {

/// Convenience method for initializing with 0-255 color values
/// :param: red The red color value
/// :param: green The green color value
/// :param: blue The blue color value
/// - parameter red: The red color value
/// - parameter green: The green color value
/// - parameter blue: The blue color value
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
Expand All @@ -89,7 +89,7 @@ internal extension UIColor {
}

/// Convenience method for initializing with a 0xFFFFFF-0x000000 color value
/// :param: hexInt The hexadecimal integer color value
/// - parameter hexInt: The hexadecimal integer color value
convenience init(hexInt: Int) {
self.init(red: (hexInt >> 16) & 0xff, green: (hexInt >> 8) & 0xff, blue: hexInt & 0xff)
}
Expand All @@ -106,7 +106,7 @@ public extension UIViewController {
controller.successHandler = success
controller.errorHandler = error

var nav = UINavigationController(rootViewController: controller)
let nav = UINavigationController(rootViewController: controller)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
let ppc = nav.popoverPresentationController!
let minimunSize = controller.view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
Expand Down
101 changes: 0 additions & 101 deletions Clutch/IQKeybordManagerSwift/IQToolbar/IQToolbar.swift

This file was deleted.

Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 7a56e6c

Please sign in to comment.