Browse Source

Merge branch 'swift3-snapshots' into swift3

Marcin Krzyżanowski 9 years ago
parent
commit
fdfbc312b6
45 changed files with 732 additions and 445 deletions
  1. 77 53
      CryptoSwift.xcodeproj/project.pbxproj
  2. 16 0
      CryptoSwiftTests/AESTests.swift
  3. 217 0
      CryptoSwiftTests/Access.swift
  4. 23 9
      CryptoSwiftTests/ExtensionsTest.swift
  5. 5 5
      CryptoSwiftTests/HMACTests.swift
  6. 7 8
      CryptoSwiftTests/HashTests.swift
  7. 2 2
      CryptoSwiftTests/Poly1305Tests.swift
  8. 11 11
      CryptoSwiftTests/RabbitTests.swift
  9. 3 3
      README.md
  10. 6 6
      Sources/CryptoSwift/AES.swift
  11. 15 0
      Sources/CryptoSwift/Array+Extension.swift
  12. 3 36
      Sources/CryptoSwift/Authenticator.swift
  13. 1 1
      Sources/CryptoSwift/BlockMode/BlockMode.swift
  14. 2 2
      Sources/CryptoSwift/BlockMode/CBC.swift
  15. 2 2
      Sources/CryptoSwift/BlockMode/CFB.swift
  16. 2 2
      Sources/CryptoSwift/BlockMode/CTR.swift
  17. 1 1
      Sources/CryptoSwift/BlockMode/ECB.swift
  18. 2 2
      Sources/CryptoSwift/BlockMode/OFB.swift
  19. 2 2
      Sources/CryptoSwift/BlockMode/PCBC.swift
  20. 11 11
      Sources/CryptoSwift/CSArrayType+Extensions.swift
  21. 8 5
      Sources/CryptoSwift/ChaCha20.swift
  22. 15 5
      Sources/CryptoSwift/Checksum.swift
  23. 76 0
      Sources/CryptoSwift/Collection+Extension.swift
  24. 10 19
      Sources/CryptoSwift/Foundation/Data+Extension.swift
  25. 9 9
      Sources/CryptoSwift/Foundation/Rabbit+Foundation.swift
  26. 4 27
      Sources/CryptoSwift/Generics.swift
  27. 20 12
      Sources/CryptoSwift/HMAC.swift
  28. 24 27
      Sources/CryptoSwift/Hash.swift
  29. 11 15
      Sources/CryptoSwift/Int+Extension.swift
  30. 1 1
      Sources/CryptoSwift/MD5.swift
  31. 4 19
      Sources/CryptoSwift/Operators.swift
  32. 2 2
      Sources/CryptoSwift/PKCS5/PBKDF1.swift
  33. 18 12
      Sources/CryptoSwift/PKCS5/PBKDF2.swift
  34. 35 28
      Sources/CryptoSwift/Poly1305.swift
  35. 13 9
      Sources/CryptoSwift/Rabbit.swift
  36. 3 3
      Sources/CryptoSwift/SHA1.swift
  37. 7 7
      Sources/CryptoSwift/SHA2.swift
  38. 9 10
      Sources/CryptoSwift/String+Extension.swift
  39. 18 0
      Sources/CryptoSwift/UInt16+Extension.swift
  40. 10 15
      Sources/CryptoSwift/UInt32+Extension.swift
  41. 18 0
      Sources/CryptoSwift/UInt64+Extension.swift
  42. 0 23
      Sources/CryptoSwift/UInt64Extension.swift
  43. 2 2
      Sources/CryptoSwift/UInt8+Extension.swift
  44. 7 7
      Sources/CryptoSwift/Updatable.swift
  45. 0 32
      Sources/CryptoSwift/Utils.swift

+ 77 - 53
CryptoSwift.xcodeproj/project.pbxproj

@@ -44,6 +44,10 @@
 		755655C91D080E3F00F004E7 /* Cryptors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755655C61D080E3F00F004E7 /* Cryptors.swift */; };
 		755655CA1D080E3F00F004E7 /* Cryptors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755655C61D080E3F00F004E7 /* Cryptors.swift */; };
 		755FB1DA199E347D00475437 /* ExtensionsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 755FB1D9199E347D00475437 /* ExtensionsTest.swift */; };
+		756B575F1D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
+		756B57601D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
+		756B57611D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
+		756B57621D5131A4001C0C72 /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756B575E1D5131A4001C0C72 /* Collection+Extension.swift */; };
 		7574E5FE1CD02C8B00E96346 /* CSArrayType+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */; };
 		7574E5FF1CD02C9300E96346 /* CSArrayType+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */; };
 		7574E6001CD02C9300E96346 /* CSArrayType+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */; };
@@ -92,10 +96,10 @@
 		757BC9551C1CA5790093AAA9 /* ChaCha20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9201C1CA5790093AAA9 /* ChaCha20.swift */; };
 		757BC9561C1CA5790093AAA9 /* ChaCha20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9201C1CA5790093AAA9 /* ChaCha20.swift */; };
 		757BC9571C1CA5790093AAA9 /* ChaCha20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9201C1CA5790093AAA9 /* ChaCha20.swift */; };
-		757BC9601C1CA5790093AAA9 /* CRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* CRC.swift */; };
-		757BC9611C1CA5790093AAA9 /* CRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* CRC.swift */; };
-		757BC9621C1CA5790093AAA9 /* CRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* CRC.swift */; };
-		757BC9631C1CA5790093AAA9 /* CRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* CRC.swift */; };
+		757BC9601C1CA5790093AAA9 /* Checksum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* Checksum.swift */; };
+		757BC9611C1CA5790093AAA9 /* Checksum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* Checksum.swift */; };
+		757BC9621C1CA5790093AAA9 /* Checksum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* Checksum.swift */; };
+		757BC9631C1CA5790093AAA9 /* Checksum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9231C1CA5790093AAA9 /* Checksum.swift */; };
 		757BC9681C1CA5790093AAA9 /* CSArrayType+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9251C1CA5790093AAA9 /* CSArrayType+Extensions.swift */; };
 		757BC9691C1CA5790093AAA9 /* CSArrayType+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9251C1CA5790093AAA9 /* CSArrayType+Extensions.swift */; };
 		757BC96A1C1CA5790093AAA9 /* CSArrayType+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9251C1CA5790093AAA9 /* CSArrayType+Extensions.swift */; };
@@ -120,10 +124,10 @@
 		757BC9811C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */; };
 		757BC9821C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */; };
 		757BC9831C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */; };
-		757BC9841C1CA5790093AAA9 /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* IntExtension.swift */; };
-		757BC9851C1CA5790093AAA9 /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* IntExtension.swift */; };
-		757BC9861C1CA5790093AAA9 /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* IntExtension.swift */; };
-		757BC9871C1CA5790093AAA9 /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* IntExtension.swift */; };
+		757BC9841C1CA5790093AAA9 /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */; };
+		757BC9851C1CA5790093AAA9 /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */; };
+		757BC9861C1CA5790093AAA9 /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */; };
+		757BC9871C1CA5790093AAA9 /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */; };
 		757BC9881C1CA5790093AAA9 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92D1C1CA5790093AAA9 /* MD5.swift */; };
 		757BC9891C1CA5790093AAA9 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92D1C1CA5790093AAA9 /* MD5.swift */; };
 		757BC98A1C1CA5790093AAA9 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92D1C1CA5790093AAA9 /* MD5.swift */; };
@@ -164,18 +168,18 @@
 		757BC9B11C1CA5790093AAA9 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9371C1CA5790093AAA9 /* String+Extension.swift */; };
 		757BC9B21C1CA5790093AAA9 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9371C1CA5790093AAA9 /* String+Extension.swift */; };
 		757BC9B31C1CA5790093AAA9 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9371C1CA5790093AAA9 /* String+Extension.swift */; };
-		757BC9B41C1CA5790093AAA9 /* UInt8Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8Extension.swift */; };
-		757BC9B51C1CA5790093AAA9 /* UInt8Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8Extension.swift */; };
-		757BC9B61C1CA5790093AAA9 /* UInt8Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8Extension.swift */; };
-		757BC9B71C1CA5790093AAA9 /* UInt8Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8Extension.swift */; };
-		757BC9B81C1CA5790093AAA9 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32Extension.swift */; };
-		757BC9B91C1CA5790093AAA9 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32Extension.swift */; };
-		757BC9BA1C1CA5790093AAA9 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32Extension.swift */; };
-		757BC9BB1C1CA5790093AAA9 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32Extension.swift */; };
-		757BC9BC1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64Extension.swift */; };
-		757BC9BD1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64Extension.swift */; };
-		757BC9BE1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64Extension.swift */; };
-		757BC9BF1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64Extension.swift */; };
+		757BC9B41C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8+Extension.swift */; };
+		757BC9B51C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8+Extension.swift */; };
+		757BC9B61C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8+Extension.swift */; };
+		757BC9B71C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9381C1CA5790093AAA9 /* UInt8+Extension.swift */; };
+		757BC9B81C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32+Extension.swift */; };
+		757BC9B91C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32+Extension.swift */; };
+		757BC9BA1C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32+Extension.swift */; };
+		757BC9BB1C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9391C1CA5790093AAA9 /* UInt32+Extension.swift */; };
+		757BC9BC1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64+Extension.swift */; };
+		757BC9BD1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64+Extension.swift */; };
+		757BC9BE1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64+Extension.swift */; };
+		757BC9BF1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93A1C1CA5790093AAA9 /* UInt64+Extension.swift */; };
 		757BC9C01C1CA5790093AAA9 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93B1C1CA5790093AAA9 /* Utils.swift */; };
 		757BC9C11C1CA5790093AAA9 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93B1C1CA5790093AAA9 /* Utils.swift */; };
 		757BC9C21C1CA5790093AAA9 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC93B1C1CA5790093AAA9 /* Utils.swift */; };
@@ -203,6 +207,11 @@
 		758F58F31C8FB6E20054C377 /* OFB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758F58F01C8FB6E20054C377 /* OFB.swift */; };
 		758F58F41C8FB6E20054C377 /* OFB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758F58F01C8FB6E20054C377 /* OFB.swift */; };
 		75B601EB197D6A6C0009B53D /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 754BE45519693E190098E6F3 /* CryptoSwift.framework */; };
+		75C2E7681D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75C2E7671D55EFA1003D2BCA /* UInt16+Extension.swift */; };
+		75C2E7691D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75C2E7671D55EFA1003D2BCA /* UInt16+Extension.swift */; };
+		75C2E76A1D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75C2E7671D55EFA1003D2BCA /* UInt16+Extension.swift */; };
+		75C2E76B1D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75C2E7671D55EFA1003D2BCA /* UInt16+Extension.swift */; };
+		75C2E76D1D55F097003D2BCA /* Access.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75C2E76C1D55F097003D2BCA /* Access.swift */; };
 		75CB93251C8F5EC10087740D /* CBC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75CB93241C8F5EC10087740D /* CBC.swift */; };
 		75CB93261C8F5EC10087740D /* CBC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75CB93241C8F5EC10087740D /* CBC.swift */; };
 		75CB93271C8F5EC10087740D /* CBC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75CB93241C8F5EC10087740D /* CBC.swift */; };
@@ -354,6 +363,7 @@
 		755655C61D080E3F00F004E7 /* Cryptors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Cryptors.swift; path = Sources/CryptoSwift/Cryptors.swift; sourceTree = SOURCE_ROOT; };
 		755D27BC1D06DE6400C41692 /* CryptoSwift.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = CryptoSwift.playground; sourceTree = "<group>"; };
 		755FB1D9199E347D00475437 /* ExtensionsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionsTest.swift; sourceTree = "<group>"; };
+		756B575E1D5131A4001C0C72 /* Collection+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Collection+Extension.swift"; path = "Sources/CryptoSwift/Collection+Extension.swift"; sourceTree = SOURCE_ROOT; };
 		756BFDCA1A82B87300B9D9A4 /* Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bridging.h; sourceTree = "<group>"; };
 		7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSArrayType+Foundation.swift"; sourceTree = "<group>"; };
 		757BC8F71C1CA56A0093AAA9 /* AES+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AES+Foundation.swift"; sourceTree = "<group>"; };
@@ -367,7 +377,7 @@
 		757BC91D1C1CA5790093AAA9 /* Authenticator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Authenticator.swift; path = Sources/CryptoSwift/Authenticator.swift; sourceTree = SOURCE_ROOT; };
 		757BC91F1C1CA5790093AAA9 /* BytesSequence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BytesSequence.swift; path = Sources/CryptoSwift/BytesSequence.swift; sourceTree = SOURCE_ROOT; };
 		757BC9201C1CA5790093AAA9 /* ChaCha20.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ChaCha20.swift; path = Sources/CryptoSwift/ChaCha20.swift; sourceTree = SOURCE_ROOT; };
-		757BC9231C1CA5790093AAA9 /* CRC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CRC.swift; path = Sources/CryptoSwift/CRC.swift; sourceTree = SOURCE_ROOT; };
+		757BC9231C1CA5790093AAA9 /* Checksum.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Checksum.swift; path = Sources/CryptoSwift/Checksum.swift; sourceTree = SOURCE_ROOT; };
 		757BC9251C1CA5790093AAA9 /* CSArrayType+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "CSArrayType+Extensions.swift"; path = "Sources/CryptoSwift/CSArrayType+Extensions.swift"; sourceTree = SOURCE_ROOT; };
 		757BC9261C1CA5790093AAA9 /* Generics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Generics.swift; path = Sources/CryptoSwift/Generics.swift; sourceTree = SOURCE_ROOT; };
 		757BC9271C1CA5790093AAA9 /* Hash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Hash.swift; path = Sources/CryptoSwift/Hash.swift; sourceTree = SOURCE_ROOT; };
@@ -375,7 +385,7 @@
 		757BC9291C1CA5790093AAA9 /* HMAC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = HMAC.swift; path = Sources/CryptoSwift/HMAC.swift; sourceTree = SOURCE_ROOT; };
 		757BC92A1C1CA5790093AAA9 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Sources/CryptoSwift/Info.plist; sourceTree = SOURCE_ROOT; };
 		757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntegerConvertible.swift; path = Sources/CryptoSwift/IntegerConvertible.swift; sourceTree = SOURCE_ROOT; };
-		757BC92C1C1CA5790093AAA9 /* IntExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntExtension.swift; path = Sources/CryptoSwift/IntExtension.swift; sourceTree = SOURCE_ROOT; };
+		757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Int+Extension.swift"; path = "Sources/CryptoSwift/Int+Extension.swift"; sourceTree = SOURCE_ROOT; };
 		757BC92D1C1CA5790093AAA9 /* MD5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MD5.swift; path = Sources/CryptoSwift/MD5.swift; sourceTree = SOURCE_ROOT; };
 		757BC92E1C1CA5790093AAA9 /* Multiplatform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Multiplatform.swift; path = Sources/CryptoSwift/Multiplatform.swift; sourceTree = SOURCE_ROOT; };
 		757BC92F1C1CA5790093AAA9 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Operators.swift; path = Sources/CryptoSwift/Operators.swift; sourceTree = SOURCE_ROOT; };
@@ -386,9 +396,9 @@
 		757BC9351C1CA5790093AAA9 /* SHA1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SHA1.swift; path = Sources/CryptoSwift/SHA1.swift; sourceTree = SOURCE_ROOT; };
 		757BC9361C1CA5790093AAA9 /* SHA2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SHA2.swift; path = Sources/CryptoSwift/SHA2.swift; sourceTree = SOURCE_ROOT; };
 		757BC9371C1CA5790093AAA9 /* String+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "String+Extension.swift"; path = "Sources/CryptoSwift/String+Extension.swift"; sourceTree = SOURCE_ROOT; };
-		757BC9381C1CA5790093AAA9 /* UInt8Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UInt8Extension.swift; path = Sources/CryptoSwift/UInt8Extension.swift; sourceTree = SOURCE_ROOT; };
-		757BC9391C1CA5790093AAA9 /* UInt32Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UInt32Extension.swift; path = Sources/CryptoSwift/UInt32Extension.swift; sourceTree = SOURCE_ROOT; };
-		757BC93A1C1CA5790093AAA9 /* UInt64Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UInt64Extension.swift; path = Sources/CryptoSwift/UInt64Extension.swift; sourceTree = SOURCE_ROOT; };
+		757BC9381C1CA5790093AAA9 /* UInt8+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UInt8+Extension.swift"; path = "Sources/CryptoSwift/UInt8+Extension.swift"; sourceTree = SOURCE_ROOT; };
+		757BC9391C1CA5790093AAA9 /* UInt32+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UInt32+Extension.swift"; path = "Sources/CryptoSwift/UInt32+Extension.swift"; sourceTree = SOURCE_ROOT; };
+		757BC93A1C1CA5790093AAA9 /* UInt64+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UInt64+Extension.swift"; path = "Sources/CryptoSwift/UInt64+Extension.swift"; sourceTree = SOURCE_ROOT; };
 		757BC93B1C1CA5790093AAA9 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Utils.swift; path = Sources/CryptoSwift/Utils.swift; sourceTree = SOURCE_ROOT; };
 		757DA2521A4ED0A4002BA3EF /* PaddingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaddingTests.swift; sourceTree = "<group>"; };
 		757DA2541A4ED408002BA3EF /* AESTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AESTests.swift; sourceTree = "<group>"; };
@@ -399,6 +409,8 @@
 		75865C5E1D4AF3880072CAC2 /* RandomAccessBlockModeWorker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RandomAccessBlockModeWorker.swift; path = Sources/CryptoSwift/BlockMode/RandomAccessBlockModeWorker.swift; sourceTree = SOURCE_ROOT; };
 		758A94271A65C59200E46135 /* HMACTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMACTests.swift; sourceTree = "<group>"; };
 		758F58F01C8FB6E20054C377 /* OFB.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = OFB.swift; path = Sources/CryptoSwift/BlockMode/OFB.swift; sourceTree = SOURCE_ROOT; };
+		75C2E7671D55EFA1003D2BCA /* UInt16+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UInt16+Extension.swift"; path = "Sources/CryptoSwift/UInt16+Extension.swift"; sourceTree = SOURCE_ROOT; };
+		75C2E76C1D55F097003D2BCA /* Access.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Access.swift; sourceTree = "<group>"; };
 		75CB93241C8F5EC10087740D /* CBC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CBC.swift; path = Sources/CryptoSwift/BlockMode/CBC.swift; sourceTree = SOURCE_ROOT; };
 		75CB932F1C8F5F580087740D /* BlockMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BlockMode.swift; path = Sources/CryptoSwift/BlockMode/BlockMode.swift; sourceTree = SOURCE_ROOT; };
 		75CB93341C8F5FCE0087740D /* PCBC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PCBC.swift; path = Sources/CryptoSwift/BlockMode/PCBC.swift; sourceTree = SOURCE_ROOT; };
@@ -499,7 +511,6 @@
 			isa = PBXGroup;
 			children = (
 				75DB81B11CDC0E4100ED181A /* Ciphers */,
-				757BC91B1C1CA5790093AAA9 /* Array+Extension.swift */,
 				757BC91D1C1CA5790093AAA9 /* Authenticator.swift */,
 				80545D121CA9FECD00474A99 /* Bit.swift */,
 				757F440D1CC1822A002B1F85 /* SecureBytes.swift */,
@@ -508,8 +519,7 @@
 				75DB81A71CDC06B100ED181A /* Updatable.swift */,
 				75558FCB1D4BA93C00CF6C18 /* RandomAccessCryptor.swift */,
 				75CB93291C8F5EC60087740D /* BlockMode */,
-				757BC9231C1CA5790093AAA9 /* CRC.swift */,
-				757BC9251C1CA5790093AAA9 /* CSArrayType+Extensions.swift */,
+				757BC9231C1CA5790093AAA9 /* Checksum.swift */,
 				757BC9261C1CA5790093AAA9 /* Generics.swift */,
 				757BC9271C1CA5790093AAA9 /* Hash.swift */,
 				757BC9281C1CA5790093AAA9 /* HashProtocol.swift */,
@@ -526,12 +536,16 @@
 				757BC9331C1CA5790093AAA9 /* Poly1305.swift */,
 				757BC9351C1CA5790093AAA9 /* SHA1.swift */,
 				757BC9361C1CA5790093AAA9 /* SHA2.swift */,
-				757BC9371C1CA5790093AAA9 /* String+Extension.swift */,
-				757BC92C1C1CA5790093AAA9 /* IntExtension.swift */,
-				757BC9381C1CA5790093AAA9 /* UInt8Extension.swift */,
-				757BC9391C1CA5790093AAA9 /* UInt32Extension.swift */,
-				757BC93A1C1CA5790093AAA9 /* UInt64Extension.swift */,
 				757BC93B1C1CA5790093AAA9 /* Utils.swift */,
+				757BC91B1C1CA5790093AAA9 /* Array+Extension.swift */,
+				757BC9251C1CA5790093AAA9 /* CSArrayType+Extensions.swift */,
+				756B575E1D5131A4001C0C72 /* Collection+Extension.swift */,
+				757BC9371C1CA5790093AAA9 /* String+Extension.swift */,
+				757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */,
+				757BC9381C1CA5790093AAA9 /* UInt8+Extension.swift */,
+				75C2E7671D55EFA1003D2BCA /* UInt16+Extension.swift */,
+				757BC9391C1CA5790093AAA9 /* UInt32+Extension.swift */,
+				757BC93A1C1CA5790093AAA9 /* UInt64+Extension.swift */,
 				757BC8F61C1CA56A0093AAA9 /* Foundation */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
 			);
@@ -560,6 +574,7 @@
 				755FB1D9199E347D00475437 /* ExtensionsTest.swift */,
 				757DA2521A4ED0A4002BA3EF /* PaddingTests.swift */,
 				75482EA31CB310B7001F66A5 /* PBKDF.swift */,
+				75C2E76C1D55F097003D2BCA /* Access.swift */,
 				756BFDCA1A82B87300B9D9A4 /* Bridging.h */,
 				754BE46519693E190098E6F3 /* Supporting Files */,
 			);
@@ -858,9 +873,10 @@
 				757F440F1CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9751C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACC1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B57601D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC9071C1CA56A0093AAA9 /* ChaCha20+Foundation.swift in Sources */,
 				75D0E05D1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
-				757BC9851C1CA5790093AAA9 /* IntExtension.swift in Sources */,
+				757BC9851C1CA5790093AAA9 /* Int+Extension.swift in Sources */,
 				757BC9B11C1CA5790093AAA9 /* String+Extension.swift in Sources */,
 				757BC96D1C1CA5790093AAA9 /* Generics.swift in Sources */,
 				757F440A1CC172B6002B1F85 /* BlockCipher.swift in Sources */,
@@ -870,17 +886,17 @@
 				75CB93361C8F5FCE0087740D /* PCBC.swift in Sources */,
 				75CB93311C8F5F580087740D /* BlockMode.swift in Sources */,
 				757BC9171C1CA56A0093AAA9 /* Utils+Foundation.swift in Sources */,
-				757BC9B51C1CA5790093AAA9 /* UInt8Extension.swift in Sources */,
+				757BC9B51C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */,
 				757BC9411C1CA5790093AAA9 /* Array+Extension.swift in Sources */,
 				75482EA81CB329C1001F66A5 /* PBKDF2.swift in Sources */,
 				757BC90F1C1CA56A0093AAA9 /* Rabbit+Foundation.swift in Sources */,
 				757BC9691C1CA5790093AAA9 /* CSArrayType+Extensions.swift in Sources */,
-				757BC9611C1CA5790093AAA9 /* CRC.swift in Sources */,
+				757BC9611C1CA5790093AAA9 /* Checksum.swift in Sources */,
 				757BC9A11C1CA5790093AAA9 /* Poly1305.swift in Sources */,
 				75CB93451C8F603C0087740D /* CTR.swift in Sources */,
 				753881EC1CB06E390089101D /* NoPadding.swift in Sources */,
 				758F58F21C8FB6E20054C377 /* OFB.swift in Sources */,
-				757BC9BD1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */,
+				757BC9BD1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */,
 				75CB934F1C8F609D0087740D /* BlockModeOptions.swift in Sources */,
 				757BC9811C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */,
 				757BC9511C1CA5790093AAA9 /* BytesSequence.swift in Sources */,
@@ -891,7 +907,8 @@
 				755655C81D080E3F00F004E7 /* Cryptors.swift in Sources */,
 				757BC9911C1CA5790093AAA9 /* Operators.swift in Sources */,
 				75CB933B1C8F5FFD0087740D /* CFB.swift in Sources */,
-				757BC9B91C1CA5790093AAA9 /* UInt32Extension.swift in Sources */,
+				75C2E7691D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */,
+				757BC9B91C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */,
 				757BC9131C1CA56A0093AAA9 /* String+FoundationExtension.swift in Sources */,
 				757BC98D1C1CA5790093AAA9 /* Multiplatform.swift in Sources */,
 				757BC93D1C1CA5790093AAA9 /* AES.swift in Sources */,
@@ -922,9 +939,10 @@
 				757F440E1CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9741C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACB1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B575F1D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC90E1C1CA56A0093AAA9 /* Rabbit+Foundation.swift in Sources */,
 				75D0E05C1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
-				757BC9841C1CA5790093AAA9 /* IntExtension.swift in Sources */,
+				757BC9841C1CA5790093AAA9 /* Int+Extension.swift in Sources */,
 				757BC9B01C1CA5790093AAA9 /* String+Extension.swift in Sources */,
 				757BC96C1C1CA5790093AAA9 /* Generics.swift in Sources */,
 				757F44091CC172B6002B1F85 /* BlockCipher.swift in Sources */,
@@ -934,15 +952,15 @@
 				75CB93351C8F5FCE0087740D /* PCBC.swift in Sources */,
 				75CB93301C8F5F580087740D /* BlockMode.swift in Sources */,
 				757BC9121C1CA56A0093AAA9 /* String+FoundationExtension.swift in Sources */,
-				757BC9B41C1CA5790093AAA9 /* UInt8Extension.swift in Sources */,
+				757BC9B41C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */,
 				757BC9401C1CA5790093AAA9 /* Array+Extension.swift in Sources */,
 				757BC9061C1CA56A0093AAA9 /* ChaCha20+Foundation.swift in Sources */,
 				75482EA71CB329C1001F66A5 /* PBKDF2.swift in Sources */,
 				757BC9681C1CA5790093AAA9 /* CSArrayType+Extensions.swift in Sources */,
-				757BC9601C1CA5790093AAA9 /* CRC.swift in Sources */,
+				757BC9601C1CA5790093AAA9 /* Checksum.swift in Sources */,
 				757BC9A01C1CA5790093AAA9 /* Poly1305.swift in Sources */,
 				75CB93441C8F603C0087740D /* CTR.swift in Sources */,
-				757BC9BC1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */,
+				757BC9BC1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */,
 				753881EB1CB06E390089101D /* NoPadding.swift in Sources */,
 				758F58F11C8FB6E20054C377 /* OFB.swift in Sources */,
 				75CB934E1C8F609D0087740D /* BlockModeOptions.swift in Sources */,
@@ -955,7 +973,8 @@
 				755655C71D080E3F00F004E7 /* Cryptors.swift in Sources */,
 				757BC9901C1CA5790093AAA9 /* Operators.swift in Sources */,
 				75CB933A1C8F5FFD0087740D /* CFB.swift in Sources */,
-				757BC9B81C1CA5790093AAA9 /* UInt32Extension.swift in Sources */,
+				75C2E7681D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */,
+				757BC9B81C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */,
 				757BC8FE1C1CA56A0093AAA9 /* AES+Foundation.swift in Sources */,
 				757BC98C1C1CA5790093AAA9 /* Multiplatform.swift in Sources */,
 				757BC93C1C1CA5790093AAA9 /* AES.swift in Sources */,
@@ -973,6 +992,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				75C2E76D1D55F097003D2BCA /* Access.swift in Sources */,
 				75482EA41CB310B7001F66A5 /* PBKDF.swift in Sources */,
 				758A94291A65C67400E46135 /* HMACTests.swift in Sources */,
 				75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */,
@@ -1003,9 +1023,10 @@
 				757F44111CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9761C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACE1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B57621D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC9101C1CA56A0093AAA9 /* Rabbit+Foundation.swift in Sources */,
 				75D0E05F1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
-				757BC9861C1CA5790093AAA9 /* IntExtension.swift in Sources */,
+				757BC9861C1CA5790093AAA9 /* Int+Extension.swift in Sources */,
 				757BC9B21C1CA5790093AAA9 /* String+Extension.swift in Sources */,
 				757BC96E1C1CA5790093AAA9 /* Generics.swift in Sources */,
 				757F440C1CC172B6002B1F85 /* BlockCipher.swift in Sources */,
@@ -1015,17 +1036,17 @@
 				75CB93371C8F5FCE0087740D /* PCBC.swift in Sources */,
 				75CB93321C8F5F580087740D /* BlockMode.swift in Sources */,
 				757BC9141C1CA56A0093AAA9 /* String+FoundationExtension.swift in Sources */,
-				757BC9B61C1CA5790093AAA9 /* UInt8Extension.swift in Sources */,
+				757BC9B61C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */,
 				757BC9421C1CA5790093AAA9 /* Array+Extension.swift in Sources */,
 				75482EAA1CB329C1001F66A5 /* PBKDF2.swift in Sources */,
 				757BC9081C1CA56A0093AAA9 /* ChaCha20+Foundation.swift in Sources */,
 				757BC96A1C1CA5790093AAA9 /* CSArrayType+Extensions.swift in Sources */,
-				757BC9621C1CA5790093AAA9 /* CRC.swift in Sources */,
+				757BC9621C1CA5790093AAA9 /* Checksum.swift in Sources */,
 				757BC9A21C1CA5790093AAA9 /* Poly1305.swift in Sources */,
 				75CB93461C8F603C0087740D /* CTR.swift in Sources */,
 				753881EE1CB06E390089101D /* NoPadding.swift in Sources */,
 				758F58F31C8FB6E20054C377 /* OFB.swift in Sources */,
-				757BC9BE1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */,
+				757BC9BE1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */,
 				75CB93501C8F609D0087740D /* BlockModeOptions.swift in Sources */,
 				757BC9821C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */,
 				757BC9521C1CA5790093AAA9 /* BytesSequence.swift in Sources */,
@@ -1036,7 +1057,8 @@
 				755655CA1D080E3F00F004E7 /* Cryptors.swift in Sources */,
 				757BC9921C1CA5790093AAA9 /* Operators.swift in Sources */,
 				75CB933C1C8F5FFD0087740D /* CFB.swift in Sources */,
-				757BC9BA1C1CA5790093AAA9 /* UInt32Extension.swift in Sources */,
+				75C2E76B1D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */,
+				757BC9BA1C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */,
 				757BC9001C1CA56A0093AAA9 /* AES+Foundation.swift in Sources */,
 				757BC98E1C1CA5790093AAA9 /* Multiplatform.swift in Sources */,
 				757BC93E1C1CA5790093AAA9 /* AES.swift in Sources */,
@@ -1067,9 +1089,10 @@
 				757F44101CC1822B002B1F85 /* SecureBytes.swift in Sources */,
 				757BC9771C1CA5790093AAA9 /* HashProtocol.swift in Sources */,
 				750D3ACD1D0EADCA00999299 /* ZeroPadding.swift in Sources */,
+				756B57611D5131A4001C0C72 /* Collection+Extension.swift in Sources */,
 				757BC9091C1CA56A0093AAA9 /* ChaCha20+Foundation.swift in Sources */,
 				75D0E05E1CFB9B9400FCEA0E /* Cipher.swift in Sources */,
-				757BC9871C1CA5790093AAA9 /* IntExtension.swift in Sources */,
+				757BC9871C1CA5790093AAA9 /* Int+Extension.swift in Sources */,
 				757BC9B31C1CA5790093AAA9 /* String+Extension.swift in Sources */,
 				757BC96F1C1CA5790093AAA9 /* Generics.swift in Sources */,
 				757F440B1CC172B6002B1F85 /* BlockCipher.swift in Sources */,
@@ -1079,17 +1102,17 @@
 				75CB93381C8F5FCE0087740D /* PCBC.swift in Sources */,
 				75CB93331C8F5F580087740D /* BlockMode.swift in Sources */,
 				757BC9191C1CA56A0093AAA9 /* Utils+Foundation.swift in Sources */,
-				757BC9B71C1CA5790093AAA9 /* UInt8Extension.swift in Sources */,
+				757BC9B71C1CA5790093AAA9 /* UInt8+Extension.swift in Sources */,
 				757BC9431C1CA5790093AAA9 /* Array+Extension.swift in Sources */,
 				75482EA91CB329C1001F66A5 /* PBKDF2.swift in Sources */,
 				757BC9111C1CA56A0093AAA9 /* Rabbit+Foundation.swift in Sources */,
 				757BC96B1C1CA5790093AAA9 /* CSArrayType+Extensions.swift in Sources */,
-				757BC9631C1CA5790093AAA9 /* CRC.swift in Sources */,
+				757BC9631C1CA5790093AAA9 /* Checksum.swift in Sources */,
 				757BC9A31C1CA5790093AAA9 /* Poly1305.swift in Sources */,
 				75CB93471C8F603C0087740D /* CTR.swift in Sources */,
 				753881ED1CB06E390089101D /* NoPadding.swift in Sources */,
 				758F58F41C8FB6E20054C377 /* OFB.swift in Sources */,
-				757BC9BF1C1CA5790093AAA9 /* UInt64Extension.swift in Sources */,
+				757BC9BF1C1CA5790093AAA9 /* UInt64+Extension.swift in Sources */,
 				75CB93511C8F609D0087740D /* BlockModeOptions.swift in Sources */,
 				757BC9831C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */,
 				757BC9531C1CA5790093AAA9 /* BytesSequence.swift in Sources */,
@@ -1100,7 +1123,8 @@
 				755655C91D080E3F00F004E7 /* Cryptors.swift in Sources */,
 				757BC9931C1CA5790093AAA9 /* Operators.swift in Sources */,
 				75CB933D1C8F5FFD0087740D /* CFB.swift in Sources */,
-				757BC9BB1C1CA5790093AAA9 /* UInt32Extension.swift in Sources */,
+				75C2E76A1D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */,
+				757BC9BB1C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */,
 				757BC9151C1CA56A0093AAA9 /* String+FoundationExtension.swift in Sources */,
 				757BC98F1C1CA5790093AAA9 /* Multiplatform.swift in Sources */,
 				757BC93F1C1CA5790093AAA9 /* AES.swift in Sources */,

+ 16 - 0
CryptoSwiftTests/AESTests.swift

@@ -277,6 +277,22 @@ final class AESTests: XCTestCase {
         XCTAssertEqual(decrypted, plaintext, "decryption failed")
     }
 
+    // https://github.com/krzyzanowskim/CryptoSwift/issues/298
+    func test_issue298() {
+        let encryptedValue = "47595cfa90f7b0b0e0d9d7240a2e035f7f4acde27d7ca778a7d8b05add32a0a92d945c0a59f7f0e029d7f2fbb258b2f0"
+        let key = "0123456789abcdef"
+        let iv = "fedcba9876543210"
+
+        do {
+            let aes = try AES(key: key, iv: iv, blockMode: .CBC, padding: NoPadding())
+            let ciphertext = try aes.decrypt(Array<UInt8>(hex: encryptedValue))
+            let str = String(data: Data(ciphertext), encoding: String.Encoding.utf8)
+            XCTAssertEqual(str!, "74b653e36430d7a9cd91a24d9944b032OrFeckrWo\0\0\0\0\0\0\0")
+        } catch {
+            XCTFail("failed")
+        }
+    }
+
 //    func testAES_encrypt_performance() {
 //        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
 //        let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]

+ 217 - 0
CryptoSwiftTests/Access.swift

@@ -0,0 +1,217 @@
+//
+//  Access.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 06/08/16.
+//  Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
+//
+
+import XCTest
+import CryptoSwift
+
+class Access: XCTestCase {
+    let cipher = try! AES(key: "secret0key000000", iv: "0123456789012345")
+    let authenticator = HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1)
+
+    func testChecksum() {
+        let _ = Checksum.crc32([1,2,3])
+        let _ = Checksum.crc16([1,2,3])
+    }
+
+    func testHash() {
+        let _ = Hash.md5([1,2,3])
+        let _ = Hash.sha1([1,2,3])
+        let _ = Hash.sha224([1,2,3])
+        let _ = Hash.sha256([1,2,3])
+        let _ = Hash.sha384([1,2,3])
+        let _ = Hash.sha512([1,2,3])
+    }
+
+    func testArrayExtension() {
+        let array = Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3")
+        let _ = array.toHexString()
+
+        let _ = array.md5()
+        let _ = array.sha1()
+        let _ = array.sha256()
+        let _ = array.sha384()
+        let _ = array.sha512()
+        let _ = array.crc32()
+        let _ = array.crc16()
+
+        do {
+            let _ = try array.encrypt(cipher: cipher)
+            let _ = try array.decrypt(cipher: cipher)
+            let _ = try array.authenticate(with: authenticator)
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testCollectionExtension() {
+        // nothing public
+    }
+
+    func testStringExtension() {
+        let string = "foofoobarbar"
+        let _ = string.md5()
+        let _ = string.sha1()
+        let _ = string.sha224()
+        let _ = string.sha256()
+        let _ = string.sha384()
+        let _ = string.sha512()
+        let _ = string.crc16()
+        let _ = string.crc32()
+
+        do {
+            let _ = try string.encrypt(cipher: cipher)
+            let _ = try string.authenticate(with: authenticator)
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testStringFoundationExtension() {
+        let string = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
+        do {
+            let _ = try string.decryptBase64ToString(cipher: cipher)
+            let _ = try string.decryptBase64(cipher: cipher)
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testIntExtension() {
+        // nothing public
+    }
+
+    func testUInt16Extension() {
+        // nothing public
+    }
+
+    func testUInt32Extension() {
+        // nothing public
+    }
+
+    func testUInt64Extension() {
+        // nothing public
+    }
+
+    func testUInt8Extension() {
+        // nothing public
+    }
+
+    func testDataExtension() {
+        let data = Data(bytes: [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8])
+        let _ = data.checksum()
+        let _ = data.md5()
+        let _ = data.sha1()
+        let _ = data.sha224()
+        let _ = data.sha256()
+        let _ = data.sha384()
+        let _ = data.sha512()
+        let _ = data.crc16()
+        let _ = data.crc32()
+
+        let _ = data.bytes
+        let _ = data.toHexString()
+
+        do {
+            let _ = try data.encrypt(cipher: cipher)
+            let _ = try data.decrypt(cipher: cipher)
+            let _ = try data.authenticate(with: authenticator)
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testPadding() {
+        // PKCS7
+        let _ = PKCS7().add(to: [1,2,3], blockSize: 16)
+        let _ = PKCS7().remove(from: [1,2,3], blockSize: 16)
+
+        // NoPadding
+        let _ = NoPadding().add(to: [1,2,3], blockSize: 16)
+        let _ = NoPadding().remove(from: [1,2,3], blockSize: 16)
+
+        // ZeroPadding
+        let _ = ZeroPadding().add(to: [1,2,3], blockSize: 16)
+        let _ = ZeroPadding().remove(from: [1,2,3], blockSize: 16)
+    }
+
+    func testPBKDF() {
+        do {
+            let _ = PKCS5.PBKDF1.Variant.md5
+            let _ = try PKCS5.PBKDF1(password: [1,2,3,4,5,6,7], salt: [1,2,3,4,5,6,7,8]).calculate()
+            let _ = try PKCS5.PBKDF2(password: [1,2,3,4,5,6,7], salt: [1,2,3,4]).calculate()
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testAuthenticators() {
+        do {
+            let _ = try HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1).authenticate([1,2,3])
+            let _ = try Poly1305(key: [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2]).authenticate([1,2,3])
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testAES() {
+        do {
+            let aes = try AES(key: "secret0key000000", iv: "0123456789012345")
+            let _ = aes.makeEncryptor()
+            let _ = aes.makeDecryptor()
+            let enc = try aes.encrypt([1,2,3])
+            let _ = try aes.decrypt(enc)
+
+            let _ = try AES(key: [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6], iv: [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6], blockMode: .CBC, padding: NoPadding())
+
+            let _ = AES.Variant.aes128
+            let _ = AES.blockSize
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testRabbit() {
+        do {
+            let rabbit = try Rabbit(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
+            let enc = rabbit.encrypt([1,2,3])
+            let _   = rabbit.decrypt(enc)
+
+            XCTAssertThrowsError(try Rabbit(key: "123"))
+
+            let _ = Rabbit.blockSize
+            let _ = Rabbit.keySize
+            let _ = Rabbit.ivSize
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testChaCha20() {
+        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
+        let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
+
+        do {
+            let _ = ChaCha20.blockSize
+            let chacha20 = try ChaCha20(key: key, iv: iv)
+            let enc = try chacha20.encrypt([1,3,4])
+            let _ = try chacha20.decrypt(enc)
+
+            XCTAssertThrowsError(try ChaCha20(key: "123", iv: "12345678"))
+
+            let _ = chacha20.makeEncryptor()
+            let _ = chacha20.makeDecryptor()
+
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
+    }
+
+    func testUpdatable() {
+        // TODO
+    }
+}

+ 23 - 9
CryptoSwiftTests/ExtensionsTest.swift

@@ -31,19 +31,19 @@ final class ExtensionsTest: XCTestCase {
     func testIntExtension() {
         let i1:Int = 1024
         let i1Array = i1.bytes(totalBytes: 32 / 8) // 32 bit
-        let i1recovered = Int.with(i1Array)
+        let i1recovered = Int(bytes: i1Array)
         
         XCTAssertEqual(i1, i1recovered, "Bytes conversion failed")
         
         let i2:Int = 1024
         let i2Array = i2.bytes(totalBytes: 160 / 8) // 160 bit
-        let i2recovered = Int.with(i2Array)
+        let i2recovered = Int(bytes: i2Array)
         
         XCTAssertEqual(i2, i2recovered, "Bytes conversion failed")
     }
     
     func testBytes() {
-        let size = sizeof(UInt32.self) // 32 or 64  bit
+        let size = MemoryLayout<UInt32>.size // 32 or 64  bit
         
         let i:UInt32 = 1024
         var bytes = i.bytes()
@@ -67,8 +67,8 @@ final class ExtensionsTest: XCTestCase {
         let ii:Int = 21
         XCTAssert(ii &<< 1 == ii << 1, "shift left failed")
         XCTAssert(ii &<< 8 == ii << 8, "shift left failed")
-        XCTAssert(ii &<< ((sizeofValue(ii) * 8) - 1) == ii << ((sizeofValue(ii) * 8) - 1), "shift left failed")
-        XCTAssert(ii &<< ((sizeofValue(ii) * 8)) == 0, "shift left failed")
+        XCTAssert(ii &<< ((MemoryLayout<Int>.size * 8) - 1) == ii << ((MemoryLayout<Int>.size * 8) - 1), "shift left failed")
+        XCTAssert(ii &<< ((MemoryLayout<Int>.size * 8)) == 0, "shift left failed")
         
         let iii:UInt32 = 21
         XCTAssert(iii &<< 1 == iii << 1, "shift left failed")
@@ -78,7 +78,7 @@ final class ExtensionsTest: XCTestCase {
     
     func testtoUInt32Array() {
         let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
-        let result = sliceToUInt32Array(chunk)
+        let result = chunk.toUInt32Array()
         
         XCTAssert(result.count == 2, "Invalid conversion")
         XCTAssert(result[0] == 117506305, "Invalid conversion")
@@ -90,9 +90,13 @@ final class ExtensionsTest: XCTestCase {
         XCTAssert(data.count == 3, "Invalid data")
     }
 
-    func test_String_encrypt_base64() {
-        let encryptedBase64 = try! "my secret string".encrypt(cipher: AES(key: "secret0key000000", iv: "0123456789012345")).toBase64()
-        XCTAssertEqual(encryptedBase64, "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs=")
+    func test_String_encrypt() {
+        do {
+            let encryptedHex = try "my secret string".encrypt(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
+            XCTAssertEqual(encryptedHex, "68f7ff8bdb61f625febdfe3d791ecf624daaed2e719a6de39112de8e0cc7349b")
+        } catch {
+            XCTFail(error.localizedDescription)
+        }
     }
 
     func test_String_decrypt_base64() {
@@ -101,4 +105,14 @@ final class ExtensionsTest: XCTestCase {
         XCTAssertEqual(decrypted, "my secret string")
     }
 
+    func test_Array_init_hex() {
+        let bytes = Array<UInt8>(hex: "0xb1b1b2b2")
+        XCTAssertEqual(bytes, [177,177,178,178])
+
+        let str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
+        let array = Array<UInt8>(hex: str)
+        let hex = array.toHexString()
+        XCTAssertEqual(str, hex)
+    }
+
 }

+ 5 - 5
CryptoSwiftTests/HMACTests.swift

@@ -24,7 +24,7 @@ final class HMACTests: XCTestCase {
         let msg:Array<UInt8> = []
         let expectedMac:Array<UInt8> = [0x74,0xe6,0xf7,0x29,0x8a,0x9c,0x2d,0x16,0x89,0x35,0xf5,0x8c,0x00,0x1b,0xad,0x88]
         
-        let hmac = try! Authenticator.HMAC(key: key, variant: .md5).authenticate(msg)
+        let hmac = try! HMAC(key: key, variant: .md5).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
     
@@ -33,7 +33,7 @@ final class HMACTests: XCTestCase {
         let msg:Array<UInt8> = []
         let expectedMac:Array<UInt8> = [0xfb,0xdb,0x1d,0x1b,0x18,0xaa,0x6c,0x08,0x32,0x4b,0x7d,0x64,0xb7,0x1f,0xb7,0x63,0x70,0x69,0x0e,0x1d]
         
-        let hmac = try! Authenticator.HMAC(key: key, variant: .sha1).authenticate(msg)
+        let hmac = try! HMAC(key: key, variant: .sha1).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 
@@ -42,7 +42,7 @@ final class HMACTests: XCTestCase {
         let msg:Array<UInt8> = []
         let expectedMac:Array<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 = try! Authenticator.HMAC(key: key, variant: .sha256).authenticate(msg)
+        let hmac = try! HMAC(key: key, variant: .sha256).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 
@@ -51,7 +51,7 @@ final class HMACTests: XCTestCase {
         let msg:Array<UInt8> = []
         let expectedMac:Array<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 = try! Authenticator.HMAC(key: key, variant: .sha384).authenticate(msg)
+        let hmac = try! HMAC(key: key, variant: .sha384).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 
@@ -60,7 +60,7 @@ final class HMACTests: XCTestCase {
         let msg:Array<UInt8> = []
         let expectedMac:Array<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 = try! Authenticator.HMAC(key: key, variant: .sha512).authenticate(msg)
+        let hmac = try! HMAC(key: key, variant: .sha512).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
 }

+ 7 - 8
CryptoSwiftTests/HashTests.swift

@@ -21,12 +21,12 @@ final class CryptoSwiftTests: XCTestCase {
     
     func testMD5_data() {
         let data = [0x31, 0x32, 0x33] as Array<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");
+        XCTAssertEqual(Hash.md5(data), [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
     }
 
     func testMD5_emptyString() {
         let data:Data = "".data(using: String.Encoding.utf8, allowLossyConversion: false)!
-        XCTAssertEqual(Hash.md5(data.bytes).calculate(), [0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e], "MD5 calculation failed")
+        XCTAssertEqual(Hash.md5(data.bytes), [0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e], "MD5 calculation failed")
     }
 
     func testMD5_string() {
@@ -44,23 +44,22 @@ final class CryptoSwiftTests: XCTestCase {
         self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
             let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
             self.startMeasuring()
-            _ = Hash.md5(arr).calculate()
+            _ = Hash.md5(arr)
             self.stopMeasuring()
         })
     }
     
     func testMD5PerformanceCommonCrypto() {
         self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
-            let size = 1024 * 1024
-            let buf = UnsafeMutablePointer<NSData>.allocate(capacity: size)
-            let data = NSData(bytes: buf, length: size)
+            let buf: UnsafeMutableRawPointer = calloc(1024 * 1024, MemoryLayout<UInt8>.size)
+            let data = NSData(bytes: buf, length: 1024 * 1024)
             let md = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(CC_MD5_DIGEST_LENGTH))
             self.startMeasuring()
             CC_MD5(data.bytes, CC_LONG(data.length), md)
             self.stopMeasuring()
             md.deallocate(capacity: Int(CC_MD5_DIGEST_LENGTH))
             md.deinitialize()
-            buf.deallocate(capacity: size)
+            buf.deallocate(bytes: 1024 * 1024, alignedTo: MemoryLayout<UInt8>.alignment)
         })
     }
     
@@ -132,7 +131,7 @@ final class CryptoSwiftTests: XCTestCase {
     }
     
     func testCRC16() {
-        let result = CRC().crc16([49,50,51,52,53,54,55,56,57] as Array<UInt8>)
+        let result = Checksum.crc16([49,50,51,52,53,54,55,56,57] as Array<UInt8>)
         XCTAssert(result == 0xBB3D, "CRC16 failed")
     }
     

+ 2 - 2
CryptoSwiftTests/Poly1305Tests.swift

@@ -24,12 +24,12 @@ final class Poly1305Tests: XCTestCase {
         let msg:Array<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:Array<UInt8> = [0xdd,0xb9,0xda,0x7d,0xdd,0x5e,0x52,0x79,0x27,0x30,0xed,0x5c,0xda,0x5f,0x90,0xa4]
         
-        let mac = try! Authenticator.Poly1305(key: key).authenticate(msg)
+        let mac = try! Poly1305(key: key).authenticate(msg)
         XCTAssertEqual(mac, expectedMac, "Invalid authentication result")
         
         // extensions
         let msgData = Data(bytes: msg)
-        let mac2 = try! msgData.authenticate(with: Authenticator.Poly1305(key: key))
+        let mac2 = try! msgData.authenticate(with: Poly1305(key: key))
         XCTAssertEqual(mac2, Data(bytes: expectedMac), "Invalid authentication result")
     }
 }

+ 11 - 11
CryptoSwiftTests/RabbitTests.swift

@@ -18,23 +18,23 @@ class RabbitTests: XCTestCase {
     override func tearDown() {
         super.tearDown()
     }
-    
+
     func testInitialization() {
         var key = Array<UInt8>(repeating: 0, count: Rabbit.keySize - 1)
         var iv: Array<UInt8>?
-        XCTAssertNil(Rabbit(key: key, iv: iv))
-        
+        XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
+
         key = Array<UInt8>(repeating: 0, count: Rabbit.keySize + 1)
-        XCTAssertNil(Rabbit(key: key, iv: iv))
+        XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
         
         key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
-        XCTAssertNotNil(Rabbit(key: key, iv: iv))
+        XCTAssertNotNil(try Rabbit(key: key, iv: iv))
         
         iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize - 1)
-        XCTAssertNil(Rabbit(key: key, iv: iv))
+        XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
         
         iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
-        XCTAssertNotNil(Rabbit(key: key, iv: iv))
+        XCTAssertNotNil(try Rabbit(key: key, iv: iv))
     }
     
     func testRabbitWithoutIV() {
@@ -71,7 +71,7 @@ class RabbitTests: XCTestCase {
         
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         for (key, expectedCipher) in cases {
-            let rabbit = Rabbit(key: key)!
+            let rabbit = try! Rabbit(key: key)
             let cipherText = rabbit.encrypt(plainText)
             XCTAssertEqual(cipherText, expectedCipher)
             XCTAssertEqual(rabbit.decrypt(cipherText), plainText)
@@ -110,7 +110,7 @@ class RabbitTests: XCTestCase {
         
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         for (iv, expectedCipher) in cases {
-            let rabbit = Rabbit(key: key, iv: iv)!
+            let rabbit = try! Rabbit(key: key, iv: iv)
             let cipherText = rabbit.encrypt(plainText)
             XCTAssertEqual(cipherText, expectedCipher)
             XCTAssertEqual(rabbit.decrypt(cipherText), plainText)
@@ -122,9 +122,9 @@ class RabbitTests: XCTestCase {
         let iv: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
         let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
         measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
-            let encrypted = Rabbit(key: key, iv: iv)?.encrypt(message)
+            let encrypted = try! Rabbit(key: key, iv: iv).encrypt(message)
             self.stopMeasuring()
-            XCTAssert(encrypted != nil, "not encrypted")
+            XCTAssert(!encrypted.isEmpty, "not encrypted")
         })
     }
 }

+ 3 - 3
README.md

@@ -178,11 +178,11 @@ let bytes = "string".utf8.map({$0})
 
 Hashing a data or array of bytes (aka `Array<UInt8>`)
 ```swift
-/* Hash enum usage */
+/* Hash struct usage */
 let input:Array<UInt8> = [49, 50, 51]
 
 let output = input.md5()
-// alternatively: let output = CryptoSwift.Hash.md5(input).calculate()
+// alternatively: let output = CryptoSwift.Hash.md5(input)
 
 print(output.toHexString())
 ```
@@ -221,7 +221,7 @@ let hmac: Array<UInt8> = try! Authenticator.HMAC(key: key, variant: .sha256).aut
 
 ```swift
 let password: Array<UInt8> = "s33krit".utf8.map {$0}
-let salt: Array<UInt8> = "nacl".utf8.map {$0}
+let salt: Array<UInt8> = "nacllcan".utf8.map {$0}
 
 let value = try! PKCS5.PBKDF1(password: password, salt: salt, iterations: 4096, variant: .sha1).calculate()
 

+ 6 - 6
Sources/CryptoSwift/AES.swift

@@ -114,7 +114,7 @@ fileprivate extension AES {
     func encryptBlock(_ block:Array<UInt8>) -> Array<UInt8>? {
         let rounds = self.variant.Nr
         let rk = self.expandedKey
-        var b = sliceToUInt32Array(block[block.indices])
+        var b = block[block.indices].toUInt32Array()
 
         var t = Array<UInt32>(repeating: 0, count: 4)
 
@@ -178,7 +178,7 @@ fileprivate extension AES {
     func decryptBlock(_ block:Array<UInt8>) -> Array<UInt8>? {
         let rounds = self.variant.Nr
         let rk = expandedKeyInv
-        var b = sliceToUInt32Array(block[block.indices])
+        var b = block[block.indices].toUInt32Array()
 
         var t = Array<UInt32>(repeating: 0, count: 4)
 
@@ -286,7 +286,7 @@ fileprivate extension AES {
             var arr = Array<UInt32>()
             for idx in stride(from: expanded.startIndex, to: expanded.endIndex, by: 4) {
                 let four = Array(expanded[idx..<idx.advanced(by: 4)].reversed())
-                let num = UInt32.with(bytes: four)
+                let num = UInt32(bytes: four)
                 arr.append(num)
             }
 
@@ -326,7 +326,7 @@ fileprivate extension AES {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
             }
             if ((i % variant.Nk) == 0) {
-                tmp = subWord(rotateLeft(UInt32.with(bytes: tmp), by: 8).bytes(totalBytes: sizeof(UInt32.self)))
+                tmp = subWord(rotateLeft(UInt32(bytes: tmp), by: 8).bytes(totalBytes: MemoryLayout<UInt32>.size))
                 tmp[0] = tmp.first! ^ Rcon[i/variant.Nk]
             } else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
                 tmp = subWord(tmp)
@@ -403,7 +403,7 @@ extension AES {
             self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired)
         }
 
-        mutating public func update<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> {
+        mutating public func update<T: Sequence>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
             self.accumulated += bytes
 
             if isLast {
@@ -448,7 +448,7 @@ extension AES {
             self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired);
         }
 
-        mutating public func update<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> {
+        mutating public func update<T: Sequence>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
             // prepend "offset" number of bytes at the begining
             if self.offset > 0 {
                 self.accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes

+ 15 - 0
Sources/CryptoSwift/Array+Extension.swift

@@ -23,3 +23,18 @@ extension Array {
     }
 }
 
+extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
+    public init(hex: String) {
+        self.init()
+        
+        let utf8 = Array<Element.IntegerLiteralType>(hex.utf8)
+        let skip0x = hex.hasPrefix("0x") ? 2 : 0
+        for idx in stride(from: utf8.startIndex.advanced(by: skip0x), to: utf8.endIndex, by: utf8.startIndex.advanced(by: 2)) {
+            let byteHex = "\(UnicodeScalar(utf8[idx]))\(UnicodeScalar(utf8[idx.advanced(by: 1)]))"
+            if let byte = UInt8(byteHex, radix: 16) {
+                self.append(byte as! Element)
+            }
+        }
+    }
+}
+

+ 3 - 36
Sources/CryptoSwift/Authenticator.swift

@@ -6,40 +6,7 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-/**
-*  Message Authentication
-*/
-public enum Authenticator {
-    
-    public enum Error: Swift.Error {
-        case authenticateError
-    }
-    
-    /**
-    Poly1305
-    
-    - parameter key: 256-bit key
-    */
-    case Poly1305(key: Array<UInt8>)
-    case HMAC(key: Array<UInt8>, variant:CryptoSwift.HMAC.Variant)
-    
-    /**
-    Generates an authenticator for message using a one-time key and returns the 16-byte result
-    
-    - returns: 16-byte message authentication code
-    */
-    public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
-        switch (self) {
-        case .Poly1305(let key):
-            guard let auth = CryptoSwift.Poly1305(key: key)?.authenticate(bytes) else {
-                throw Error.authenticateError
-            }
-            return auth
-        case .HMAC(let key, let variant):
-            guard let auth = CryptoSwift.HMAC(key: key, variant: variant)?.authenticate(bytes) else {
-                throw Error.authenticateError
-            }
-            return auth
-        }
-    }
+public protocol Authenticator {
+    /// Generates an authenticator for message using a one-time key and returns the 16-byte result
+    func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8>
 }

+ 1 - 1
Sources/CryptoSwift/BlockMode/BlockMode.swift

@@ -6,7 +6,7 @@
 //  Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
 //
 
-typealias CipherOperationOnBlock = (block: Array<UInt8>) -> Array<UInt8>?
+typealias CipherOperationOnBlock = (_ block: Array<UInt8>) -> Array<UInt8>?
 
 public enum BlockMode {
     case ECB, CBC, PCBC, CFB, OFB, CTR

+ 2 - 2
Sources/CryptoSwift/BlockMode/CBC.swift

@@ -21,7 +21,7 @@ struct CBCModeWorker: BlockModeWorker {
     }
 
     mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
-        guard let ciphertext = cipherOperation(block: xor(prev ?? iv, plaintext)) else {
+        guard let ciphertext = cipherOperation(xor(prev ?? iv, plaintext)) else {
             return plaintext
         }
         prev = ciphertext
@@ -29,7 +29,7 @@ struct CBCModeWorker: BlockModeWorker {
     }
 
     mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
-        guard let plaintext = cipherOperation(block: ciphertext) else {
+        guard let plaintext = cipherOperation(ciphertext) else {
             return ciphertext
         }
         let result = xor(prev ?? iv, plaintext)

+ 2 - 2
Sources/CryptoSwift/BlockMode/CFB.swift

@@ -21,7 +21,7 @@ struct CFBModeWorker: BlockModeWorker {
     }
 
     mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
-        guard let ciphertext = cipherOperation(block: prev ?? iv) else {
+        guard let ciphertext = cipherOperation(prev ?? iv) else {
             return plaintext
         }
         prev = xor(plaintext, ciphertext)
@@ -29,7 +29,7 @@ struct CFBModeWorker: BlockModeWorker {
     }
 
     mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
-        guard let plaintext = cipherOperation(block: prev ?? iv) else {
+        guard let plaintext = cipherOperation(prev ?? iv) else {
             return ciphertext
         }
         let result = xor(plaintext, ciphertext)

+ 2 - 2
Sources/CryptoSwift/BlockMode/CTR.swift

@@ -24,7 +24,7 @@ struct CTRModeWorker: RandomAccessBlockModeWorker {
         let nonce = buildNonce(iv, counter: UInt64(counter))
         counter = counter + 1
 
-        guard let ciphertext = cipherOperation(block: nonce) else {
+        guard let ciphertext = cipherOperation(nonce) else {
             return plaintext
         }
 
@@ -40,6 +40,6 @@ private func buildNonce(_ iv: Array<UInt8>, counter: UInt64) -> Array<UInt8> {
     let noncePartLen = AES.blockSize / 2
     let noncePrefix = Array(iv[0..<noncePartLen])
     let nonceSuffix = Array(iv[noncePartLen..<iv.count])
-    let c = UInt64.with(bytes: nonceSuffix) + counter
+    let c = UInt64(bytes: nonceSuffix) + counter
     return noncePrefix + arrayOfBytes(value: c)
 }

+ 1 - 1
Sources/CryptoSwift/BlockMode/ECB.swift

@@ -17,7 +17,7 @@ struct ECBModeWorker: BlockModeWorker {
     }
 
     mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
-        guard let ciphertext = cipherOperation(block: plaintext) else {
+        guard let ciphertext = cipherOperation(plaintext) else {
             return plaintext
         }
         return ciphertext

+ 2 - 2
Sources/CryptoSwift/BlockMode/OFB.swift

@@ -21,7 +21,7 @@ struct OFBModeWorker: BlockModeWorker {
     }
 
     mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
-        guard let ciphertext = cipherOperation(block: prev ?? iv) else {
+        guard let ciphertext = cipherOperation(prev ?? iv) else {
             return plaintext
         }
         prev = ciphertext
@@ -29,7 +29,7 @@ struct OFBModeWorker: BlockModeWorker {
     }
 
     mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
-        guard let decrypted = cipherOperation(block: prev ?? iv) else {
+        guard let decrypted = cipherOperation(prev ?? iv) else {
             return ciphertext
         }
         let plaintext = xor(decrypted, ciphertext)

+ 2 - 2
Sources/CryptoSwift/BlockMode/PCBC.swift

@@ -21,7 +21,7 @@ struct PCBCModeWorker: BlockModeWorker {
     }
 
     mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
-        guard let ciphertext = cipherOperation(block: xor(prev ?? iv, plaintext)) else {
+        guard let ciphertext = cipherOperation(xor(prev ?? iv, plaintext)) else {
             return plaintext
         }
         prev = xor(plaintext, ciphertext)
@@ -29,7 +29,7 @@ struct PCBCModeWorker: BlockModeWorker {
     }
 
     mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
-        guard let plaintext = cipherOperation(block: ciphertext) else {
+        guard let plaintext = cipherOperation(ciphertext) else {
             return ciphertext
         }
         let result = xor(prev ?? iv, plaintext)

+ 11 - 11
Sources/CryptoSwift/CSArrayType+Extensions.swift

@@ -24,35 +24,35 @@ public extension CSArrayType where Iterator.Element == UInt8 {
 
 public extension CSArrayType where Iterator.Element == UInt8 {
     public func md5() -> [Iterator.Element] {
-        return Hash.md5(cs_arrayValue()).calculate()
+        return Hash.md5(cs_arrayValue())
     }
     
     public func sha1() -> [Iterator.Element] {
-        return Hash.sha1(cs_arrayValue()).calculate()
+        return Hash.sha1(cs_arrayValue())
     }
     
     public func sha224() -> [Iterator.Element] {
-        return Hash.sha224(cs_arrayValue()).calculate()
+        return Hash.sha224(cs_arrayValue())
     }
     
     public func sha256() -> [Iterator.Element] {
-        return Hash.sha256(cs_arrayValue()).calculate()
+        return Hash.sha256(cs_arrayValue())
     }
     
     public func sha384() -> [Iterator.Element] {
-        return Hash.sha384(cs_arrayValue()).calculate()
+        return Hash.sha384(cs_arrayValue())
     }
     
     public func sha512() -> [Iterator.Element] {
-        return Hash.sha512(cs_arrayValue()).calculate()
+        return Hash.sha512(cs_arrayValue())
     }
     
-    public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> [Iterator.Element] {
-        return Hash.crc32(cs_arrayValue(), seed: seed, reflect: reflect).calculate()
+    public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> UInt32 {
+        return Checksum.crc32(cs_arrayValue(), seed: seed, reflect: reflect)
     }
     
-    public func crc16(seed: UInt16? = nil) -> [Iterator.Element] {
-        return Hash.crc16(cs_arrayValue(), seed: seed).calculate()
+    public func crc16(seed: UInt16? = nil) -> UInt16 {
+        return Checksum.crc16(cs_arrayValue(), seed: seed)
     }
     
     public func encrypt(cipher: Cipher) throws -> [Iterator.Element] {
@@ -63,7 +63,7 @@ public extension CSArrayType where Iterator.Element == UInt8 {
         return try cipher.decrypt(cs_arrayValue())
     }
     
-    public func authenticate(with authenticator: Authenticator) throws -> [Iterator.Element] {
+    public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Iterator.Element] {
         return try authenticator.authenticate(cs_arrayValue())
     }
 }

+ 8 - 5
Sources/CryptoSwift/ChaCha20.swift

@@ -19,6 +19,8 @@ final public class ChaCha20: BlockCipher {
         var input = Array<UInt32>(repeating: 0, count: 16)
 
         init(key:Array<UInt8>, iv:Array<UInt8>) throws {
+            precondition(iv.count >= 8)
+
             let kbits = key.count * 8
 
             if (kbits != 128 && kbits != 256) {
@@ -69,7 +71,8 @@ final public class ChaCha20: BlockCipher {
     }
     
     private final func wordToByte(_ input:Array<UInt32> /* 64 */) -> Array<UInt8>? /* 16 */ {
-        assert(input.count == 16)
+        precondition(input.count == 16)
+
         var x = input
 
         for _ in 0..<10 {
@@ -150,7 +153,7 @@ extension ChaCha20 {
             self.chacha = chacha
         }
 
-        mutating public func update<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> {
+        mutating public func update<T: Sequence>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
             self.accumulated += bytes
 
             var encrypted = Array<UInt8>()
@@ -179,7 +182,7 @@ extension ChaCha20 {
             self.chacha = chacha
         }
 
-        mutating public func update<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, isLast: Bool = true) throws -> Array<UInt8> {
+        mutating public func update<T: Sequence>(withBytes bytes:T, isLast: Bool = true) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
             // prepend "offset" number of bytes at the begining
             if self.offset > 0 {
                 self.accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes
@@ -237,10 +240,10 @@ extension ChaCha20: Cipher {
 // MARK: Helpers
 
 /// Change array to number. It's here because arrayOfBytes is too slow
-private func wordNumber(_ bytes:ArraySlice<UInt8>) -> UInt32 {
+private func wordNumber<T: Collection>(_ bytes: T) -> UInt32 where T.Iterator.Element == UInt8, T.IndexDistance == Int {
     var value:UInt32 = 0
     for i:UInt32 in 0..<4 {
-        let j = bytes.startIndex + Int(i)
+        let j = bytes.index(bytes.startIndex, offsetBy: Int(i))
         value = value | UInt32(bytes[j]) << (8 * i)
     }
 

+ 15 - 5
Sources/CryptoSwift/CRC.swift → Sources/CryptoSwift/Checksum.swift

@@ -1,13 +1,12 @@
 //
-//  CRC.swift
+//  Checksum.swift
 //  CryptoSwift
 //
 //  Created by Marcin Krzyzanowski on 25/08/14.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-final class CRC {
-    
+public final class Checksum {
     private static let table32:Array<UInt32> = [0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
         0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
         0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
@@ -79,7 +78,7 @@ final class CRC {
         for chunk in BytesSequence(chunkSize: 256, data: message) {
             for b in chunk {
                 let idx = Int((crc ^ UInt32(reflect ? b : reversed(b))) & 0xff)
-                crc = (crc >> 8) ^ CRC.table32[idx]
+                crc = (crc >> 8) ^ Checksum.table32[idx]
             }
         }
         return (reflect ? crc : reversed(crc)) ^ 0xffffffff
@@ -89,9 +88,20 @@ final class CRC {
         var crc:UInt16 = seed != nil ? seed! : 0x0000
         for chunk in BytesSequence(chunkSize: 256, data: message) {
             for b in chunk {
-                crc = (crc >> 8) ^ CRC.table16[Int((crc ^ UInt16(b)) & 0xFF)]
+                crc = (crc >> 8) ^ Checksum.table16[Int((crc ^ UInt16(b)) & 0xFF)]
             }
         }
         return crc
     }
 }
+
+//MARK: Public interface
+public extension Checksum {
+    static func crc32(_ message:Array<UInt8>, seed: UInt32? = nil, reflect : Bool = true) -> UInt32 {
+        return Checksum().crc32(message, seed: seed, reflect: reflect)
+    }
+
+    static func crc16(_ message:Array<UInt8>, seed: UInt16? = nil) -> UInt16 {
+        return Checksum().crc16(message, seed: seed)
+    }
+}

+ 76 - 0
Sources/CryptoSwift/Collection+Extension.swift

@@ -0,0 +1,76 @@
+//
+//  Collection+Extension.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 02/08/16.
+//  Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
+//
+
+extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
+    func toUInt32Array() -> Array<UInt32> {
+        var result = Array<UInt32>()
+        result.reserveCapacity(16)
+        for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt32>.size) {
+            var val: UInt32 = 0
+            val |= self.count > 3 ? UInt32(self[idx.advanced(by: 3)]) << 24 : 0
+            val |= self.count > 2 ? UInt32(self[idx.advanced(by: 2)]) << 16 : 0
+            val |= self.count > 1 ? UInt32(self[idx.advanced(by: 1)]) << 8  : 0
+            val |= self.count > 0 ? UInt32(self[idx]) : 0
+            result.append(val)
+        }
+
+        for _ in result.count..<MemoryLayout<UInt32>.size {
+            result.append(0)
+        }
+
+        return result
+    }
+
+    func toUInt64Array() -> Array<UInt64> {
+        var result = Array<UInt64>()
+        result.reserveCapacity(32)
+        for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt64>.size) {
+            var val:UInt64 = 0
+            val |= self.count > 7 ? UInt64(self[idx.advanced(by: 7)]) << 56 : 0
+            val |= self.count > 6 ? UInt64(self[idx.advanced(by: 6)]) << 48 : 0
+            val |= self.count > 5 ? UInt64(self[idx.advanced(by: 5)]) << 40 : 0
+            val |= self.count > 4 ? UInt64(self[idx.advanced(by: 4)]) << 32 : 0
+            val |= self.count > 3 ? UInt64(self[idx.advanced(by: 3)]) << 24 : 0
+            val |= self.count > 2 ? UInt64(self[idx.advanced(by: 2)]) << 16 : 0
+            val |= self.count > 1 ? UInt64(self[idx.advanced(by: 1)]) << 8 : 0
+            val |= self.count > 0 ? UInt64(self[idx.advanced(by: 0)]) << 0 : 0
+            result.append(val)
+        }
+
+        for _ in result.count..<MemoryLayout<UInt64>.size {
+            result.append(0)
+        }
+
+        return result
+    }
+
+    /// Initialize integer from array of bytes. Caution: may be slow!
+    func toInteger<T:Integer>() -> T where T: ByteConvertible, T: BitshiftOperationsType {
+        if self.count == 0 {
+            return 0;
+        }
+
+        var bytes = self.reversed() //FIXME: check it this is equivalent of Array(...)
+        if bytes.count < MemoryLayout<T>.size {
+            let paddingCount = MemoryLayout<T>.size - bytes.count
+            if (paddingCount > 0) {
+                bytes += Array<UInt8>(repeating: 0, count: paddingCount)
+            }
+        }
+
+        if MemoryLayout<T>.size == 1 {
+            return T(truncatingBitPattern: UInt64(bytes[0]))
+        }
+
+        var result: T = 0
+        for byte in bytes.reversed() {
+            result = result << 8 | T(byte)
+        }
+        return result
+    }
+}

+ 10 - 19
Sources/CryptoSwift/Foundation/Data+Extension.swift

@@ -8,15 +8,6 @@
 
 import Foundation
 
-extension NSMutableData {
-    
-    /** Convenient way to append bytes */
-    internal func appendBytes(_ arrayOfBytes: Array<UInt8>) {
-        self.append(arrayOfBytes, length: arrayOfBytes.count)
-    }
-    
-}
-
 extension Data {
 
     /// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 65536
@@ -31,43 +22,43 @@ extension Data {
     }
     
     public func md5() -> Data {
-        let result = Hash.md5(self.bytes).calculate()
+        let result = Hash.md5(self.bytes)
         return Data(bytes: result)
     }
 
     public func sha1() -> Data? {
-        let result = Hash.sha1(self.bytes).calculate()
+        let result = Hash.sha1(self.bytes)
         return Data(bytes: result)
     }
 
     public func sha224() -> Data? {
-        let result = Hash.sha224(self.bytes).calculate()
+        let result = Hash.sha224(self.bytes)
         return Data(bytes: result)
     }
 
     public func sha256() -> Data? {
-        let result = Hash.sha256(self.bytes).calculate()
+        let result = Hash.sha256(self.bytes)
         return Data(bytes: result)
     }
 
     public func sha384() -> Data? {
-        let result = Hash.sha384(self.bytes).calculate()
+        let result = Hash.sha384(self.bytes)
         return Data(bytes: result)
     }
 
     public func sha512() -> Data? {
-        let result = Hash.sha512(self.bytes).calculate()
+        let result = Hash.sha512(self.bytes)
         return Data(bytes: result)
     }
 
     public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> Data? {
-        let result = Hash.crc32(self.bytes, seed: seed, reflect: reflect).calculate()
-        return Data(bytes: result)
+        let result = Checksum.crc32(self.bytes, seed: seed, reflect: reflect)
+        return Data(bytes: result.bytes())
     }
 
     public func crc16(seed: UInt16? = nil) -> Data? {
-        let result = Hash.crc16(self.bytes, seed: seed).calculate()
-        return Data(bytes: result)
+        let result = Checksum.crc16(self.bytes, seed: seed)
+        return Data(bytes: result.bytes())
     }
 
     public func encrypt(cipher: Cipher) throws -> Data {

+ 9 - 9
Sources/CryptoSwift/Foundation/Rabbit+Foundation.swift

@@ -9,20 +9,20 @@
 import Foundation
 
 extension Rabbit {
-    convenience public init?(key: String) {
+    convenience public init(key: String) throws {
         guard let kkey = key.bridge().data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false)?.bytes else {
-            return nil
+            throw Error.invalidKeyOrInitializationVector
         }
-        self.init(key: kkey)
-        
+        try self.init(key: kkey)
     }
     
-    convenience public init?(key: String, iv: String) {
+    convenience public init(key: String, iv: String) throws {
         guard let kkey = key.bridge().data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false)?.bytes,
-            let iiv = iv.bridge().data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false)?.bytes
-            else {
-                return nil
+              let iiv = iv.bridge().data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false)?.bytes
+        else {
+            throw Error.invalidKeyOrInitializationVector
         }
-        self.init(key: kkey, iv: iiv)
+
+        try self.init(key: kkey, iv: iiv)
     }
 }

+ 4 - 27
Sources/CryptoSwift/Generics.swift

@@ -33,40 +33,17 @@ func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T
     return bitPattern
 }
 
-/// Initialize integer from array of bytes.
-/// This method may be slow
-@_specialize(UInt32)
-func integerWith<T:Integer>(_ bytes: Array<UInt8>) -> T where T:ByteConvertible, T: BitshiftOperationsType {
-    var bytes = bytes.reversed() as Array<UInt8> //FIXME: check it this is equivalent of Array(...)
-    if bytes.count < sizeof(T.self) {
-        let paddingCount = sizeof(T.self) - bytes.count
-        if (paddingCount > 0) {
-            bytes += Array<UInt8>(repeating: 0, count: paddingCount)
-        }
-    }
-    
-    if sizeof(T.self) == 1 {
-        return T(truncatingBitPattern: UInt64(bytes.first!))
-    }
-    
-    var result: T = 0
-    for byte in bytes.reversed() {
-        result = result << 8 | T(byte)
-    }
-    return result
-}
-
 /// Array of bytes, little-endian representation. Don't use if not necessary.
 /// I found this method slow
 func arrayOfBytes<T>(value:T, length:Int? = nil) -> Array<UInt8> {
-    let totalBytes = length ?? sizeof(T.self)
+    let totalBytes = length ?? MemoryLayout<T>.size
 
     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
     valuePointer.pointee = value
 
     let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
     var bytes = Array<UInt8>(repeating: 0, count: totalBytes)
-    for j in 0..<min(sizeof(T.self),totalBytes) {
+    for j in 0..<min(MemoryLayout<T>.size,totalBytes) {
         bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
     }
     
@@ -78,7 +55,7 @@ func arrayOfBytes<T>(value:T, length:Int? = nil) -> Array<UInt8> {
 
 // MARK: - shiftLeft
 
-// helper to be able tomake shift operation on T
+// helper to be able to make shift operation on T
 @_specialize(Int)
 func << <T:SignedInteger>(lhs: T, rhs: Int) -> Int {
     let a = lhs as! Int
@@ -101,7 +78,7 @@ func shiftLeft<T: SignedInteger>(_ value: T, by count: Int) -> T where T: Initia
         return 0;
     }
     
-    let bitsCount = (sizeofValue(value) * 8)
+    let bitsCount = (MemoryLayout<T>.size * 8)
     let shiftCount = Int(Swift.min(count, bitsCount - 1))
     
     var shiftedValue:T = 0;

+ 20 - 12
Sources/CryptoSwift/HMAC.swift

@@ -6,7 +6,11 @@
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
 
-final public class HMAC {
+final public class HMAC: Authenticator {
+
+    public enum Error: Swift.Error {
+        case authenticateError
+    }
     
     public enum Variant {
         case sha1, sha256, sha384, sha512, md5
@@ -29,15 +33,15 @@ final public class HMAC {
         func calculateHash(_ bytes:Array<UInt8>) -> Array<UInt8>? {
             switch (self) {
             case .sha1:
-                return Hash.sha1(bytes).calculate()
+                return Hash.sha1(bytes)
             case .sha256:
-                return Hash.sha256(bytes).calculate()
+                return Hash.sha256(bytes)
             case .sha384:
-                return Hash.sha384(bytes).calculate()
+                return Hash.sha384(bytes)
             case .sha512:
-                return Hash.sha512(bytes).calculate()
+                return Hash.sha512(bytes)
             case .md5:
-                return Hash.md5(bytes).calculate()
+                return Hash.md5(bytes)
             }
         }
         
@@ -54,7 +58,7 @@ final public class HMAC {
     var key:Array<UInt8>
     let variant:Variant
 
-    public init? (key: Array<UInt8>, variant:HMAC.Variant = .md5) {
+    public init (key: Array<UInt8>, variant:HMAC.Variant = .md5) {
         self.variant = variant
         self.key = key
 
@@ -67,7 +71,9 @@ final public class HMAC {
         self.key = ZeroPadding().add(to: key, blockSize: variant.blockSize())
     }
 
-    public func authenticate(_ bytes:Array<UInt8>) -> Array<UInt8>? {
+    //MARK: Authenticator
+
+    public func authenticate(_ bytes:Array<UInt8>) throws -> Array<UInt8> {
         var opad = Array<UInt8>(repeating: 0x5c, count: variant.blockSize())
         for idx in key.indices {
             opad[idx] = key[idx] ^ opad[idx]
@@ -77,10 +83,12 @@ final public class HMAC {
             ipad[idx] = key[idx] ^ ipad[idx]
         }
 
-        var finalHash:Array<UInt8>? = nil;
-        if let ipadAndMessageHash = variant.calculateHash(ipad + bytes) {
-            finalHash = variant.calculateHash(opad + ipadAndMessageHash);
+        guard let ipadAndMessageHash = variant.calculateHash(ipad + bytes),
+              let result = variant.calculateHash(opad + ipadAndMessageHash) else
+        {
+            throw Error.authenticateError
         }
-        return finalHash
+
+        return result
     }
 }

+ 24 - 27
Sources/CryptoSwift/Hash.swift

@@ -1,36 +1,33 @@
 //
-//  CryptoHash.swift
+//  Hash.swift
 //  CryptoSwift
 //
 //  Created by Marcin Krzyzanowski on 07/08/14.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-public enum Hash {
-    case md5(Array<UInt8>)
-    case sha1(Array<UInt8>)
-    case sha224(Array<UInt8>), sha256(Array<UInt8>), sha384(Array<UInt8>), sha512(Array<UInt8>)
-    case crc32(Array<UInt8>, seed: UInt32?, reflect: Bool)
-    case crc16(Array<UInt8>, seed: UInt16?)
-    
-    public func calculate() -> Array<UInt8> {
-        switch self {
-        case .md5(let bytes):
-            return MD5(bytes).calculate()
-        case .sha1(let bytes):
-            return SHA1(bytes).calculate()
-        case .sha224(let bytes):
-            return SHA2(bytes, variant: .sha224).calculate32()
-        case .sha256(let bytes):
-            return SHA2(bytes, variant: .sha256).calculate32()
-        case .sha384(let bytes):
-            return SHA2(bytes, variant: .sha384).calculate64()
-        case .sha512(let bytes):
-            return SHA2(bytes, variant: .sha512).calculate64()
-        case .crc32(let bytes, let seed, let reflect):
-            return CRC().crc32(bytes, seed: seed, reflect: reflect).bytes()
-        case .crc16(let bytes, let seed):
-            return UInt32(CRC().crc16(bytes, seed: seed)).bytes(totalBytes: 2)
-        }
+public struct Hash {
+    public static func md5(_ bytes: Array<UInt8>) -> Array<UInt8> {
+        return MD5(bytes).calculate()
+    }
+
+    public static func sha1(_ bytes: Array<UInt8>) -> Array<UInt8> {
+        return SHA1(bytes).calculate()
+    }
+
+    public static func sha224(_ bytes: Array<UInt8>) -> Array<UInt8> {
+        return SHA2(bytes, variant: .sha224).calculate32()
+    }
+
+    public static func sha256(_ bytes: Array<UInt8>) -> Array<UInt8> {
+        return SHA2(bytes, variant: .sha256).calculate32()
+    }
+
+    public static func sha384(_ bytes: Array<UInt8>) -> Array<UInt8> {
+        return SHA2(bytes, variant: .sha384).calculate64()
+    }
+
+    public static func sha512(_ bytes: Array<UInt8>) -> Array<UInt8> {
+        return SHA2(bytes, variant: .sha512).calculate64()
     }
 }

+ 11 - 15
Sources/CryptoSwift/IntExtension.swift → Sources/CryptoSwift/Int+Extension.swift

@@ -30,18 +30,14 @@ extension Int {
 
 /* array of bytes */
 extension Int {
-    /** Array of bytes with optional padding (little-endian) */
-    public func bytes(totalBytes: Int = sizeof(Int.self)) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
-    }
-
-    public static func with(_ bytes: ArraySlice<UInt8>) -> Int {
-        return integerWith(Array(bytes))
+    /** Int with collection of bytes (little-endian) */
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
     }
 
-    /** Int with array bytes (little-endian) */
-    public static func with(_ bytes: Array<UInt8>) -> Int {
-        return integerWith(bytes)
+    /** Array of bytes with optional padding (little-endian) */
+    func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
     }
 }
 
@@ -51,17 +47,17 @@ extension Int {
 extension Int {
     
     /** Shift bits to the left. All bits are shifted (including sign bit) */
-    fileprivate mutating func shiftLeft(by count: Int) {
+    mutating func shiftLeft(by count: Int) {
         self = CryptoSwift.shiftLeft(self, by: count) //FIXME: count:
     }
     
     /** Shift bits to the right. All bits are shifted (including sign bit) */
-    fileprivate mutating func shiftRight(by count: Int) {
+    mutating func shiftRight(by count: Int) {
         if (self == 0) {
             return
         }
         
-        let bitsCount = sizeofValue(self) * 8
+        let bitsCount = MemoryLayout<Int>.size * 8
 
         if (count >= bitsCount) {
             return
@@ -85,12 +81,12 @@ extension Int {
 // Left operator
 
 /** shift left and assign with bits truncation */
-public func &<<= (lhs: inout Int, rhs: Int) {
+func &<<= (lhs: inout Int, rhs: Int) {
     lhs.shiftLeft(by: rhs)
 }
 
 /** shift left with bits truncation */
-public func &<< (lhs: Int, rhs: Int) -> Int {
+func &<< (lhs: Int, rhs: Int) -> Int {
     var l = lhs;
     l.shiftLeft(by: rhs)
     return l

+ 1 - 1
Sources/CryptoSwift/MD5.swift

@@ -56,7 +56,7 @@ final class MD5 : HashProtocol  {
         let chunkSizeBytes = 512 / 8 // 64
         for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
-            var M = sliceToUInt32Array(chunk)
+            var M = chunk.toUInt32Array()
             assert(M.count == 16, "Invalid array")
             
             // Initialize hash value for this chunk:

+ 4 - 19
Sources/CryptoSwift/Operators.swift

@@ -22,22 +22,7 @@ j &<<= 2        //shift left and assign
 @see: https://medium.com/@krzyzanowskim/swiftly-shift-bits-and-protect-yourself-be33016ce071
 */
 
-infix operator &<<= {
-  associativity none
-  precedence 160
-}
-
-infix operator &<< {
-  associativity none
-  precedence 160
-}
-
-infix operator &>>= {
-  associativity none
-  precedence 160
-}
-
-infix operator &>> {
-  associativity none
-  precedence 160
-}
+infix operator &<<= : BitwiseShiftPrecedence
+infix operator &<< : BitwiseShiftPrecedence
+infix operator &>>= : BitwiseShiftPrecedence
+infix operator &>> : BitwiseShiftPrecedence

+ 2 - 2
Sources/CryptoSwift/PKCS5/PBKDF1.swift

@@ -35,9 +35,9 @@ public extension PKCS5 {
             fileprivate func calculateHash(_ bytes:Array<UInt8>) -> Array<UInt8>? {
                 switch (self) {
                 case .sha1:
-                    return Hash.sha1(bytes).calculate()
+                    return Hash.sha1(bytes)
                 case .md5:
-                    return Hash.md5(bytes).calculate()
+                    return Hash.md5(bytes)
                 }
             }
         }

+ 18 - 12
Sources/CryptoSwift/PKCS5/PBKDF2.swift

@@ -37,8 +37,10 @@ public extension PKCS5 {
         ///   - keyLength: intended length of derived key
         public init(password: Array<UInt8>, salt: Array<UInt8>, iterations: Int = 4096 /* c */, keyLength: Int? = nil /* dkLen */, variant: HMAC.Variant = .sha256) throws {
             precondition(iterations > 0)
+
+            let prf = HMAC(key: password, variant: variant)
             
-            guard let prf = HMAC(key: password, variant: variant), iterations > 0 && !password.isEmpty && !salt.isEmpty else {
+            guard iterations > 0 && !password.isEmpty && !salt.isEmpty else {
                 throw Error.invalidInput
             }
 
@@ -82,22 +84,26 @@ fileprivate extension PKCS5.PBKDF2 {
     // F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
     // U_1 = PRF (P, S || INT (i))
     func calculateBlock(_ salt: Array<UInt8>, blockNum: UInt) -> Array<UInt8>? {
-        guard let u1 = prf.authenticate(salt + INT(blockNum)) else {
+        guard let u1 = try? prf.authenticate(salt + INT(blockNum)) else {
             return nil
         }
 
-        var u = u1
-        var ret = u
-        if self.iterations > 1 {
-            // U_2 = PRF (P, U_1) ,
-            // U_c = PRF (P, U_{c-1}) .
-            for _ in 2...self.iterations {
-                u = prf.authenticate(u)!
-                for x in 0..<ret.count {
-                    ret[x] = ret[x] ^ u[x]
+        do {
+            var u = u1
+            var ret = u
+            if self.iterations > 1 {
+                // U_2 = PRF (P, U_1) ,
+                // U_c = PRF (P, U_{c-1}) .
+                for _ in 2...self.iterations {
+                    u = try prf.authenticate(u)
+                    for x in 0..<ret.count {
+                        ret[x] = ret[x] ^ u[x]
+                    }
                 }
             }
+            return ret
+        } catch {
+            return nil
         }
-        return ret
     }
 }

+ 35 - 28
Sources/CryptoSwift/Poly1305.swift

@@ -10,7 +10,12 @@
 //  Poly1305 takes a 32-byte, one-time key and a message and produces a 16-byte tag that authenticates the
 //  message such that an attacker has a negligible chance of producing a valid tag for an inauthentic message.
 
-final public class Poly1305 {
+final public class Poly1305: Authenticator {
+
+    public enum Error: Swift.Error {
+        case authenticateError
+    }
+
     let blockSize = 16
     private var ctx:Context?
     
@@ -23,12 +28,9 @@ final public class Poly1305 {
         var final:UInt8   = 0
         var leftover:Int = 0
         
-        init?(_ key: Array<UInt8>) {
-            assert(key.count == 32,"Invalid key length");
-            if (key.count != 32) {
-                return nil;
-            }
-            
+        init(_ key: Array<UInt8>) {
+            precondition(key.count == 32, "Invalid key length")
+
             for i in 0..<17 {
                 h[i] = 0
             }
@@ -74,29 +76,9 @@ final public class Poly1305 {
             }
         }
     }
-
-    /**
-     Calculate Message Authentication Code (MAC) for message.
-     Calculation context is discarder on instance deallocation.
-
-     - parameter key:     256-bit key
-     - parameter message: Message
-
-     - returns: Message Authentication Code
-     */
-    public func authenticate(_ bytes:Array<UInt8>) -> Array<UInt8>? {
-        if let ctx = self.ctx {
-            update(ctx, message: bytes)
-            return finish(ctx)
-        }
-        return nil
-    }
     
-    public init? (key: Array<UInt8>) {
+    public init (key: Array<UInt8>) {
         ctx = Context(key)
-        if (ctx == nil) {
-            return nil
-        }
     }
 
     // MARK: - Private
@@ -290,4 +272,29 @@ final public class Poly1305 {
             bytes -= blockSize
         }
     }
+
+    //MARK: - Authenticator
+
+    /**
+     Calculate Message Authentication Code (MAC) for message.
+     Calculation context is discarder on instance deallocation.
+
+     - parameter key:     256-bit key
+     - parameter message: Message
+
+     - returns: Message Authentication Code
+     */
+    public func authenticate(_ bytes:Array<UInt8>) throws -> Array<UInt8> {
+        guard let ctx = self.ctx else {
+            throw Error.authenticateError
+        }
+
+        update(ctx, message: bytes)
+
+        guard let result = finish(ctx) else {
+            throw Error.authenticateError
+        }
+
+        return result
+    }
 }

+ 13 - 9
Sources/CryptoSwift/Rabbit.swift

@@ -9,7 +9,11 @@
 private typealias Key = SecureBytes
 
 final public class Rabbit: BlockCipher {
-    
+
+    public enum Error: Swift.Error {
+        case invalidKeyOrInitializationVector
+    }
+
     /// Size of IV in bytes
     public static let ivSize = 64 / 8
     
@@ -47,16 +51,16 @@ final public class Rabbit: BlockCipher {
     ]
     
     // MARK: - Initializers
-    convenience public init?(key:Array<UInt8>) {
-        self.init(key: key, iv: nil)
+    convenience public init(key:Array<UInt8>) throws {
+        try self.init(key: key, iv: nil)
     }
     
-    public init?(key:Array<UInt8>, iv:Array<UInt8>?) {
+    public init(key:Array<UInt8>, iv:Array<UInt8>?) throws {
         self.key = Key(bytes: key)
         self.iv = iv
         
         guard key.count == Rabbit.keySize && (iv == nil || iv!.count == Rabbit.ivSize) else {
-            return nil
+            throw Error.invalidKeyOrInitializationVector
         }
     }
     
@@ -100,10 +104,10 @@ final public class Rabbit: BlockCipher {
     private func setupIV(_ iv: Array<UInt8>) {
         // 63...56 55...48 47...40 39...32 31...24 23...16 15...8 7...0 IV bits
         //    0       1       2       3       4       5       6     7   IV bytes in array
-        let iv0: UInt32 = integerWith([iv[4], iv[5], iv[6], iv[7]])
-        let iv1: UInt32 = integerWith([iv[0], iv[1], iv[4], iv[5]])
-        let iv2: UInt32 = integerWith([iv[0], iv[1], iv[2], iv[3]])
-        let iv3: UInt32 = integerWith([iv[2], iv[3], iv[6], iv[7]])
+        let iv0 = UInt32(bytes: [iv[4], iv[5], iv[6], iv[7]])
+        let iv1 = UInt32(bytes: [iv[0], iv[1], iv[4], iv[5]])
+        let iv2 = UInt32(bytes: [iv[0], iv[1], iv[2], iv[3]])
+        let iv3 = UInt32(bytes: [iv[2], iv[3], iv[6], iv[7]])
         
         // Modify the counter state as function of the IV
         c[0] = c[0] ^ iv0

+ 3 - 3
Sources/CryptoSwift/SHA1.swift

@@ -34,9 +34,9 @@ final class SHA1 : HashProtocol {
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
-                    let end = start + sizeofValue(M[x])
-                    let le = sliceToUInt32Array(chunk[start..<end])[0]
+                    let start = chunk.startIndex + (x * MemoryLayout<UInt32>.size)
+                    let end = start + MemoryLayout<UInt32>.size
+                    let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:

+ 7 - 7
Sources/CryptoSwift/SHA2.swift

@@ -130,13 +130,13 @@ final class SHA2 : HashProtocol {
         for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
             // Extend the sixteen 32-bit words into sixty-four 32-bit words:
-            var M:Array<UInt32> = Array<UInt32>(repeating: 0, count: variant.k.count)
+            var M = Array<UInt32>(repeating: 0, count: variant.k.count)
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
-                    let end = start + sizeofValue(M[x])
-                    let le = sliceToUInt32Array(chunk[start..<end])[0]
+                    let start = chunk.startIndex + (x * MemoryLayout<UInt32>.size)
+                    let end = start + MemoryLayout<UInt32>.size
+                    let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:
@@ -217,9 +217,9 @@ final class SHA2 : HashProtocol {
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
-                    let end = start + sizeofValue(M[x])
-                    let le = sliceToUInt64Array(chunk[start..<end])[0]
+                    let start = chunk.startIndex + (x * MemoryLayout<UInt64>.size)
+                    let end = start + MemoryLayout<UInt64>.size
+                    let le = chunk[start..<end].toUInt64Array()[0]
                     M[x] = le.bigEndian
                     break
                 default:

+ 9 - 10
Sources/CryptoSwift/String+Extension.swift

@@ -34,23 +34,22 @@ extension String {
     }
 
     public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> String {
-        return self.utf8.lazy.map({ $0 as UInt8 }).crc32(seed: seed, reflect: reflect).toHexString()
+        return self.utf8.lazy.map({ $0 as UInt8 }).crc32(seed: seed, reflect: reflect).bytes().toHexString()
     }
 
     public func crc16(seed: UInt16? = nil) -> String {
-        return self.utf8.lazy.map({ $0 as UInt8 }).crc16(seed: seed).toHexString()
+        return self.utf8.lazy.map({ $0 as UInt8 }).crc16(seed: seed).bytes().toHexString()
     }
 
-    public func encrypt(cipher: Cipher) throws -> Array<UInt8> {
-        return try self.utf8.lazy.map({ $0 as UInt8 }).encrypt(cipher: cipher)
+    /// Returns hex string of bytes
+    public func encrypt(cipher: Cipher) throws -> String {
+        return try self.utf8.lazy.map({ $0 as UInt8 }).encrypt(cipher: cipher).toHexString()
     }
 
-    public func decrypt(cipher: Cipher) throws -> Array<UInt8> {
-        return try self.utf8.lazy.map({ $0 as UInt8 }).decrypt(cipher: cipher)
-    }
-    
-    /// Returns hex string of bytes.
-    public func authenticate(with authenticator: Authenticator) throws -> String {
+    // decrypt() does not make sense for String
+
+    /// Returns hex string of bytes
+    public func authenticate<A: Authenticator>(with authenticator: A) throws -> String {
         return try self.utf8.lazy.map({ $0 as UInt8 }).authenticate(with: authenticator).toHexString()
     }
 }

+ 18 - 0
Sources/CryptoSwift/UInt16+Extension.swift

@@ -0,0 +1,18 @@
+//
+//  UInt16+Extension.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 06/08/16.
+//  Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
+//
+
+/** array of bytes */
+extension UInt16 {
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
+    }
+
+    func bytes(totalBytes: Int = MemoryLayout<UInt16>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
+    }
+}

+ 10 - 15
Sources/CryptoSwift/UInt32Extension.swift → Sources/CryptoSwift/UInt32+Extension.swift

@@ -18,17 +18,12 @@ extension UInt32: _UInt32Type {}
 
 /** array of bytes */
 extension UInt32 {
-    public func bytes(totalBytes: Int = sizeof(UInt32.self)) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
-    }
-
-    public static func with(bytes: ArraySlice<UInt8>) -> UInt32 {
-        return integerWith(Array(bytes))
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
     }
 
-    /** Int with array bytes (little-endian) */
-    public static func with(bytes: Array<UInt8>) -> UInt32 {
-        return integerWith(bytes)
+    func bytes(totalBytes: Int = MemoryLayout<UInt32>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
     }
 }
 
@@ -36,12 +31,12 @@ extension UInt32 {
 extension UInt32 {
     
     /** Shift bits to the left. All bits are shifted (including sign bit) */
-    fileprivate mutating func shiftLeft(by count: UInt32) {
+    mutating func shiftLeft(by count: UInt32) {
         if (self == 0) {
             return
         }
         
-        let bitsCount = UInt32(sizeof(UInt32.self) * 8)
+        let bitsCount = UInt32(MemoryLayout<UInt32>.size * 8)
         let shiftCount = Swift.min(count, bitsCount - 1)
         var shiftedValue:UInt32 = 0;
         
@@ -62,12 +57,12 @@ extension UInt32 {
     }
     
     /** Shift bits to the right. All bits are shifted (including sign bit) */
-    fileprivate mutating func shiftRight(by count: UInt32) {
+    mutating func shiftRight(by count: UInt32) {
         if (self == 0) {
             return
         }
         
-        let bitsCount = UInt32(sizeofValue(self) * 8)
+        let bitsCount = UInt32(MemoryLayout<UInt32>.size * 8)
 
         if (count >= bitsCount) {
             return
@@ -90,12 +85,12 @@ extension UInt32 {
 }
 
 /** shift left and assign with bits truncation */
-public func &<<= (lhs: inout UInt32, rhs: UInt32) {
+func &<<= (lhs: inout UInt32, rhs: UInt32) {
     lhs.shiftLeft(by: rhs)
 }
 
 /** shift left with bits truncation */
-public func &<< (lhs: UInt32, rhs: UInt32) -> UInt32 {
+func &<< (lhs: UInt32, rhs: UInt32) -> UInt32 {
     var l = lhs;
     l.shiftLeft(by: rhs)
     return l

+ 18 - 0
Sources/CryptoSwift/UInt64+Extension.swift

@@ -0,0 +1,18 @@
+//
+//  UInt64Extension.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 02/09/14.
+//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
+//
+
+/** array of bytes */
+extension UInt64 {
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
+    }
+
+    func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
+    }
+}

+ 0 - 23
Sources/CryptoSwift/UInt64Extension.swift

@@ -1,23 +0,0 @@
-//
-//  UInt64Extension.swift
-//  CryptoSwift
-//
-//  Created by Marcin Krzyzanowski on 02/09/14.
-//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
-//
-
-/** array of bytes */
-extension UInt64 {
-    public func bytes(totalBytes: Int = sizeof(UInt64.self)) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
-    }
-
-    public static func with(bytes: ArraySlice<UInt8>) -> UInt64 {
-        return integerWith(Array(bytes))
-    }
-
-    /** Int with array bytes (little-endian) */
-    public static func with(bytes: Array<UInt8>) -> UInt64 {
-        return integerWith(bytes)
-    }
-}

+ 2 - 2
Sources/CryptoSwift/UInt8Extension.swift → Sources/CryptoSwift/UInt8+Extension.swift

@@ -46,7 +46,7 @@ extension UInt8 {
     
     /** array of bits */
     func bits() -> [Bit] {
-        let totalBitsCount = sizeofValue(self) * 8
+        let totalBitsCount = MemoryLayout<UInt8>.size * 8
         
         var bitsArray = [Bit](repeating: Bit.zero, count: totalBitsCount)
         
@@ -80,7 +80,7 @@ extension UInt8 {
             return
         }
 
-        let bitsCount = UInt8(sizeof(UInt8.self) * 8)
+        let bitsCount = UInt8(MemoryLayout<UInt8>.size * 8)
 
         if (count >= bitsCount) {
             return

+ 7 - 7
Sources/CryptoSwift/Updatable.swift

@@ -12,7 +12,7 @@ public protocol Updatable {
     /// - parameter bytes: Bytes to process
     /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call.
     /// - returns: Processed data or empty array.
-    mutating func update<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, isLast: Bool) throws -> Array<UInt8>
+    mutating func update<T: Sequence>(withBytes bytes:T, isLast: Bool) throws -> Array<UInt8> where T.Iterator.Element == UInt8
 
     /// Update given bytes in chunks.
     ///
@@ -20,29 +20,29 @@ public protocol Updatable {
     /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call.
     /// - parameter output: Resulting data
     /// - returns: Processed data or empty array.
-    mutating func update<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, isLast: Bool, output: (Array<UInt8>) -> Void) throws
+    mutating func update<T: Sequence>(withBytes bytes:T, isLast: Bool, output: (Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8
 
     /// Finish updates. This may apply padding.
     /// - parameter bytes: Bytes to process
     /// - returns: Processed data.
-    mutating func finish<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T) throws -> Array<UInt8>
+    mutating func finish<T: Sequence>(withBytes bytes:T) throws -> Array<UInt8> where T.Iterator.Element == UInt8
 
     /// Finish updates. This may apply padding.
     /// - parameter bytes: Bytes to process
     /// - parameter output: Resulting data
     /// - returns: Processed data.
-    mutating func finish<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, output: (Array<UInt8>) -> Void) throws
+    mutating func finish<T: Sequence>(withBytes bytes:T, output: (Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8
 }
 
 extension Updatable {
-    mutating public func update<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, isLast: Bool = false, output: (Array<UInt8>) -> Void) throws {
+    mutating public func update<T: Sequence>(withBytes bytes:T, isLast: Bool = false, output: (Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8 {
         let processed = try self.update(withBytes: bytes, isLast: isLast)
         if (!processed.isEmpty) {
             output(processed)
         }
     }
 
-    mutating public func finish<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T) throws -> Array<UInt8> {
+    mutating public func finish<T: Sequence>(withBytes bytes:T) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
         return try self.update(withBytes: bytes, isLast: true)
     }
     
@@ -50,7 +50,7 @@ extension Updatable {
         return try self.update(withBytes: [], isLast: true)
     }
 
-    mutating public func finish<T: Sequence where T.Iterator.Element == UInt8>(withBytes bytes:T, output: (Array<UInt8>) -> Void) throws {
+    mutating public func finish<T: Sequence>(withBytes bytes:T, output: (Array<UInt8>) -> Void) throws where T.Iterator.Element == UInt8 {
         let processed = try self.update(withBytes: bytes, isLast: true)
         if (!processed.isEmpty) {
             output(processed)

+ 0 - 32
Sources/CryptoSwift/Utils.swift

@@ -52,38 +52,6 @@ func reversed(_ uint32 : UInt32) -> UInt32 {
     return v
 }
 
-func sliceToUInt32Array(_ slice: ArraySlice<UInt8>) -> Array<UInt32> {
-    var result = Array<UInt32>()
-    result.reserveCapacity(16)
-    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt32.self)) {
-        let val1:UInt32 = (UInt32(slice[idx.advanced(by: 3)]) << 24)
-        let val2:UInt32 = (UInt32(slice[idx.advanced(by: 2)]) << 16)
-        let val3:UInt32 = (UInt32(slice[idx.advanced(by: 1)]) << 8)
-        let val4:UInt32 = UInt32(slice[idx])
-        let val:UInt32 = val1 | val2 | val3 | val4
-        result.append(val)
-    }
-    return result
-}
-
-func sliceToUInt64Array(_ slice: ArraySlice<UInt8>) -> Array<UInt64> {
-    var result = Array<UInt64>()
-    result.reserveCapacity(32)
-    for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt64.self)) {
-        var val:UInt64 = 0
-        val |= UInt64(slice[idx.advanced(by: 7)]) << 56
-        val |= UInt64(slice[idx.advanced(by: 6)]) << 48
-        val |= UInt64(slice[idx.advanced(by: 5)]) << 40
-        val |= UInt64(slice[idx.advanced(by: 4)]) << 32
-        val |= UInt64(slice[idx.advanced(by: 3)]) << 24
-        val |= UInt64(slice[idx.advanced(by: 2)]) << 16
-        val |= UInt64(slice[idx.advanced(by: 1)]) << 8
-        val |= UInt64(slice[idx.advanced(by: 0)]) << 0
-        result.append(val)
-    }
-    return result
-}
-
 func xor(_ a: Array<UInt8>, _ b:Array<UInt8>) -> Array<UInt8> {
     var xored = Array<UInt8>(repeating: 0, count: min(a.count, b.count))
     for i in 0..<xored.count {