瀏覽代碼

Cleanups. No Optionals when not used.

Marcin Krzyżanowski 10 年之前
父節點
當前提交
b6ee8ae51f

+ 11 - 15
CryptoSwift.xcodeproj/project.pbxproj

@@ -24,7 +24,7 @@
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
 		75164E4919AD30AC00737F30 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75164E4819AD30AC00737F30 /* Utils.swift */; };
 		751C5C3D19B26B000094C75D /* Poly1305.swift in Sources */ = {isa = PBXBuildFile; fileRef = 751C5C3C19B26B000094C75D /* Poly1305.swift */; };
-		753403F71BB898470039099C /* Array<UInt8>+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753403F61BB898470039099C /* Array<UInt8>+Extension.swift */; settings = {ASSET_TAGS = (); }; };
+		75232CCE1BC6E6AC007F68B1 /* CSArrayType+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75232CCD1BC6E6AC007F68B1 /* CSArrayType+Extensions.swift */; };
 		7539E3291B3B4A530037F4E1 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750A545F1992D2680017DA75 /* MD5.swift */; };
 		7539E32B1B3B4C6B0037F4E1 /* HMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758A94251A65AEB100E46135 /* HMAC.swift */; };
 		7539E32C1B3B4C750037F4E1 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7599C9C5199EA28700A3988B /* String+Extension.swift */; };
@@ -47,7 +47,6 @@
 		758A94281A65C59200E46135 /* HMACTests.swift in Resources */ = {isa = PBXBuildFile; fileRef = 758A94271A65C59200E46135 /* HMACTests.swift */; };
 		758A94291A65C67400E46135 /* HMACTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758A94271A65C59200E46135 /* HMACTests.swift */; };
 		758C764119B61AE500653BC6 /* Generics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758C764019B61AE500653BC6 /* Generics.swift */; };
-		758C764319B61DE900653BC6 /* UInt16Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758C764219B61DE900653BC6 /* UInt16Extension.swift */; };
 		758F3F781992F6CE0014BBDA /* UInt8Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758F3F771992F6CE0014BBDA /* UInt8Extension.swift */; };
 		759D481119B517BC005FF7FC /* BitExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759D481019B517BC005FF7FC /* BitExtension.swift */; };
 		75A663A61AA0CAD00052110B /* Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75A663A51AA0CAD00052110B /* Padding.swift */; };
@@ -56,17 +55,17 @@
 		75B601EB197D6A6C0009B53D /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 754BE45519693E190098E6F3 /* CryptoSwift.framework */; };
 		75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */; };
 		75BE4EB11B1E4A9F007A2B57 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BE4EB01B1E4A9F007A2B57 /* IntegerConvertible.swift */; };
-		75D63F751BB711270041579B /* BytesSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F741BB711270041579B /* BytesSequence.swift */; settings = {ASSET_TAGS = (); }; };
-		75D63F771BB840050041579B /* ArraySlice<UInt8>+Bytes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F761BB840050041579B /* ArraySlice<UInt8>+Bytes.swift */; settings = {ASSET_TAGS = (); }; };
+		75D63F751BB711270041579B /* BytesSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F741BB711270041579B /* BytesSequence.swift */; };
+		75D63F771BB840050041579B /* ArraySlice<UInt8>+Bytes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D63F761BB840050041579B /* ArraySlice<UInt8>+Bytes.swift */; };
 		75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2319B60C08007CB2A4 /* Operators.swift */; };
 		75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */; };
 		75D94E2819B60DDE007CB2A4 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */; };
 		75EB380119ABDD710002375A /* ChaCha20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EB380019ABDD710002375A /* ChaCha20.swift */; };
-		75FB9C9A1BB8A4BD009CAFC5 /* AES+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C951BB8A4BD009CAFC5 /* AES+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
-		75FB9C9B1BB8A4BD009CAFC5 /* Array<UInt8>+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C961BB8A4BD009CAFC5 /* Array<UInt8>+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
-		75FB9C9C1BB8A4BD009CAFC5 /* ChaCha20+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C971BB8A4BD009CAFC5 /* ChaCha20+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
-		75FB9C9D1BB8A4BD009CAFC5 /* NSData+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C981BB8A4BD009CAFC5 /* NSData+Extension.swift */; settings = {ASSET_TAGS = (); }; };
-		75FB9C9E1BB8A4BD009CAFC5 /* Utils+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C991BB8A4BD009CAFC5 /* Utils+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
+		75FB9C9A1BB8A4BD009CAFC5 /* AES+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C951BB8A4BD009CAFC5 /* AES+Foundation.swift */; };
+		75FB9C9B1BB8A4BD009CAFC5 /* Array<UInt8>+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C961BB8A4BD009CAFC5 /* Array<UInt8>+Foundation.swift */; };
+		75FB9C9C1BB8A4BD009CAFC5 /* ChaCha20+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C971BB8A4BD009CAFC5 /* ChaCha20+Foundation.swift */; };
+		75FB9C9D1BB8A4BD009CAFC5 /* NSData+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C981BB8A4BD009CAFC5 /* NSData+Extension.swift */; };
+		75FB9C9E1BB8A4BD009CAFC5 /* Utils+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75FB9C991BB8A4BD009CAFC5 /* Utils+Foundation.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -139,8 +138,8 @@
 		75153D4119AA3C7900750381 /* SHA2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA2.swift; sourceTree = "<group>"; };
 		75164E4819AD30AC00737F30 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
 		751C5C3C19B26B000094C75D /* Poly1305.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305.swift; sourceTree = "<group>"; };
+		75232CCD1BC6E6AC007F68B1 /* CSArrayType+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSArrayType+Extensions.swift"; sourceTree = "<group>"; };
 		752E087A199FF27C005B0EA0 /* SHA1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA1.swift; sourceTree = "<group>"; };
-		753403F61BB898470039099C /* Array<UInt8>+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array<UInt8>+Extension.swift"; sourceTree = "<group>"; };
 		75445820196AA2A5002FF20E /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
 		7547195019931802002FA5F1 /* IntExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntExtension.swift; sourceTree = "<group>"; };
 		754BE45519693E190098E6F3 /* CryptoSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CryptoSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -164,7 +163,6 @@
 		758A94251A65AEB100E46135 /* HMAC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMAC.swift; sourceTree = "<group>"; };
 		758A94271A65C59200E46135 /* HMACTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMACTests.swift; sourceTree = "<group>"; };
 		758C764019B61AE500653BC6 /* Generics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Generics.swift; sourceTree = "<group>"; };
-		758C764219B61DE900653BC6 /* UInt16Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt16Extension.swift; sourceTree = "<group>"; };
 		758F3F771992F6CE0014BBDA /* UInt8Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt8Extension.swift; sourceTree = "<group>"; };
 		7599C9C5199EA28700A3988B /* String+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
 		759D481019B517BC005FF7FC /* BitExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BitExtension.swift; sourceTree = "<group>"; };
@@ -255,12 +253,11 @@
 				759D481019B517BC005FF7FC /* BitExtension.swift */,
 				7547195019931802002FA5F1 /* IntExtension.swift */,
 				758F3F771992F6CE0014BBDA /* UInt8Extension.swift */,
-				758C764219B61DE900653BC6 /* UInt16Extension.swift */,
 				75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */,
 				75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */,
 				754C8FEC19979F94005AD904 /* Array+Extension.swift */,
-				753403F61BB898470039099C /* Array<UInt8>+Extension.swift */,
 				75D63F761BB840050041579B /* ArraySlice<UInt8>+Bytes.swift */,
+				75232CCD1BC6E6AC007F68B1 /* CSArrayType+Extensions.swift */,
 				7599C9C5199EA28700A3988B /* String+Extension.swift */,
 				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				75164E4819AD30AC00737F30 /* Utils.swift */,
@@ -470,10 +467,10 @@
 				75FB9C9C1BB8A4BD009CAFC5 /* ChaCha20+Foundation.swift in Sources */,
 				759D481119B517BC005FF7FC /* BitExtension.swift in Sources */,
 				754C8FED19979F94005AD904 /* Array+Extension.swift in Sources */,
+				75232CCE1BC6E6AC007F68B1 /* CSArrayType+Extensions.swift in Sources */,
 				7539E3291B3B4A530037F4E1 /* MD5.swift in Sources */,
 				75FB9C9B1BB8A4BD009CAFC5 /* Array<UInt8>+Foundation.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
-				758C764319B61DE900653BC6 /* UInt16Extension.swift in Sources */,
 				75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */,
 				75FB9C9D1BB8A4BD009CAFC5 /* NSData+Extension.swift in Sources */,
 				7539E32D1B3B4E950037F4E1 /* SHA1.swift in Sources */,
@@ -485,7 +482,6 @@
 				75D63F771BB840050041579B /* ArraySlice<UInt8>+Bytes.swift in Sources */,
 				754C30B71AA13BC000E6FFA4 /* PKCS7.swift in Sources */,
 				755111E819B7B7DF00C2AD86 /* Authenticator.swift in Sources */,
-				753403F71BB898470039099C /* Array<UInt8>+Extension.swift in Sources */,
 				754DD76E19A149AF00E52288 /* HashProtocol.swift in Sources */,
 				758C764119B61AE500653BC6 /* Generics.swift in Sources */,
 				758F3F781992F6CE0014BBDA /* UInt8Extension.swift in Sources */,

+ 0 - 59
CryptoSwift/Array<UInt8>+Extension.swift

@@ -1,59 +0,0 @@
-//
-//  ArrayUInt8+CryptoSwift.swift
-//  CryptoSwift
-//
-//  Created by Marcin Krzyzanowski on 27/09/15.
-//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
-//
-
-extension Array where Element: _UInt8Type {
-    
-    public func toHexString() -> String {
-        return self.lazy.reduce("") { $0 + String(format:"%02x", $1 as! UInt8) }
-    }
-    
-    public func md5() -> [UInt8]? {
-        return Hash.md5(Element.arrayValue(self)).calculate()
-    }
-    
-    public func sha1() -> [UInt8]? {
-        return Hash.sha1(Element.arrayValue(self)).calculate()
-    }
-
-    public func sha224() -> [UInt8]? {
-        return Hash.sha224(Element.arrayValue(self)).calculate()
-    }
-
-    public func sha256() -> [UInt8]? {
-        return Hash.sha256(Element.arrayValue(self)).calculate()
-    }
-
-    public func sha384() -> [UInt8]? {
-        return Hash.sha384(Element.arrayValue(self)).calculate()
-    }
-
-    public func sha512() -> [UInt8]? {
-        return Hash.sha512(Element.arrayValue(self)).calculate()
-    }
-    
-    public func crc32() -> [UInt8]? {
-        return Hash.crc32(Element.arrayValue(self)).calculate()
-    }
-
-    public func crc16() -> [UInt8]? {
-        return Hash.crc16(Element.arrayValue(self)).calculate()
-    }
-
-    public func encrypt(cipher: Cipher) throws -> [UInt8]? {
-        return try cipher.encrypt(Element.arrayValue(self))
-    }
-    
-    public func decrypt(cipher: Cipher) throws -> [UInt8]? {
-        return try cipher.decrypt(Element.arrayValue(self))
-    }
-
-    public func authenticate(authenticator: Authenticator) -> [UInt8]? {
-        return authenticator.authenticate(Element.arrayValue(self))
-    }
-
-}

+ 14 - 3
CryptoSwift/Authenticator.swift

@@ -10,6 +10,11 @@
 *  Message Authentication
 */
 public enum Authenticator {
+    
+    public enum Error: ErrorType {
+        case AuthenticateError
+    }
+    
     /**
     Poly1305
     
@@ -23,12 +28,18 @@ public enum Authenticator {
     
     - returns: 16-byte message authentication code
     */
-    public func authenticate(message: [UInt8]) -> [UInt8]? {
+    public func authenticate(message: [UInt8]) throws -> [UInt8] {
         switch (self) {
         case .Poly1305(let key):
-            return CryptoSwift.Poly1305.authenticate(key: key, message: message)
+            guard let auth = CryptoSwift.Poly1305.authenticate(key: key, message: message) else {
+                throw Error.AuthenticateError
+            }
+            return auth
         case .HMAC(let key, let variant):
-            return CryptoSwift.HMAC.authenticate(key: key, message: message, variant: variant)
+            guard let auth = CryptoSwift.HMAC.authenticate(key: key, message: message, variant: variant) else {
+                throw Error.AuthenticateError
+            }
+            return auth
         }
     }
 }

+ 68 - 0
CryptoSwift/CSArrayType+Extensions.swift

@@ -0,0 +1,68 @@
+//
+//  _ArrayType+Extensions.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 08/10/15.
+//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+public protocol CSArrayType: _ArrayType {
+    func cs_arrayValue() -> [Generator.Element]
+}
+
+extension Array: CSArrayType {
+    public func cs_arrayValue() -> [Generator.Element] {
+        return self
+    }
+}
+
+public extension CSArrayType where Generator.Element == UInt8 {
+    
+    public func toHexString() -> String {
+        return self.lazy.reduce("") { $0 + String(format:"%02x", $1) }
+    }
+    
+    public func md5() -> [Generator.Element] {
+        return Hash.md5(cs_arrayValue()).calculate()
+    }
+    
+    public func sha1() -> [Generator.Element] {
+        return Hash.sha1(cs_arrayValue()).calculate()
+    }
+    
+    public func sha224() -> [Generator.Element] {
+        return Hash.sha224(cs_arrayValue()).calculate()
+    }
+    
+    public func sha256() -> [Generator.Element] {
+        return Hash.sha256(cs_arrayValue()).calculate()
+    }
+    
+    public func sha384() -> [Generator.Element] {
+        return Hash.sha384(cs_arrayValue()).calculate()
+    }
+    
+    public func sha512() -> [Generator.Element] {
+        return Hash.sha512(cs_arrayValue()).calculate()
+    }
+    
+    public func crc32() -> [Generator.Element] {
+        return Hash.crc32(cs_arrayValue()).calculate()
+    }
+    
+    public func crc16() -> [Generator.Element] {
+        return Hash.crc16(cs_arrayValue()).calculate()
+    }
+    
+    public func encrypt(cipher: Cipher) throws -> [Generator.Element] {
+        return try cipher.encrypt(cs_arrayValue())
+    }
+    
+    public func decrypt(cipher: Cipher) throws -> [Generator.Element] {
+        return try cipher.decrypt(cs_arrayValue())
+    }
+    
+    public func authenticate(authenticator: Authenticator) throws -> [Generator.Element] {
+        return try authenticator.authenticate(cs_arrayValue())
+    }
+}

+ 0 - 6
CryptoSwift/Foundation/Array<UInt8>+Foundation.swift

@@ -8,12 +8,6 @@
 
 import Foundation
 
-extension _UInt8Type {
-    private static func Zero() -> Self {
-        return (0 as UInt8) as! Self
-    }
-}
-
 extension Array where Element: _UInt8Type {
     public init(_ data: NSData) {
         self = Array<Element>(count: data.length, repeatedValue: Element.Zero())

+ 14 - 16
CryptoSwift/Foundation/NSData+Extension.swift

@@ -30,61 +30,59 @@ extension NSData {
         return UInt16(s)
     }
     
-    @nonobjc public func md5() -> NSData? {
-        guard let result = Hash.md5(self.arrayOfBytes()).calculate() else { return nil }
+    @nonobjc public func md5() -> NSData {
+        let result = Hash.md5(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
     public func sha1() -> NSData? {
-        guard let result = Hash.sha1(self.arrayOfBytes()).calculate() else { return nil }
+        let result = Hash.sha1(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
     public func sha224() -> NSData? {
-        guard let result = Hash.sha224(self.arrayOfBytes()).calculate() else { return nil }
+        let result = Hash.sha224(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
     public func sha256() -> NSData? {
-        guard let result = Hash.sha256(self.arrayOfBytes()).calculate() else { return nil }
+        let result = Hash.sha256(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
     public func sha384() -> NSData? {
-        guard let result = Hash.sha384(self.arrayOfBytes()).calculate() else { return nil }
+        let result = Hash.sha384(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
     public func sha512() -> NSData? {
-        guard let result = Hash.sha512(self.arrayOfBytes()).calculate() else { return nil }
+        let result = Hash.sha512(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
     public func crc32() -> NSData? {
-        guard let result = Hash.crc32(self.arrayOfBytes()).calculate() else { return nil }
+        let result = Hash.crc32(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
     public func crc16() -> NSData? {
-        guard let result = Hash.crc16(self.arrayOfBytes()).calculate() else { return nil }
+        let result = Hash.crc16(self.arrayOfBytes()).calculate()
         return NSData.withBytes(result)
     }
 
-    public func encrypt(cipher: Cipher) throws -> NSData? {
+    public func encrypt(cipher: Cipher) throws -> NSData {
         let encrypted = try cipher.encrypt(self.arrayOfBytes())
         return NSData.withBytes(encrypted)
     }
 
-    public func decrypt(cipher: Cipher) throws -> NSData? {
+    public func decrypt(cipher: Cipher) throws -> NSData {
         let decrypted = try cipher.decrypt(self.arrayOfBytes())
         return NSData.withBytes(decrypted)
     }
     
-    public func authenticate(authenticator: Authenticator) -> NSData? {
-        if let result = authenticator.authenticate(self.arrayOfBytes()) {
-            return NSData.withBytes(result)
-        }
-        return nil
+    public func authenticate(authenticator: Authenticator) throws -> NSData {
+        let result = try authenticator.authenticate(self.arrayOfBytes())
+        return NSData.withBytes(result)
     }
 }
 

+ 1 - 1
CryptoSwift/Hash.swift

@@ -13,7 +13,7 @@ public enum Hash {
     case crc32(Array<UInt8>)
     case crc16(Array<UInt8>)
     
-    public func calculate() -> [UInt8]? {
+    public func calculate() -> [UInt8] {
         switch self {
         case md5(let bytes):
             return MD5(bytes).calculate()

+ 22 - 23
CryptoSwift/String+Extension.swift

@@ -9,48 +9,47 @@
 /** String extension */
 extension String {
     
-    public func md5() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).md5()?.toHexString()
+    public func md5() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).md5().toHexString()
     }
     
-    public func sha1() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).sha1()?.toHexString()
+    public func sha1() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).sha1().toHexString()
     }
 
-    public func sha224() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).sha224()?.toHexString()
+    public func sha224() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).sha224().toHexString()
     }
 
-    public func sha256() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).sha256()?.toHexString()
+    public func sha256() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).sha256().toHexString()
     }
 
-    public func sha384() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).sha384()?.toHexString()
+    public func sha384() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).sha384().toHexString()
     }
 
-    public func sha512() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).sha512()?.toHexString()
+    public func sha512() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).sha512().toHexString()
     }
 
-    public func crc32() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).crc32()?.toHexString()
+    public func crc32() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).crc32().toHexString()
     }
 
-    public func crc16() -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).crc16()?.toHexString()
+    public func crc16() -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).crc16().toHexString()
     }
 
-    public func encrypt(cipher: Cipher) throws -> String? {
-        return try self.utf8.lazy.map({ $0 as UInt8 }).encrypt(cipher)?.toHexString()
+    public func encrypt(cipher: Cipher) throws -> String {
+        return try self.utf8.lazy.map({ $0 as UInt8 }).encrypt(cipher).toHexString()
     }
 
-    public func decrypt(cipher: Cipher) throws -> String? {
-        return try self.utf8.lazy.map({ $0 as UInt8 }).decrypt(cipher)?.toHexString()
+    public func decrypt(cipher: Cipher) throws -> String {
+        return try self.utf8.lazy.map({ $0 as UInt8 }).decrypt(cipher).toHexString()
     }
     
-    public func authenticate(authenticator: Authenticator) -> String? {
-        return self.utf8.lazy.map({ $0 as UInt8 }).authenticate(authenticator)?.toHexString()
+    public func authenticate(authenticator: Authenticator) throws -> String {
+        return  try self.utf8.lazy.map({ $0 as UInt8 }).authenticate(authenticator).toHexString()
     }
-
 }

+ 0 - 45
CryptoSwift/UInt16Extension.swift

@@ -1,45 +0,0 @@
-//
-//  UInt16Extension.swift
-//  CryptoSwift
-//
-//  Created by Marcin Krzyzanowski on 02/09/14.
-//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
-//
-
-import Darwin
-
-/** Shift bits */
-extension UInt16 {
-    /** Shift bits to the right. All bits are shifted (including sign bit) */
-    mutating func shiftRight(count: UInt16) -> UInt16 {
-        if (self == 0) {
-            return self;
-        }
-
-        let bitsCount = UInt16(sizeofValue(self) * 8)
-
-        if (count >= bitsCount) {
-            return 0
-        }
-
-        let maxBitsForValue = UInt16(floor(log2(Double(self) + 1)))
-        let shiftCount = Swift.min(count, maxBitsForValue - 1)
-        var shiftedValue:UInt16 = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            let byte = 1 << bitIdx
-            if ((self & byte) == byte) {
-                shiftedValue = shiftedValue | (byte >> shiftCount)
-            }
-        }
-        self = shiftedValue
-        return self
-    }
-}
-
-/** shift right and assign with bits truncation */
-func &>> (lhs: UInt16, rhs: UInt16) -> UInt16 {
-    var l = lhs;
-    l.shiftRight(rhs)
-    return l
-}

+ 4 - 7
CryptoSwift/UInt8Extension.swift

@@ -8,18 +8,15 @@
 
 import Darwin
 
-public protocol _UInt8Type {
-    /// [Hack](https://twitter.com/jckarter/status/648235521127256064) for array `value`
-    static func arrayValue(array: [Self]) -> [UInt8]
-}
+public protocol _UInt8Type { }
+extension UInt8: _UInt8Type {}
 
 extension _UInt8Type {
-    static public func arrayValue(array: [Self]) -> [Self] {
-        return array
+    static func Zero() -> Self {
+        return 0 as! Self
     }
 }
 
-extension UInt8: _UInt8Type {}
 
 /** casting */
 extension UInt8 {

+ 1 - 3
CryptoSwiftTests/ChaCha20Tests.swift

@@ -58,9 +58,7 @@ final class ChaCha20Tests: XCTestCase {
                 let messageData = NSData(bytes: message, length: message.count);
                 let encrypted2 = try! messageData.encrypt(Cipher.ChaCha20(setup))
                 XCTAssertNotNil(encrypted2, "")
-                if let encrypted2 = encrypted2 {
-                    XCTAssertEqual(NSData.withBytes(encrypted), encrypted2, "ChaCha20 extension failed")
-                }
+                XCTAssertEqual(NSData.withBytes(encrypted), encrypted2, "ChaCha20 extension failed")
             } catch Cipher.Error.EncryptError {
                 XCTAssert(false, "Encryption failed")
             } catch Cipher.Error.DecryptError {

+ 10 - 10
CryptoSwiftTests/HMACTests.swift

@@ -24,8 +24,8 @@ final class HMACTests: XCTestCase {
         let msg:[UInt8] = []
         let expectedMac:[UInt8] = [0x74,0xe6,0xf7,0x29,0x8a,0x9c,0x2d,0x16,0x89,0x35,0xf5,0x8c,0x00,0x1b,0xad,0x88]
         
-        let hmac = Authenticator.HMAC(key: key, variant: .md5).authenticate(msg)
-        XCTAssertEqual(hmac!, expectedMac, "Invalid authentication result")
+        let hmac = try! Authenticator.HMAC(key: key, variant: .md5).authenticate(msg)
+        XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
     
     func testSHA1() {
@@ -33,8 +33,8 @@ final class HMACTests: XCTestCase {
         let msg:[UInt8] = []
         let expectedMac:[UInt8] = [0xfb,0xdb,0x1d,0x1b,0x18,0xaa,0x6c,0x08,0x32,0x4b,0x7d,0x64,0xb7,0x1f,0xb7,0x63,0x70,0x69,0x0e,0x1d]
         
-        let hmac = Authenticator.HMAC(key: key, variant: .sha1).authenticate(msg)
-        XCTAssertEqual(hmac!, expectedMac, "Invalid authentication result")
+        let hmac = try! Authenticator.HMAC(key: key, variant: .sha1).authenticate(msg)
+        XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 
     func testSHA256() {
@@ -42,8 +42,8 @@ final class HMACTests: XCTestCase {
         let msg:[UInt8] = []
         let expectedMac:[UInt8] = [0xb6,0x13,0x67,0x9a,0x08,0x14,0xd9,0xec,0x77,0x2f,0x95,0xd7,0x78,0xc3,0x5f,0xc5,0xff,0x16,0x97,0xc4,0x93,0x71,0x56,0x53,0xc6,0xc7,0x12,0x14,0x42,0x92,0xc5,0xad]
         
-        let hmac = Authenticator.HMAC(key: key, variant: .sha256).authenticate(msg)
-        XCTAssertEqual(hmac!, expectedMac, "Invalid authentication result")
+        let hmac = try! Authenticator.HMAC(key: key, variant: .sha256).authenticate(msg)
+        XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 
     func testSHA384() {
@@ -51,8 +51,8 @@ final class HMACTests: XCTestCase {
         let msg:[UInt8] = []
         let expectedMac:[UInt8] = [0x6c, 0x1f, 0x2e, 0xe9, 0x38, 0xfa, 0xd2, 0xe2, 0x4b, 0xd9, 0x12, 0x98, 0x47, 0x43, 0x82, 0xca, 0x21, 0x8c, 0x75, 0xdb, 0x3d, 0x83, 0xe1, 0x14, 0xb3, 0xd4, 0x36, 0x77, 0x76, 0xd1, 0x4d, 0x35, 0x51, 0x28, 0x9e, 0x75, 0xe8, 0x20, 0x9c, 0xd4, 0xb7, 0x92, 0x30, 0x28, 0x40, 0x23, 0x4a, 0xdc]
 
-        let hmac = Authenticator.HMAC(key: key, variant: .sha384).authenticate(msg)
-        XCTAssertEqual(hmac!, expectedMac, "Invalid authentication result")
+        let hmac = try! Authenticator.HMAC(key: key, variant: .sha384).authenticate(msg)
+        XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 
     func testSHA512() {
@@ -60,7 +60,7 @@ final class HMACTests: XCTestCase {
         let msg:[UInt8] = []
         let expectedMac:[UInt8] = [0xb9, 0x36, 0xce, 0xe8, 0x6c, 0x9f, 0x87, 0xaa, 0x5d, 0x3c, 0x6f, 0x2e, 0x84, 0xcb, 0x5a, 0x42, 0x39, 0xa5, 0xfe, 0x50, 0x48, 0x0a, 0x6e, 0xc6, 0x6b, 0x70, 0xab, 0x5b, 0x1f, 0x4a, 0xc6, 0x73, 0x0c, 0x6c, 0x51, 0x54, 0x21, 0xb3, 0x27, 0xec, 0x1d, 0x69, 0x40, 0x2e, 0x53, 0xdf, 0xb4, 0x9a, 0xd7, 0x38, 0x1e, 0xb0, 0x67, 0xb3, 0x38, 0xfd, 0x7b, 0x0c, 0xb2, 0x22, 0x47, 0x22, 0x5d, 0x47]
 
-        let hmac = Authenticator.HMAC(key: key, variant: .sha512).authenticate(msg)
-        XCTAssertEqual(hmac!, expectedMac, "Invalid authentication result")
+        let hmac = try! Authenticator.HMAC(key: key, variant: .sha512).authenticate(msg)
+        XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 }

+ 27 - 94
CryptoSwiftTests/HashTests.swift

@@ -19,55 +19,25 @@ final class CryptoSwiftTests: XCTestCase {
         super.tearDown()
     }
     
-    func testMD5() {
-        let data1 = [0x31, 0x32, 0x33] as [UInt8] // "1", "2", "3"
-        if let hash = Hash.md5(data1).calculate() {
-            XCTAssertEqual(hash, [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
-        } else {
-            XCTAssert(false, "Missing result")
-        }
-        
-        let string:NSString = ""
-        let data:NSData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
-        if let hash = Hash.md5(data.arrayOfBytes()).calculate() {
-            XCTAssertEqual(hash, [0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e], "MD5 calculation failed")
-        } else {
-            XCTAssert(false, "Missing result")
-        }
-        
-        if let hash = "123".md5() {
-            XCTAssertEqual(hash, "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
-        }
-        
-        if let hash = "".md5() {
-            XCTAssertEqual(hash, "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
-        } else {
-            XCTAssert(false, "Hash for empty string is missing")
-        }
-        
-        if let hash = "a".md5() {
-            XCTAssertEqual(hash, "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
-        }
-        
-        if let hash = "abc".md5() {
-            XCTAssertEqual(hash, "900150983cd24fb0d6963f7d28e17f72", "MD5 calculation failed")
-        }
-        
-        if let hash = "message digest".md5() {
-            XCTAssertEqual(hash, "f96b697d7cb7938d525a2f31aaf161d0", "MD5 calculation failed")
-        }
-
-        if let hash = "abcdefghijklmnopqrstuvwxyz".md5() {
-            XCTAssertEqual(hash, "c3fcd3d76192e4007dfb496cca67e13b", "MD5 calculation failed")
-        }
+    func testMD5_data() {
+        let data = [0x31, 0x32, 0x33] as [UInt8] // "1", "2", "3"
+        XCTAssertEqual(Hash.md5(data).calculate(), [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
+    }
 
-        if let hash = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".md5() {
-            XCTAssertEqual(hash, "d174ab98d277d9f5a5611c2c9f419d9f", "MD5 calculation failed")
-        }
+    func testMD5_emptyString() {
+        let data:NSData = "".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
+        XCTAssertEqual(Hash.md5(data.arrayOfBytes()).calculate(), [0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e], "MD5 calculation failed")
+    }
 
-        if let hash = "12345678901234567890123456789012345678901234567890123456789012345678901234567890".md5() {
-            XCTAssertEqual(hash, "57edf4a22be3c955ac49da2e2107b67a", "MD5 calculation failed")
-        }
+    func testMD5_string() {
+        XCTAssertEqual("123".md5(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
+        XCTAssertEqual("".md5(), "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
+        XCTAssertEqual("a".md5(), "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
+        XCTAssertEqual("abc".md5(), "900150983cd24fb0d6963f7d28e17f72", "MD5 calculation failed")
+        XCTAssertEqual("message digest".md5(), "f96b697d7cb7938d525a2f31aaf161d0", "MD5 calculation failed")
+        XCTAssertEqual("abcdefghijklmnopqrstuvwxyz".md5(), "c3fcd3d76192e4007dfb496cca67e13b", "MD5 calculation failed")
+        XCTAssertEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".md5(), "d174ab98d277d9f5a5611c2c9f419d9f", "MD5 calculation failed")
+        XCTAssertEqual("12345678901234567890123456789012345678901234567890123456789012345678901234567890".md5(), "57edf4a22be3c955ac49da2e2107b67a", "MD5 calculation failed")
     }
     
     func testMD5PerformanceSwift() {
@@ -105,20 +75,9 @@ final class CryptoSwiftTests: XCTestCase {
             XCTAssertEqual(hash.toHexString(), "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "SHA1 calculation failed");
         }
         
-        if let hash = "abc".sha1() {
-            XCTAssertEqual(hash, "a9993e364706816aba3e25717850c26c9cd0d89d", "SHA1 calculation failed")
-        }
-
-        if let hash = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1() {
-            XCTAssertEqual(hash, "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
-        }
-        
-        if let hash = "".sha1() {
-            XCTAssertEqual(hash, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
-        } else {
-            XCTAssert(false, "SHA1 calculation failed")
-        }
-        
+        XCTAssertEqual("abc".sha1(), "a9993e364706816aba3e25717850c26c9cd0d89d", "SHA1 calculation failed")
+        XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
+        XCTAssertEqual("".sha1(), "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
     }
     
     func testSHA224() {
@@ -134,16 +93,8 @@ final class CryptoSwiftTests: XCTestCase {
             XCTAssertEqual(hash.toHexString(), "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", "SHA256 calculation failed");
         }
         
-        if let hash = "Rosetta code".sha256() {
-            XCTAssertEqual(hash, "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
-        }
-        
-        if let hash = "".sha256() {
-            XCTAssertEqual(hash, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 calculation failed")
-        } else {
-            XCTAssert(false, "SHA256 calculation failed")
-        }
-
+        XCTAssertEqual("Rosetta code".sha256(), "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
+        XCTAssertEqual("".sha256(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "SHA256 calculation failed")
     }
 
     func testSHA384() {
@@ -152,15 +103,8 @@ final class CryptoSwiftTests: XCTestCase {
             XCTAssertEqual(hash.toHexString(), "9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f", "SHA384 calculation failed");
         }
         
-        if let hash = "The quick brown fox jumps over the lazy dog.".sha384() {
-            XCTAssertEqual(hash, "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
-        }
-        
-        if let hash = "".sha384() {
-            XCTAssertEqual(hash, "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "SHA384 calculation failed")
-        } else {
-            XCTAssert(false, "SHA384 calculation failed")
-        }
+        XCTAssertEqual("The quick brown fox jumps over the lazy dog.".sha384(), "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
+        XCTAssertEqual("".sha384(), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "SHA384 calculation failed")
     }
 
     func testSHA512() {
@@ -169,15 +113,8 @@ final class CryptoSwiftTests: XCTestCase {
             XCTAssertEqual(hash.toHexString(), "3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2", "SHA512 calculation failed");
         }
         
-        if let hash = "The quick brown fox jumps over the lazy dog.".sha512() {
-            XCTAssertEqual(hash, "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed", "SHA512 calculation failed");
-        }
-        
-        if let hash = "".sha512() {
-            XCTAssertEqual(hash, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "SHA512 calculation failed")
-        } else {
-            XCTAssert(false, "SHA512 calculation failed")
-        }
+        XCTAssertEqual("The quick brown fox jumps over the lazy dog.".sha512(), "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed", "SHA512 calculation failed");
+        XCTAssertEqual("".sha512(), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "SHA512 calculation failed")
     }
     
     func testCRC32() {
@@ -186,11 +123,7 @@ final class CryptoSwiftTests: XCTestCase {
             XCTAssertEqual(crc.toHexString(), "884863d2", "CRC32 calculation failed");
         }
         
-        if let crc = "".crc32() {
-            XCTAssertEqual(crc, "00000000", "CRC32 calculation failed");
-        } else {
-            XCTAssert(false, "CRC32 calculation failed")
-        }
+        XCTAssertEqual("".crc32(), "00000000", "CRC32 calculation failed");
     }
     
     func testCRC16() {

+ 4 - 10
CryptoSwiftTests/Poly1305Tests.swift

@@ -24,18 +24,12 @@ final class Poly1305Tests: XCTestCase {
         let msg:[UInt8] = [0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1]
         let expectedMac:[UInt8] = [0xdd,0xb9,0xda,0x7d,0xdd,0x5e,0x52,0x79,0x27,0x30,0xed,0x5c,0xda,0x5f,0x90,0xa4]
         
-        if let mac = Authenticator.Poly1305(key: key).authenticate(msg) {
-            XCTAssertEqual(mac, expectedMac, "Invalid authentication result")
-        } else {
-            XCTFail("Missing MAC")
-        }
+        let mac = try! Authenticator.Poly1305(key: key).authenticate(msg)
+        XCTAssertEqual(mac, expectedMac, "Invalid authentication result")
         
         // extensions
         let msgData = NSData.withBytes(msg)
-        if let mac2 = msgData.authenticate(Authenticator.Poly1305(key: key)) {
-            XCTAssertEqual(mac2, NSData.withBytes(expectedMac), "Invalid authentication result")
-        } else {
-            XCTFail("Missing MAC")
-        }
+        let mac2 = try! msgData.authenticate(Authenticator.Poly1305(key: key))
+        XCTAssertEqual(mac2, NSData.withBytes(expectedMac), "Invalid authentication result")
     }
 }