瀏覽代碼

Failable initializers

Marcin Krzyżanowski 10 年之前
父節點
當前提交
11159915a9
共有 5 個文件被更改,包括 22 次插入14 次删除
  1. 3 1
      CryptoSwift/ChaCha20.swift
  2. 2 2
      CryptoSwift/Cipher.swift
  3. 4 1
      CryptoSwift/CryptoHashBase.swift
  4. 6 6
      CryptoSwift/Hash.swift
  5. 7 4
      CryptoSwift/Poly1305.swift

+ 3 - 1
CryptoSwift/ChaCha20.swift

@@ -22,9 +22,11 @@ class ChaCha20 {
         }
     }
     
-    init(key:NSData, iv:NSData) {
+    init?(key:NSData, iv:NSData) {
         if let c = contextSetup(iv: iv, key: key) {
             context = c
+        } else {
+            return nil
         }
     }
     

+ 2 - 2
CryptoSwift/Cipher.swift

@@ -15,7 +15,7 @@ public enum Cipher {
         switch (self) {
             case .ChaCha20(let key, let iv):
                 var chacha = CryptoSwift.ChaCha20(key: key, iv: iv);
-                return chacha.encrypt(message)
+                return chacha?.encrypt(message)
         }
     }
     
@@ -23,7 +23,7 @@ public enum Cipher {
         switch (self) {
         case .ChaCha20(let key, let iv):
             var chacha = CryptoSwift.ChaCha20(key: key, iv: iv);
-            return chacha.decrypt(message)
+            return chacha?.decrypt(message)
         }
     }
 

+ 4 - 1
CryptoSwift/CryptoHashBase.swift

@@ -12,8 +12,11 @@ class HashBase {
     
     var message: NSData
     
-    init(_ message: NSData) {
+    init?(_ message: NSData) {
         self.message = message
+        if (self.message.length == 0) {
+            return nil
+        }
     }
     
     /** Common part for hash calculation. Prepare header data. */

+ 6 - 6
CryptoSwift/Hash.swift

@@ -17,17 +17,17 @@ public enum Hash {
     public func calculate() -> NSData! {
         switch self {
         case md5(let data):
-            return MD5(data).calculate()
+            return MD5(data)?.calculate()
         case sha1(let data):
-            return SHA1(data).calculate()
+            return SHA1(data)?.calculate()
         case sha224(let data):
-            return SHA2(data).calculate32(.sha224)
+            return SHA2(data)?.calculate32(.sha224)
         case sha256(let data):
-            return SHA2(data).calculate32(.sha256)
+            return SHA2(data)?.calculate32(.sha256)
         case sha384(let data):
-            return SHA2(data).calculate64(.sha384)
+            return SHA2(data)?.calculate64(.sha384)
         case sha512(let data):
-            return SHA2(data).calculate64(.sha512)
+            return SHA2(data)?.calculate64(.sha512)
         case crc32(let data):
             return CRC().crc32(data);
         default:

+ 7 - 4
CryptoSwift/Poly1305.swift

@@ -25,10 +25,10 @@ public class Poly1305 {
         var final:Byte   = 0
         var leftover:Int = 0
         
-        init (_ key: [Byte]) {
+        init?(_ key: [Byte]) {
             assert(key.count == 32,"Invalid key length");
             if (key.count != 32) {
-                return;
+                return nil;
             }
             
             for i in 0..<17 {
@@ -96,13 +96,16 @@ public class Poly1305 {
     }
 
     class internal func authenticate(# key: [Byte], message: [Byte]) -> [Byte]? {
-        return Poly1305(key).authenticate(message: message)
+        return Poly1305(key)?.authenticate(message: message)
     }
     
     // MARK: - Private
     
-    private init (_ key: [Byte]) {
+    private init? (_ key: [Byte]) {
         ctx = Context(key)
+        if (ctx == nil) {
+            return nil
+        }
     }
     
     private func authenticate(# message:[Byte]) -> [Byte]? {