Marcin Krzyżanowski 8 жил өмнө
parent
commit
70fe18456e
59 өөрчлөгдсөн 1039 нэмэгдсэн , 1017 устгасан
  1. 24 24
      Sources/CryptoSwift/AES.swift
  2. 3 3
      Sources/CryptoSwift/Array+Extension.swift
  3. 1 0
      Sources/CryptoSwift/Bit.swift
  4. 1 1
      Sources/CryptoSwift/BlockMode/BlockModeOptions.swift
  5. 1 1
      Sources/CryptoSwift/BlockMode/CBC.swift
  6. 2 2
      Sources/CryptoSwift/BlockMode/CTR.swift
  7. 3 3
      Sources/CryptoSwift/BytesSequence.swift
  8. 12 9
      Sources/CryptoSwift/CSArrayType+Extensions.swift
  9. 55 53
      Sources/CryptoSwift/ChaCha20.swift
  10. 74 75
      Sources/CryptoSwift/Checksum.swift
  11. 6 5
      Sources/CryptoSwift/Collection+Extension.swift
  12. 4 3
      Sources/CryptoSwift/Cryptors.swift
  13. 1 2
      Sources/CryptoSwift/Digest.swift
  14. 3 2
      Sources/CryptoSwift/Foundation/AES+Foundation.swift
  15. 1 0
      Sources/CryptoSwift/Foundation/CSArrayType+Foundation.swift
  16. 2 1
      Sources/CryptoSwift/Foundation/ChaCha20+Foundation.swift
  17. 6 7
      Sources/CryptoSwift/Foundation/Data+Extension.swift
  18. 3 2
      Sources/CryptoSwift/Foundation/HMAC+Foundation.swift
  19. 5 4
      Sources/CryptoSwift/Foundation/Rabbit+Foundation.swift
  20. 0 1
      Sources/CryptoSwift/Foundation/String+FoundationExtension.swift
  21. 7 7
      Sources/CryptoSwift/Foundation/Utils+Foundation.swift
  22. 21 23
      Sources/CryptoSwift/Generics.swift
  23. 13 14
      Sources/CryptoSwift/HMAC.swift
  24. 5 4
      Sources/CryptoSwift/Int+Extension.swift
  25. 15 10
      Sources/CryptoSwift/IntegerConvertible.swift
  26. 38 39
      Sources/CryptoSwift/MD5.swift
  27. 6 6
      Sources/CryptoSwift/NoPadding.swift
  28. 12 12
      Sources/CryptoSwift/Operators.swift
  29. 6 6
      Sources/CryptoSwift/PKCS5/PBKDF1.swift
  30. 11 9
      Sources/CryptoSwift/PKCS5/PBKDF2.swift
  31. 1 2
      Sources/CryptoSwift/PKCS5/PKCS5.swift
  32. 7 8
      Sources/CryptoSwift/PKCS7.swift
  33. 2 2
      Sources/CryptoSwift/Padding.swift
  34. 115 116
      Sources/CryptoSwift/Poly1305.swift
  35. 45 44
      Sources/CryptoSwift/Rabbit.swift
  36. 19 20
      Sources/CryptoSwift/SHA1.swift
  37. 83 83
      Sources/CryptoSwift/SHA2.swift
  38. 18 18
      Sources/CryptoSwift/SHA3.swift
  39. 3 4
      Sources/CryptoSwift/String+Extension.swift
  40. 1 0
      Sources/CryptoSwift/UInt16+Extension.swift
  41. 2 2
      Sources/CryptoSwift/UInt32+Extension.swift
  42. 2 0
      Sources/CryptoSwift/UInt64+Extension.swift
  43. 12 14
      Sources/CryptoSwift/UInt8+Extension.swift
  44. 11 10
      Sources/CryptoSwift/Updatable.swift
  45. 11 12
      Sources/CryptoSwift/Utils.swift
  46. 5 5
      Sources/CryptoSwift/ZeroPadding.swift
  47. 139 137
      Tests/CryptoSwiftTests/AESTests.swift
  48. 33 33
      Tests/CryptoSwiftTests/Access.swift
  49. 50 48
      Tests/CryptoSwiftTests/ChaCha20Tests.swift
  50. 36 34
      Tests/CryptoSwiftTests/DigestTests.swift
  51. 5 4
      Tests/CryptoSwiftTests/Error+Extension.swift
  52. 21 18
      Tests/CryptoSwiftTests/ExtensionsTest.swift
  53. 21 22
      Tests/CryptoSwiftTests/HMACTests.swift
  54. 6 8
      Tests/CryptoSwiftTests/PBKDF.swift
  55. 14 13
      Tests/CryptoSwiftTests/PaddingTests.swift
  56. 9 9
      Tests/CryptoSwiftTests/Poly1305Tests.swift
  57. 23 20
      Tests/CryptoSwiftTests/RabbitTests.swift
  58. 3 2
      Tests/CryptoSwiftTests/RandomBytesSequenceTests.swift
  59. 1 1
      Tests/LinuxMain.swift

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 24 - 24
Sources/CryptoSwift/AES.swift


+ 3 - 3
Sources/CryptoSwift/Array+Extension.swift

@@ -13,7 +13,7 @@ extension Array {
         var words = Array<Array<Element>>()
         var words = Array<Array<Element>>()
         words.reserveCapacity(self.count / chunksize)
         words.reserveCapacity(self.count / chunksize)
         for idx in stride(from: chunksize, through: self.count, by: chunksize) {
         for idx in stride(from: chunksize, through: self.count, by: chunksize) {
-            words.append(Array(self[idx - chunksize..<idx])) // slow for large table
+            words.append(Array(self[idx - chunksize ..< idx])) // slow for large table
         }
         }
         let reminder = self.suffix(self.count % chunksize)
         let reminder = self.suffix(self.count % chunksize)
         if !reminder.isEmpty {
         if !reminder.isEmpty {
@@ -24,9 +24,10 @@ extension Array {
 }
 }
 
 
 extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
 extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
+
     public init(hex: String) {
     public init(hex: String) {
         self.init()
         self.init()
-        
+
         let utf8 = Array<Element.IntegerLiteralType>(hex.utf8)
         let utf8 = Array<Element.IntegerLiteralType>(hex.utf8)
         let skip0x = hex.hasPrefix("0x") ? 2 : 0
         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)) {
         for idx in stride(from: utf8.startIndex.advanced(by: skip0x), to: utf8.endIndex, by: utf8.startIndex.advanced(by: 2)) {
@@ -37,4 +38,3 @@ extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
         }
         }
     }
     }
 }
 }
-

+ 1 - 0
Sources/CryptoSwift/Bit.swift

@@ -12,6 +12,7 @@ enum Bit: Int {
 }
 }
 
 
 extension Bit {
 extension Bit {
+
     func inverted() -> Bit {
     func inverted() -> Bit {
         return self == .zero ? .one : .zero
         return self == .zero ? .one : .zero
     }
     }

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

@@ -12,4 +12,4 @@ struct BlockModeOptions: OptionSet {
     static let None = BlockModeOptions(rawValue: 0)
     static let None = BlockModeOptions(rawValue: 0)
     static let InitializationVectorRequired = BlockModeOptions(rawValue: 1)
     static let InitializationVectorRequired = BlockModeOptions(rawValue: 1)
     static let PaddingRequired = BlockModeOptions(rawValue: 2)
     static let PaddingRequired = BlockModeOptions(rawValue: 2)
-}
+}

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

@@ -25,7 +25,7 @@ struct CBCModeWorker: BlockModeWorker {
             return plaintext
             return plaintext
         }
         }
         prev = ciphertext
         prev = ciphertext
-        return ciphertext 
+        return ciphertext
     }
     }
 
 
     mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
     mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {

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

@@ -38,8 +38,8 @@ struct CTRModeWorker: RandomAccessBlockModeWorker {
 
 
 private func buildNonce(_ iv: Array<UInt8>, counter: UInt64) -> Array<UInt8> {
 private func buildNonce(_ iv: Array<UInt8>, counter: UInt64) -> Array<UInt8> {
     let noncePartLen = AES.blockSize / 2
     let noncePartLen = AES.blockSize / 2
-    let noncePrefix = Array(iv[0..<noncePartLen])
-    let nonceSuffix = Array(iv[noncePartLen..<iv.count])
+    let noncePrefix = Array(iv[0 ..< noncePartLen])
+    let nonceSuffix = Array(iv[noncePartLen ..< iv.count])
     let c = UInt64(bytes: nonceSuffix) + counter
     let c = UInt64(bytes: nonceSuffix) + counter
     return noncePrefix + c.bytes()
     return noncePrefix + c.bytes()
 }
 }

+ 3 - 3
Sources/CryptoSwift/BytesSequence.swift

@@ -8,7 +8,7 @@
 
 
 /// Generic version of BytesSequence is slower, therefore specialized version is in use
 /// Generic version of BytesSequence is slower, therefore specialized version is in use
 ///
 ///
-//struct BytesSequence<D: RandomAccessCollection>: Sequence where D.Iterator.Element == UInt8, D.IndexDistance == Int, D.SubSequence.IndexDistance == Int, D.Index == Int {
+// struct BytesSequence<D: RandomAccessCollection>: Sequence where D.Iterator.Element == UInt8, D.IndexDistance == Int, D.SubSequence.IndexDistance == Int, D.Index == Int {
 //    let chunkSize: D.IndexDistance
 //    let chunkSize: D.IndexDistance
 //    let data: D
 //    let data: D
 //
 //
@@ -24,7 +24,7 @@
 //            return nil
 //            return nil
 //        }
 //        }
 //    }
 //    }
-//}
+// }
 
 
 struct BytesSequence: Sequence {
 struct BytesSequence: Sequence {
     let chunkSize: Array<UInt8>.IndexDistance
     let chunkSize: Array<UInt8>.IndexDistance
@@ -34,7 +34,7 @@ struct BytesSequence: Sequence {
         var offset = data.startIndex
         var offset = data.startIndex
         return AnyIterator {
         return AnyIterator {
             let end = Swift.min(self.chunkSize, self.data.count &- offset)
             let end = Swift.min(self.chunkSize, self.data.count &- offset)
-            let result = self.data[offset..<offset &+ end]
+            let result = self.data[offset ..< offset &+ end]
             offset = offset.advanced(by: result.count)
             offset = offset.advanced(by: result.count)
             if !result.isEmpty {
             if !result.isEmpty {
                 return result
                 return result

+ 12 - 9
Sources/CryptoSwift/CSArrayType+Extensions.swift

@@ -11,12 +11,14 @@ public protocol CSArrayType: Collection, RangeReplaceableCollection {
 }
 }
 
 
 extension Array: CSArrayType {
 extension Array: CSArrayType {
+
     public func cs_arrayValue() -> [Iterator.Element] {
     public func cs_arrayValue() -> [Iterator.Element] {
         return self
         return self
     }
     }
 }
 }
 
 
 public extension CSArrayType where Iterator.Element == UInt8 {
 public extension CSArrayType where Iterator.Element == UInt8 {
+
     public func toHexString() -> String {
     public func toHexString() -> String {
         return self.lazy.reduce("") {
         return self.lazy.reduce("") {
             var s = String($1, radix: 16)
             var s = String($1, radix: 16)
@@ -29,26 +31,27 @@ public extension CSArrayType where Iterator.Element == UInt8 {
 }
 }
 
 
 public extension CSArrayType where Iterator.Element == UInt8 {
 public extension CSArrayType where Iterator.Element == UInt8 {
+
     public func md5() -> [Iterator.Element] {
     public func md5() -> [Iterator.Element] {
         return Digest.md5(cs_arrayValue())
         return Digest.md5(cs_arrayValue())
     }
     }
-    
+
     public func sha1() -> [Iterator.Element] {
     public func sha1() -> [Iterator.Element] {
         return Digest.sha1(cs_arrayValue())
         return Digest.sha1(cs_arrayValue())
     }
     }
-    
+
     public func sha224() -> [Iterator.Element] {
     public func sha224() -> [Iterator.Element] {
         return Digest.sha224(cs_arrayValue())
         return Digest.sha224(cs_arrayValue())
     }
     }
-    
+
     public func sha256() -> [Iterator.Element] {
     public func sha256() -> [Iterator.Element] {
         return Digest.sha256(cs_arrayValue())
         return Digest.sha256(cs_arrayValue())
     }
     }
-    
+
     public func sha384() -> [Iterator.Element] {
     public func sha384() -> [Iterator.Element] {
         return Digest.sha384(cs_arrayValue())
         return Digest.sha384(cs_arrayValue())
     }
     }
-    
+
     public func sha512() -> [Iterator.Element] {
     public func sha512() -> [Iterator.Element] {
         return Digest.sha512(cs_arrayValue())
         return Digest.sha512(cs_arrayValue())
     }
     }
@@ -57,14 +60,14 @@ public extension CSArrayType where Iterator.Element == UInt8 {
         return Digest.sha3(cs_arrayValue(), variant: variant)
         return Digest.sha3(cs_arrayValue(), variant: variant)
     }
     }
 
 
-    public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> UInt32 {
+    public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
         return Checksum.crc32(cs_arrayValue(), seed: seed, reflect: reflect)
         return Checksum.crc32(cs_arrayValue(), seed: seed, reflect: reflect)
     }
     }
-    
+
     public func crc16(seed: UInt16? = nil) -> UInt16 {
     public func crc16(seed: UInt16? = nil) -> UInt16 {
         return Checksum.crc16(cs_arrayValue(), seed: seed)
         return Checksum.crc16(cs_arrayValue(), seed: seed)
     }
     }
-    
+
     public func encrypt(cipher: Cipher) throws -> [Iterator.Element] {
     public func encrypt(cipher: Cipher) throws -> [Iterator.Element] {
         return try cipher.encrypt(cs_arrayValue())
         return try cipher.encrypt(cs_arrayValue())
     }
     }
@@ -72,7 +75,7 @@ public extension CSArrayType where Iterator.Element == UInt8 {
     public func decrypt(cipher: Cipher) throws -> [Iterator.Element] {
     public func decrypt(cipher: Cipher) throws -> [Iterator.Element] {
         return try cipher.decrypt(cs_arrayValue())
         return try cipher.decrypt(cs_arrayValue())
     }
     }
-    
+
     public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Iterator.Element] {
     public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Iterator.Element] {
         return try authenticator.authenticate(cs_arrayValue())
         return try authenticator.authenticate(cs_arrayValue())
     }
     }

+ 55 - 53
Sources/CryptoSwift/ChaCha20.swift

@@ -6,19 +6,19 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 //
 
 
-final public class ChaCha20: BlockCipher {
-    
+public final class ChaCha20: BlockCipher {
+
     public enum Error: Swift.Error {
     public enum Error: Swift.Error {
         case invalidKeyOrInitializationVector
         case invalidKeyOrInitializationVector
     }
     }
-    
+
     public static let blockSize = 64 // 512 / 8
     public static let blockSize = 64 // 512 / 8
     fileprivate let context: Context
     fileprivate let context: Context
 
 
     private struct Context {
     private struct Context {
         var input = Array<UInt32>(repeating: 0, count: 16)
         var input = Array<UInt32>(repeating: 0, count: 16)
 
 
-        init(key:Array<UInt8>, iv:Array<UInt8>) throws {
+        init(key: Array<UInt8>, iv: Array<UInt8>) throws {
             precondition(iv.count >= 8)
             precondition(iv.count >= 8)
 
 
             let kbits = key.count * 8
             let kbits = key.count * 8
@@ -28,98 +28,99 @@ final public class ChaCha20: BlockCipher {
             }
             }
 
 
             // 4 - 8
             // 4 - 8
-            for i in 0..<4 {
+            for i in 0 ..< 4 {
                 let start = i * 4
                 let start = i * 4
-                input[i + 4] = wordNumber(key[start..<(start + 4)])
+                input[i + 4] = wordNumber(key[start ..< (start + 4)])
             }
             }
 
 
-            var addPos = 0;
+            var addPos = 0
             switch (kbits) {
             switch (kbits) {
-                case 256:
-                    addPos += 16
-                    // sigma
-                    input[0] = 0x61707865 //apxe
-                    input[1] = 0x3320646e //3 dn
-                    input[2] = 0x79622d32 //yb-2
-                    input[3] = 0x6b206574 //k et
-                default:
-                    // tau
-                    input[0] = 0x61707865 //apxe
-                    input[1] = 0x3620646e //6 dn
-                    input[2] = 0x79622d31 //yb-1
-                    input[3] = 0x6b206574 //k et
+            case 256:
+                addPos += 16
+                // sigma
+                input[0] = 0x61707865 // apxe
+                input[1] = 0x3320646e // 3 dn
+                input[2] = 0x79622d32 // yb-2
+                input[3] = 0x6b206574 // k et
+            default:
+                // tau
+                input[0] = 0x61707865 // apxe
+                input[1] = 0x3620646e // 6 dn
+                input[2] = 0x79622d31 // yb-1
+                input[3] = 0x6b206574 // k et
             }
             }
 
 
             // 8 - 11
             // 8 - 11
-            for i in 0..<4 {
-                let start = addPos + (i*4)
+            for i in 0 ..< 4 {
+                let start = addPos + (i * 4)
 
 
-                let bytes = key[start..<(start + 4)]
+                let bytes = key[start ..< (start + 4)]
                 input[i + 8] = wordNumber(bytes)
                 input[i + 8] = wordNumber(bytes)
             }
             }
 
 
             // iv
             // iv
             input[12] = 0
             input[12] = 0
             input[13] = 0
             input[13] = 0
-            input[14] = wordNumber(iv[0..<4])
-            input[15] = wordNumber(iv[4..<8])
+            input[14] = wordNumber(iv[0 ..< 4])
+            input[15] = wordNumber(iv[4 ..< 8])
         }
         }
     }
     }
-    
-    public init(key:Array<UInt8>, iv:Array<UInt8>) throws {
+
+    public init(key: Array<UInt8>, iv: Array<UInt8>) throws {
         self.context = try Context(key: key, iv: iv)
         self.context = try Context(key: key, iv: iv)
     }
     }
-    
-    fileprivate func wordToByte(_ input:Array<UInt32> /* 64 */) -> Array<UInt8>? /* 16 */ {
+
+    fileprivate func wordToByte(_ input: Array<UInt32> /* 64 */ ) -> Array<UInt8>? /* 16 */ {
         precondition(input.count == 16)
         precondition(input.count == 16)
 
 
         var x = input
         var x = input
 
 
-        for _ in 0..<10 {
+        for _ in 0 ..< 10 {
             quarterround(&x[0], &x[4], &x[8], &x[12])
             quarterround(&x[0], &x[4], &x[8], &x[12])
-            quarterround(&x[1], &x[5], &x[9],  &x[13])
+            quarterround(&x[1], &x[5], &x[9], &x[13])
             quarterround(&x[2], &x[6], &x[10], &x[14])
             quarterround(&x[2], &x[6], &x[10], &x[14])
             quarterround(&x[3], &x[7], &x[11], &x[15])
             quarterround(&x[3], &x[7], &x[11], &x[15])
             quarterround(&x[0], &x[5], &x[10], &x[15])
             quarterround(&x[0], &x[5], &x[10], &x[15])
             quarterround(&x[1], &x[6], &x[11], &x[12])
             quarterround(&x[1], &x[6], &x[11], &x[12])
-            quarterround(&x[2], &x[7], &x[8],  &x[13])
-            quarterround(&x[3], &x[4], &x[9],  &x[14])
+            quarterround(&x[2], &x[7], &x[8], &x[13])
+            quarterround(&x[3], &x[4], &x[9], &x[14])
         }
         }
 
 
         var output = Array<UInt8>()
         var output = Array<UInt8>()
         output.reserveCapacity(16)
         output.reserveCapacity(16)
 
 
-        for i in 0..<16 {
+        for i in 0 ..< 16 {
             x[i] = x[i] &+ input[i]
             x[i] = x[i] &+ input[i]
             output.append(contentsOf: x[i].bytes().reversed())
             output.append(contentsOf: x[i].bytes().reversed())
         }
         }
 
 
-        return output;
+        return output
     }
     }
 
 
-    private final func quarterround(_ a:inout UInt32, _ b:inout UInt32, _ c:inout UInt32, _ d:inout UInt32) {
+    private final func quarterround(_ a: inout UInt32, _ b: inout UInt32, _ c: inout UInt32, _ d: inout UInt32) {
         a = a &+ b
         a = a &+ b
-        d = rotateLeft((d ^ a), by: 16) //FIXME: WAT? n:
-        
+        d = rotateLeft((d ^ a), by: 16) // FIXME: WAT? n:
+
         c = c &+ d
         c = c &+ d
-        b = rotateLeft((b ^ c), by: 12);
-        
+        b = rotateLeft((b ^ c), by: 12)
+
         a = a &+ b
         a = a &+ b
-        d = rotateLeft((d ^ a), by: 8);
+        d = rotateLeft((d ^ a), by: 8)
 
 
         c = c &+ d
         c = c &+ d
-        b = rotateLeft((b ^ c), by: 7);
+        b = rotateLeft((b ^ c), by: 7)
     }
     }
 }
 }
 
 
 // MARK: Cipher
 // MARK: Cipher
 extension ChaCha20: Cipher {
 extension ChaCha20: Cipher {
+
     public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
     public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
         var ctx = context
         var ctx = context
         var c = Array<UInt8>(repeating: 0, count: bytes.count)
         var c = Array<UInt8>(repeating: 0, count: bytes.count)
 
 
-        var cPos:Int = 0
-        var mPos:Int = 0
+        var cPos: Int = 0
+        var mPos: Int = 0
         var bytesCount = bytes.count
         var bytesCount = bytes.count
 
 
         while (true) {
         while (true) {
@@ -130,12 +131,12 @@ extension ChaCha20: Cipher {
                     /* stopping at 2^70 bytes per nonce is user's responsibility */
                     /* stopping at 2^70 bytes per nonce is user's responsibility */
                 }
                 }
                 if (bytesCount <= ChaCha20.blockSize) {
                 if (bytesCount <= ChaCha20.blockSize) {
-                    for i in 0..<bytesCount {
+                    for i in 0 ..< bytesCount {
                         c[i + cPos] = bytes[i + mPos] ^ output[i]
                         c[i + cPos] = bytes[i + mPos] ^ output[i]
                     }
                     }
                     return c
                     return c
                 }
                 }
-                for i in 0..<ChaCha20.blockSize {
+                for i in 0 ..< ChaCha20.blockSize {
                     c[i + cPos] = bytes[i + mPos] ^ output[i]
                     c[i + cPos] = bytes[i + mPos] ^ output[i]
                 }
                 }
                 bytesCount -= ChaCha20.blockSize
                 bytesCount -= ChaCha20.blockSize
@@ -152,6 +153,7 @@ extension ChaCha20: Cipher {
 
 
 // MARK: Encryptor
 // MARK: Encryptor
 extension ChaCha20 {
 extension ChaCha20 {
+
     public struct Encryptor: Updatable {
     public struct Encryptor: Updatable {
         private var accumulated = Array<UInt8>()
         private var accumulated = Array<UInt8>()
         private let chacha: ChaCha20
         private let chacha: ChaCha20
@@ -160,7 +162,7 @@ extension ChaCha20 {
             self.chacha = chacha
             self.chacha = chacha
         }
         }
 
 
-        mutating public func update<T: Sequence>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
+        mutating public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
             self.accumulated += bytes
             self.accumulated += bytes
 
 
             var encrypted = Array<UInt8>()
             var encrypted = Array<UInt8>()
@@ -178,6 +180,7 @@ extension ChaCha20 {
 
 
 // MARK: Decryptor
 // MARK: Decryptor
 extension ChaCha20 {
 extension ChaCha20 {
+
     public struct Decryptor: Updatable {
     public struct Decryptor: Updatable {
         private var accumulated = Array<UInt8>()
         private var accumulated = Array<UInt8>()
 
 
@@ -189,7 +192,7 @@ extension ChaCha20 {
             self.chacha = chacha
             self.chacha = chacha
         }
         }
 
 
-        mutating public func update<T: Sequence>(withBytes bytes:T, isLast: Bool = true) throws -> Array<UInt8> where T.Iterator.Element == 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
             // prepend "offset" number of bytes at the begining
             if self.offset > 0 {
             if self.offset > 0 {
                 self.accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes
                 self.accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes
@@ -207,7 +210,7 @@ extension ChaCha20 {
 
 
                     // remove "offset" from the beginning of first chunk
                     // remove "offset" from the beginning of first chunk
                     if self.offsetToRemove > 0 {
                     if self.offsetToRemove > 0 {
-                        plaintext.removeFirst(self.offsetToRemove);
+                        plaintext.removeFirst(self.offsetToRemove)
                         self.offsetToRemove = 0
                         self.offsetToRemove = 0
                     }
                     }
 
 
@@ -220,9 +223,9 @@ extension ChaCha20 {
     }
     }
 }
 }
 
 
-
 // MARK: Cryptors
 // MARK: Cryptors
 extension ChaCha20: Cryptors {
 extension ChaCha20: Cryptors {
+
     public func makeEncryptor() -> ChaCha20.Encryptor {
     public func makeEncryptor() -> ChaCha20.Encryptor {
         return Encryptor(chacha: self)
         return Encryptor(chacha: self)
     }
     }
@@ -236,12 +239,11 @@ extension ChaCha20: Cryptors {
 
 
 /// Change array to number. It's here because arrayOfBytes is too slow
 /// Change array to number. It's here because arrayOfBytes is too slow
 private func wordNumber<T: Collection>(_ bytes: T) -> UInt32 where T.Iterator.Element == UInt8, T.IndexDistance == Int {
 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 {
+    var value: UInt32 = 0
+    for i: UInt32 in 0 ..< 4 {
         let j = bytes.index(bytes.startIndex, offsetBy: Int(i))
         let j = bytes.index(bytes.startIndex, offsetBy: Int(i))
         value = value | UInt32(bytes[j]) << (8 * i)
         value = value | UInt32(bytes[j]) << (8 * i)
     }
     }
 
 
     return value
     return value
 }
 }
-

+ 74 - 75
Sources/CryptoSwift/Checksum.swift

@@ -8,74 +8,74 @@
 
 
 /// CRC - cyclic redundancy check code.
 /// CRC - cyclic redundancy check code.
 public final class Checksum {
 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,
-        0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
-        0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
-        0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
-        0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
-        0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
-        0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
-        0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
-        0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
-        0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
-        0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
-        0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
-        0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
-        0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
-        0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
-        0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
-        0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
-        0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
-        0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
-        0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
-        0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
-        0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
-        0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
-        0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
-        0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
-        0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
-        0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
-        0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
-        0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
-        0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d]
-    
-    private static let table16:[UInt16] = [0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
-        0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
-        0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
-        0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
-        0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
-        0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
-        0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
-        0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
-        0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
-        0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
-        0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
-        0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
-        0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
-        0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
-        0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
-        0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
-        0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
-        0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
-        0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
-        0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
-        0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
-        0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
-        0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
-        0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
-        0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
-        0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
-        0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
-        0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
-        0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
-        0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
-        0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
-        0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040]
-    
-    func crc32(_ message:Array<UInt8>, seed: UInt32? = nil, reflect : Bool = true) -> UInt32 {
-        var crc:UInt32 = seed != nil ? seed! : 0xffffffff
+    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,
+                                                0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
+                                                0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
+                                                0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+                                                0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
+                                                0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
+                                                0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+                                                0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
+                                                0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
+                                                0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+                                                0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
+                                                0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
+                                                0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+                                                0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
+                                                0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
+                                                0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+                                                0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
+                                                0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
+                                                0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+                                                0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
+                                                0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
+                                                0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+                                                0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
+                                                0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
+                                                0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+                                                0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
+                                                0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
+                                                0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+                                                0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
+                                                0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d]
+
+    private static let table16: [UInt16] = [0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
+                                           0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
+                                           0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
+                                           0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
+                                           0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
+                                           0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
+                                           0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
+                                           0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
+                                           0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
+                                           0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
+                                           0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
+                                           0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
+                                           0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
+                                           0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
+                                           0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
+                                           0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
+                                           0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
+                                           0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
+                                           0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
+                                           0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
+                                           0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
+                                           0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
+                                           0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
+                                           0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
+                                           0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
+                                           0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
+                                           0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
+                                           0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
+                                           0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
+                                           0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
+                                           0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
+                                           0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040]
+
+    func crc32(_ message: Array<UInt8>, seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
+        var crc: UInt32 = seed != nil ? seed! : 0xffffffff
         for chunk in BytesSequence(chunkSize: 256, data: message) {
         for chunk in BytesSequence(chunkSize: 256, data: message) {
             for b in chunk {
             for b in chunk {
                 let idx = Int((crc ^ UInt32(reflect ? b : reversed(b))) & 0xff)
                 let idx = Int((crc ^ UInt32(reflect ? b : reversed(b))) & 0xff)
@@ -84,9 +84,9 @@ public final class Checksum {
         }
         }
         return (reflect ? crc : reversed(crc)) ^ 0xffffffff
         return (reflect ? crc : reversed(crc)) ^ 0xffffffff
     }
     }
-    
-    func crc16(_ message:Array<UInt8>, seed: UInt16? = nil) -> UInt16 {
-        var crc:UInt16 = seed != nil ? seed! : 0x0000
+
+    func crc16(_ message: Array<UInt8>, seed: UInt16? = nil) -> UInt16 {
+        var crc: UInt16 = seed != nil ? seed! : 0x0000
         for chunk in BytesSequence(chunkSize: 256, data: message) {
         for chunk in BytesSequence(chunkSize: 256, data: message) {
             for b in chunk {
             for b in chunk {
                 crc = (crc >> 8) ^ Checksum.table16[Int((crc ^ UInt16(b)) & 0xFF)]
                 crc = (crc >> 8) ^ Checksum.table16[Int((crc ^ UInt16(b)) & 0xFF)]
@@ -96,7 +96,7 @@ public final class Checksum {
     }
     }
 }
 }
 
 
-//MARK: Public interface
+// MARK: Public interface
 public extension Checksum {
 public extension Checksum {
 
 
     /// Calculate CRC32
     /// Calculate CRC32
@@ -106,18 +106,17 @@ public extension Checksum {
     /// - parameter reflect: is reflect (default true)
     /// - parameter reflect: is reflect (default true)
     ///
     ///
     /// - returns: Calculated code
     /// - returns: Calculated code
-    static func crc32(_ message:Array<UInt8>, seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
+    static func crc32(_ message: Array<UInt8>, seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
         return Checksum().crc32(message, seed: seed, reflect: reflect)
         return Checksum().crc32(message, seed: seed, reflect: reflect)
     }
     }
 
 
-
     /// Calculate CRC16
     /// Calculate CRC16
     ///
     ///
     /// - parameter message: Message
     /// - parameter message: Message
     /// - parameter seed:    Seed value (Optional)
     /// - parameter seed:    Seed value (Optional)
     ///
     ///
     /// - returns: Calculated code
     /// - returns: Calculated code
-    static func crc16(_ message:Array<UInt8>, seed: UInt16? = nil) -> UInt16 {
+    static func crc16(_ message: Array<UInt8>, seed: UInt16? = nil) -> UInt16 {
         return Checksum().crc16(message, seed: seed)
         return Checksum().crc16(message, seed: seed)
     }
     }
 }
 }

+ 6 - 5
Sources/CryptoSwift/Collection+Extension.swift

@@ -7,6 +7,7 @@
 //
 //
 
 
 extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
 extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
+
     func toUInt32Array() -> Array<UInt32> {
     func toUInt32Array() -> Array<UInt32> {
         var result = Array<UInt32>()
         var result = Array<UInt32>()
         result.reserveCapacity(16)
         result.reserveCapacity(16)
@@ -14,7 +15,7 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
             var val: UInt32 = 0
             var val: UInt32 = 0
             val |= self.count > 3 ? UInt32(self[idx.advanced(by: 3)]) << 24 : 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 > 2 ? UInt32(self[idx.advanced(by: 2)]) << 16 : 0
-            val |= self.count > 1 ? UInt32(self[idx.advanced(by: 1)]) << 8  : 0
+            val |= self.count > 1 ? UInt32(self[idx.advanced(by: 1)]) << 8 : 0
             val |= self.count > 0 ? UInt32(self[idx]) : 0
             val |= self.count > 0 ? UInt32(self[idx]) : 0
             result.append(val)
             result.append(val)
         }
         }
@@ -26,7 +27,7 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
         var result = Array<UInt64>()
         var result = Array<UInt64>()
         result.reserveCapacity(32)
         result.reserveCapacity(32)
         for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt64>.size) {
         for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt64>.size) {
-            var val:UInt64 = 0
+            var val: UInt64 = 0
             val |= self.count > 7 ? UInt64(self[idx.advanced(by: 7)]) << 56 : 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 > 6 ? UInt64(self[idx.advanced(by: 6)]) << 48 : 0
             val |= self.count > 5 ? UInt64(self[idx.advanced(by: 5)]) << 40 : 0
             val |= self.count > 5 ? UInt64(self[idx.advanced(by: 5)]) << 40 : 0
@@ -43,13 +44,13 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
 
 
     /// Initialize integer from array of bytes. Caution: may be slow!
     /// Initialize integer from array of bytes. Caution: may be slow!
     @available(*, deprecated: 0.6.0, message: "Dont use it. Too generic to be fast")
     @available(*, deprecated: 0.6.0, message: "Dont use it. Too generic to be fast")
-    func toInteger<T:Integer>() -> T where T: ByteConvertible, T: BitshiftOperationsType {
+    func toInteger<T: Integer>() -> T where T: ByteConvertible, T: BitshiftOperationsType {
         if self.count == 0 {
         if self.count == 0 {
-            return 0;
+            return 0
         }
         }
 
 
         let size = MemoryLayout<T>.size
         let size = MemoryLayout<T>.size
-        var bytes = self.reversed() //FIXME: check it this is equivalent of Array(...)
+        var bytes = self.reversed() // FIXME: check it this is equivalent of Array(...)
         if bytes.count < size {
         if bytes.count < size {
             let paddingCount = size - bytes.count
             let paddingCount = size - bytes.count
             if (paddingCount > 0) {
             if (paddingCount > 0) {

+ 4 - 3
Sources/CryptoSwift/Cryptors.swift

@@ -24,12 +24,13 @@ public protocol Cryptors: class {
     func makeDecryptor() -> DecryptorType
     func makeDecryptor() -> DecryptorType
 
 
     /// Generate array of random bytes. Helper function.
     /// Generate array of random bytes. Helper function.
-    static func randomIV(_ blockSize:Int) -> Array<UInt8>
+    static func randomIV(_ blockSize: Int) -> Array<UInt8>
 }
 }
 
 
 extension Cryptors {
 extension Cryptors {
-    static public func randomIV(_ blockSize:Int) -> Array<UInt8> {
-        var randomIV:Array<UInt8> = Array<UInt8>()
+
+    public static func randomIV(_ blockSize: Int) -> Array<UInt8> {
+        var randomIV: Array<UInt8> = Array<UInt8>()
         randomIV.reserveCapacity(blockSize)
         randomIV.reserveCapacity(blockSize)
         for randomByte in RandomBytesSequence(size: blockSize) {
         for randomByte in RandomBytesSequence(size: blockSize) {
             randomIV.append(randomByte)
             randomIV.append(randomByte)

+ 1 - 2
Sources/CryptoSwift/Digest.swift

@@ -6,7 +6,7 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 //
 
 
-@available(*, deprecated:0.6.0, renamed: "Digest")
+@available(*, deprecated: 0.6.0, renamed: "Digest")
 public typealias Hash = Digest
 public typealias Hash = Digest
 
 
 /// Hash functions to calculate Digest.
 /// Hash functions to calculate Digest.
@@ -61,5 +61,4 @@ public struct Digest {
     public static func sha3(_ bytes: Array<UInt8>, variant: SHA3.Variant) -> Array<UInt8> {
     public static func sha3(_ bytes: Array<UInt8>, variant: SHA3.Variant) -> Array<UInt8> {
         return SHA3(variant: variant).calculate(for: bytes)
         return SHA3(variant: variant).calculate(for: bytes)
     }
     }
-
 }
 }

+ 3 - 2
Sources/CryptoSwift/Foundation/AES+Foundation.swift

@@ -9,11 +9,12 @@
 import Foundation
 import Foundation
 
 
 extension AES {
 extension AES {
-    convenience public init(key:String, iv:String, blockMode:BlockMode = .CBC, padding: Padding = PKCS7()) throws {
+
+    public convenience init(key: String, iv: String, blockMode: BlockMode = .CBC, padding: Padding = PKCS7()) throws {
         guard let kkey = key.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes, let iiv = iv.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes else {
         guard let kkey = key.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes, let iiv = iv.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes else {
             throw Error.invalidKeyOrInitializationVector
             throw Error.invalidKeyOrInitializationVector
         }
         }
-        
+
         try self.init(key: kkey, iv: iiv, blockMode: blockMode, padding: padding)
         try self.init(key: kkey, iv: iiv, blockMode: blockMode, padding: padding)
     }
     }
 }
 }

+ 1 - 0
Sources/CryptoSwift/Foundation/CSArrayType+Foundation.swift

@@ -9,6 +9,7 @@
 import Foundation
 import Foundation
 
 
 public extension CSArrayType where Iterator.Element == UInt8 {
 public extension CSArrayType where Iterator.Element == UInt8 {
+
     public func toBase64() -> String? {
     public func toBase64() -> String? {
         guard let bytesArray = self as? Array<UInt8> else {
         guard let bytesArray = self as? Array<UInt8> else {
             return nil
             return nil

+ 2 - 1
Sources/CryptoSwift/Foundation/ChaCha20+Foundation.swift

@@ -9,7 +9,8 @@
 import Foundation
 import Foundation
 
 
 extension ChaCha20 {
 extension ChaCha20 {
-    convenience public init(key:String, iv:String) throws {
+
+    public convenience init(key: String, iv: String) throws {
         guard let kkey = key.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes, let iiv = iv.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes else {
         guard let kkey = key.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes, let iiv = iv.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes else {
             throw Error.invalidKeyOrInitializationVector
             throw Error.invalidKeyOrInitializationVector
         }
         }

+ 6 - 7
Sources/CryptoSwift/Foundation/Data+Extension.swift

@@ -12,15 +12,15 @@ extension Data {
 
 
     /// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 65536
     /// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 65536
     public func checksum() -> UInt16 {
     public func checksum() -> UInt16 {
-        var s:UInt32 = 0
+        var s: UInt32 = 0
         var bytesArray = self.bytes
         var bytesArray = self.bytes
-        for i in 0..<bytesArray.count {
+        for i in 0 ..< bytesArray.count {
             s = s + UInt32(bytesArray[i])
             s = s + UInt32(bytesArray[i])
         }
         }
         s = s % 65536
         s = s % 65536
         return UInt16(s)
         return UInt16(s)
     }
     }
-    
+
     public func md5() -> Data {
     public func md5() -> Data {
         let result = Digest.md5(self.bytes)
         let result = Digest.md5(self.bytes)
         return Data(bytes: result)
         return Data(bytes: result)
@@ -50,7 +50,7 @@ extension Data {
         return Data(bytes: Digest.sha3(self.bytes, variant: variant))
         return Data(bytes: Digest.sha3(self.bytes, variant: variant))
     }
     }
 
 
-    public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> Data {
+    public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> Data {
         return Data(bytes: Checksum.crc32(self.bytes, seed: seed, reflect: reflect).bytes())
         return Data(bytes: Checksum.crc32(self.bytes, seed: seed, reflect: reflect).bytes())
     }
     }
 
 
@@ -65,7 +65,7 @@ extension Data {
     public func decrypt(cipher: Cipher) throws -> Data {
     public func decrypt(cipher: Cipher) throws -> Data {
         return Data(bytes: try cipher.decrypt(self.bytes))
         return Data(bytes: try cipher.decrypt(self.bytes))
     }
     }
-    
+
     public func authenticate(with authenticator: Authenticator) throws -> Data {
     public func authenticate(with authenticator: Authenticator) throws -> Data {
         return Data(bytes: try authenticator.authenticate(self.bytes))
         return Data(bytes: try authenticator.authenticate(self.bytes))
     }
     }
@@ -76,9 +76,8 @@ extension Data {
     public var bytes: Array<UInt8> {
     public var bytes: Array<UInt8> {
         return Array(self)
         return Array(self)
     }
     }
-    
+
     public func toHexString() -> String {
     public func toHexString() -> String {
         return self.bytes.toHexString()
         return self.bytes.toHexString()
     }
     }
 }
 }
-

+ 3 - 2
Sources/CryptoSwift/Foundation/HMAC+Foundation.swift

@@ -9,11 +9,12 @@
 import Foundation
 import Foundation
 
 
 extension HMAC {
 extension HMAC {
-    convenience public init (key: String, variant:HMAC.Variant = .md5) throws {
+
+    public convenience init(key: String, variant: HMAC.Variant = .md5) throws {
         guard let kkey = key.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes else {
         guard let kkey = key.data(using: String.Encoding.utf8, allowLossyConversion: false)?.bytes else {
             throw Error.invalidInput
             throw Error.invalidInput
         }
         }
-        
+
         self.init(key: kkey, variant: variant)
         self.init(key: kkey, variant: variant)
     }
     }
 }
 }

+ 5 - 4
Sources/CryptoSwift/Foundation/Rabbit+Foundation.swift

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

+ 0 - 1
Sources/CryptoSwift/Foundation/String+FoundationExtension.swift

@@ -32,5 +32,4 @@ extension String {
 
 
         return try decodedData.decrypt(cipher: cipher).bytes
         return try decodedData.decrypt(cipher: cipher).bytes
     }
     }
-
 }
 }

+ 7 - 7
Sources/CryptoSwift/Foundation/Utils+Foundation.swift

@@ -8,13 +8,13 @@
 
 
 import Foundation
 import Foundation
 
 
-func perf(_ text: String, closure: () -> ()) {
-    let measurementStart = Date();
-    
+func perf(_ text: String, closure: () -> Void) {
+    let measurementStart = Date()
+
     closure()
     closure()
-    
-    let measurementStop = Date();
+
+    let measurementStop = Date()
     let executionTime = measurementStop.timeIntervalSince(measurementStart)
     let executionTime = measurementStop.timeIntervalSince(measurementStart)
-    
-    print("\(text) \(executionTime)");
+
+    print("\(text) \(executionTime)")
 }
 }

+ 21 - 23
Sources/CryptoSwift/Generics.swift

@@ -7,23 +7,22 @@
 //
 //
 
 
 /** Protocol and extensions for integerFrom(bits:). Bit hakish for me, but I can't do it in any other way */
 /** Protocol and extensions for integerFrom(bits:). Bit hakish for me, but I can't do it in any other way */
-protocol Initiable  {
+protocol Initiable {
     init(_ v: Int)
     init(_ v: Int)
     init(_ v: UInt)
     init(_ v: UInt)
 }
 }
 
 
-extension Int:Initiable {}
-extension UInt:Initiable {}
-extension UInt8:Initiable {}
-extension UInt16:Initiable {}
-extension UInt32:Initiable {}
-extension UInt64:Initiable {}
+extension Int: Initiable {}
+extension UInt: Initiable {}
+extension UInt8: Initiable {}
+extension UInt16: Initiable {}
+extension UInt32: Initiable {}
+extension UInt64: Initiable {}
 
 
 /** build bit pattern from array of bits */
 /** build bit pattern from array of bits */
 @_specialize(UInt8)
 @_specialize(UInt8)
-func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T
-{
-    var bitPattern:T = 0
+func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T {
+    var bitPattern: T = 0
     for idx in bits.indices {
     for idx in bits.indices {
         if bits[idx] == Bit.one {
         if bits[idx] == Bit.one {
             let bit = T(UIntMax(1) << UIntMax(idx))
             let bit = T(UIntMax(1) << UIntMax(idx))
@@ -39,19 +38,19 @@ func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T
 /// - parameter length: length of output array. By default size of value type
 /// - parameter length: length of output array. By default size of value type
 ///
 ///
 /// - returns: Array of bytes
 /// - returns: Array of bytes
-func arrayOfBytes<T: Integer>(value:T, length totalBytes: Int = MemoryLayout<T>.size) -> Array<UInt8> {
+func arrayOfBytes<T: Integer>(value: T, length totalBytes: Int = MemoryLayout<T>.size) -> Array<UInt8> {
     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
     valuePointer.pointee = value
     valuePointer.pointee = value
 
 
     let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
     let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
     var bytes = Array<UInt8>(repeating: 0, count: totalBytes)
     var bytes = Array<UInt8>(repeating: 0, count: totalBytes)
-    for j in 0..<min(MemoryLayout<T>.size,totalBytes) {
+    for j in 0 ..< min(MemoryLayout<T>.size, totalBytes) {
         bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
         bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
     }
     }
-    
+
     valuePointer.deinitialize()
     valuePointer.deinitialize()
     valuePointer.deallocate(capacity: 1)
     valuePointer.deallocate(capacity: 1)
-    
+
     return bytes
     return bytes
 }
 }
 
 
@@ -59,14 +58,14 @@ func arrayOfBytes<T: Integer>(value:T, length totalBytes: Int = MemoryLayout<T>.
 
 
 // helper to be able to make shift operation on T
 // helper to be able to make shift operation on T
 @_specialize(Int)
 @_specialize(Int)
-func << <T:SignedInteger>(lhs: T, rhs: Int) -> Int {
+func << <T: SignedInteger>(lhs: T, rhs: Int) -> Int {
     let a = lhs as! Int
     let a = lhs as! Int
     let b = rhs
     let b = rhs
     return a << b
     return a << b
 }
 }
 
 
 @_specialize(UInt)
 @_specialize(UInt)
-func << <T:UnsignedInteger>(lhs: T, rhs: Int) -> UInt {
+func << <T: UnsignedInteger>(lhs: T, rhs: Int) -> UInt {
     let a = lhs as! UInt
     let a = lhs as! UInt
     let b = rhs
     let b = rhs
     return a << b
     return a << b
@@ -77,20 +76,20 @@ func << <T:UnsignedInteger>(lhs: T, rhs: Int) -> UInt {
 @_specialize(Int)
 @_specialize(Int)
 func shiftLeft<T: SignedInteger>(_ value: T, by count: Int) -> T where T: Initiable {
 func shiftLeft<T: SignedInteger>(_ value: T, by count: Int) -> T where T: Initiable {
     if (value == 0) {
     if (value == 0) {
-        return 0;
+        return 0
     }
     }
-    
+
     let bitsCount = (MemoryLayout<T>.size * 8)
     let bitsCount = (MemoryLayout<T>.size * 8)
     let shiftCount = Int(Swift.min(count, bitsCount - 1))
     let shiftCount = Int(Swift.min(count, bitsCount - 1))
-    
-    var shiftedValue:T = 0;
-    for bitIdx in 0..<bitsCount {
+
+    var shiftedValue: T = 0
+    for bitIdx in 0 ..< bitsCount {
         let bit = T(IntMax(1 << bitIdx))
         let bit = T(IntMax(1 << bitIdx))
         if ((value & bit) == bit) {
         if ((value & bit) == bit) {
             shiftedValue = shiftedValue | T(bit << shiftCount)
             shiftedValue = shiftedValue | T(bit << shiftCount)
         }
         }
     }
     }
-    
+
     if (shiftedValue != 0 && count >= bitsCount) {
     if (shiftedValue != 0 && count >= bitsCount) {
         // clear last bit that couldn't be shifted out of range
         // clear last bit that couldn't be shifted out of range
         shiftedValue = shiftedValue & T(~(1 << (bitsCount - 1)))
         shiftedValue = shiftedValue & T(~(1 << (bitsCount - 1)))
@@ -134,4 +133,3 @@ func shiftLeft(_ value: Int32, by count: Int) -> Int32 {
 func shiftLeft(_ value: Int64, by count: Int) -> Int64 {
 func shiftLeft(_ value: Int64, by count: Int) -> Int64 {
     return Int64(shiftLeft(Int(value), by: count))
     return Int64(shiftLeft(Int(value), by: count))
 }
 }
-

+ 13 - 14
Sources/CryptoSwift/HMAC.swift

@@ -6,17 +6,17 @@
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
 //
 
 
-final public class HMAC: Authenticator {
+public final class HMAC: Authenticator {
 
 
     public enum Error: Swift.Error {
     public enum Error: Swift.Error {
         case authenticateError
         case authenticateError
         case invalidInput
         case invalidInput
     }
     }
-    
+
     public enum Variant {
     public enum Variant {
         case sha1, sha256, sha384, sha512, md5
         case sha1, sha256, sha384, sha512, md5
-        
-        var digestLength:Int {
+
+        var digestLength: Int {
             switch (self) {
             switch (self) {
             case .sha1:
             case .sha1:
                 return SHA1.digestLength
                 return SHA1.digestLength
@@ -31,7 +31,7 @@ final public class HMAC: Authenticator {
             }
             }
         }
         }
 
 
-        func calculateHash(_ bytes:Array<UInt8>) -> Array<UInt8>? {
+        func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8>? {
             switch (self) {
             switch (self) {
             case .sha1:
             case .sha1:
                 return Digest.sha1(bytes)
                 return Digest.sha1(bytes)
@@ -45,7 +45,7 @@ final public class HMAC: Authenticator {
                 return Digest.md5(bytes)
                 return Digest.md5(bytes)
             }
             }
         }
         }
-        
+
         func blockSize() -> Int {
         func blockSize() -> Int {
             switch self {
             switch self {
             case .md5:
             case .md5:
@@ -57,11 +57,11 @@ final public class HMAC: Authenticator {
             }
             }
         }
         }
     }
     }
-    
-    var key:Array<UInt8>
-    let variant:Variant
 
 
-    public init (key: Array<UInt8>, variant:HMAC.Variant = .md5) {
+    var key: Array<UInt8>
+    let variant: Variant
+
+    public init(key: Array<UInt8>, variant: HMAC.Variant = .md5) {
         self.variant = variant
         self.variant = variant
         self.key = key
         self.key = key
 
 
@@ -76,9 +76,9 @@ final public class HMAC: Authenticator {
         }
         }
     }
     }
 
 
-    //MARK: Authenticator
+    // MARK: Authenticator
 
 
-    public func authenticate(_ bytes:Array<UInt8>) throws -> Array<UInt8> {
+    public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
         var opad = Array<UInt8>(repeating: 0x5c, count: variant.blockSize())
         var opad = Array<UInt8>(repeating: 0x5c, count: variant.blockSize())
         for idx in key.indices {
         for idx in key.indices {
             opad[idx] = key[idx] ^ opad[idx]
             opad[idx] = key[idx] ^ opad[idx]
@@ -89,8 +89,7 @@ final public class HMAC: Authenticator {
         }
         }
 
 
         guard let ipadAndMessageHash = variant.calculateHash(ipad + bytes),
         guard let ipadAndMessageHash = variant.calculateHash(ipad + bytes),
-              let result = variant.calculateHash(opad + ipadAndMessageHash) else
-        {
+            let result = variant.calculateHash(opad + ipadAndMessageHash) else {
             throw Error.authenticateError
             throw Error.authenticateError
         }
         }
 
 

+ 5 - 4
Sources/CryptoSwift/Int+Extension.swift

@@ -20,9 +20,9 @@
     import Darwin
     import Darwin
 #endif
 #endif
 
 
-
 /* array of bits */
 /* array of bits */
 extension Int {
 extension Int {
+
     init(bits: [Bit]) {
     init(bits: [Bit]) {
         self.init(bitPattern: integerFrom(bits) as UInt)
         self.init(bitPattern: integerFrom(bits) as UInt)
     }
     }
@@ -30,13 +30,14 @@ extension Int {
 
 
 /* array of bytes */
 /* array of bytes */
 extension Int {
 extension Int {
+
     /** Int with collection of bytes (little-endian) */
     /** Int with collection of bytes (little-endian) */
-    //init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+    // init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
     //    self = bytes.toInteger()
     //    self = bytes.toInteger()
-    //}
+    // }
 
 
     /** Array of bytes with optional padding */
     /** Array of bytes with optional padding */
     func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
     func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
-         return arrayOfBytes(value: self, length: totalBytes)
+        return arrayOfBytes(value: self, length: totalBytes)
     }
     }
 }
 }

+ 15 - 10
Sources/CryptoSwift/IntegerConvertible.swift

@@ -18,20 +18,25 @@ protocol ByteConvertible {
     init(truncatingBitPattern: UInt64)
     init(truncatingBitPattern: UInt64)
 }
 }
 
 
-extension Int    : BitshiftOperationsType, ByteConvertible { }
-extension Int8   : BitshiftOperationsType, ByteConvertible { }
-extension Int16  : BitshiftOperationsType, ByteConvertible { }
-extension Int32  : BitshiftOperationsType, ByteConvertible { }
-extension Int64  : BitshiftOperationsType, ByteConvertible {
+extension Int: BitshiftOperationsType, ByteConvertible {}
+extension Int8: BitshiftOperationsType, ByteConvertible {}
+extension Int16: BitshiftOperationsType, ByteConvertible {}
+extension Int32: BitshiftOperationsType, ByteConvertible {}
+
+extension Int64: BitshiftOperationsType, ByteConvertible {
+
     init(truncatingBitPattern value: UInt64) {
     init(truncatingBitPattern value: UInt64) {
         self = Int64(bitPattern: value)
         self = Int64(bitPattern: value)
     }
     }
 }
 }
-extension UInt   : BitshiftOperationsType, ByteConvertible { }
-extension UInt8  : BitshiftOperationsType, ByteConvertible { }
-extension UInt16 : BitshiftOperationsType, ByteConvertible { }
-extension UInt32 : BitshiftOperationsType, ByteConvertible { }
-extension UInt64 : BitshiftOperationsType, ByteConvertible {
+
+extension UInt: BitshiftOperationsType, ByteConvertible {}
+extension UInt8: BitshiftOperationsType, ByteConvertible {}
+extension UInt16: BitshiftOperationsType, ByteConvertible {}
+extension UInt32: BitshiftOperationsType, ByteConvertible {}
+
+extension UInt64: BitshiftOperationsType, ByteConvertible {
+
     init(truncatingBitPattern value: UInt64) {
     init(truncatingBitPattern value: UInt64) {
         self = value
         self = value
     }
     }

+ 38 - 39
Sources/CryptoSwift/MD5.swift

@@ -6,41 +6,40 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 //
 
 
-public final class MD5: DigestType  {
-    static let blockSize:Int = 64
-    static let digestLength:Int = 16 // 128 / 8
-    fileprivate static let hashInitialValue:Array<UInt32> = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
+public final class MD5: DigestType {
+    static let blockSize: Int = 64
+    static let digestLength: Int = 16 // 128 / 8
+    fileprivate static let hashInitialValue: Array<UInt32> = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]
 
 
     fileprivate var accumulated = Array<UInt8>()
     fileprivate var accumulated = Array<UInt8>()
     fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var processedBytesTotalCount: Int = 0
-    fileprivate var accumulatedHash:Array<UInt32> = MD5.hashInitialValue
+    fileprivate var accumulatedHash: Array<UInt32> = MD5.hashInitialValue
 
 
     /** specifies the per-round shift amounts */
     /** specifies the per-round shift amounts */
-    private let s: Array<UInt32> = [7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,
-                       5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,
-                       4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,
-                       6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21]
-    
+    private let s: Array<UInt32> = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
+                                    5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
+                                    4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
+                                    6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
+
     /** binary integer part of the sines of integers (Radians) */
     /** binary integer part of the sines of integers (Radians) */
-    private let k: Array<UInt32> = [0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
-                       0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
-                       0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
-                       0x6b901122,0xfd987193,0xa679438e,0x49b40821,
-                       0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
-                       0xd62f105d,0x2441453,0xd8a1e681,0xe7d3fbc8,
-                       0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
-                       0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
-                       0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
-                       0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
-                       0x289b7ec6,0xeaa127fa,0xd4ef3085,0x4881d05,
-                       0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
-                       0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
-                       0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
-                       0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
-                       0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391]
+    private let k: Array<UInt32> = [0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
+                                    0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
+                                    0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
+                                    0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
+                                    0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
+                                    0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
+                                    0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
+                                    0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
+                                    0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
+                                    0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
+                                    0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
+                                    0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
+                                    0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
+                                    0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
+                                    0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
+                                    0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391]
 
 
     public init() {
     public init() {
-        
     }
     }
 
 
     public func calculate(for bytes: Array<UInt8>) -> Array<UInt8> {
     public func calculate(for bytes: Array<UInt8>) -> Array<UInt8> {
@@ -59,32 +58,32 @@ public final class MD5: DigestType  {
         assert(M.count == 16, "Invalid array")
         assert(M.count == 16, "Invalid array")
 
 
         // Initialize hash value for this chunk:
         // Initialize hash value for this chunk:
-        var A:UInt32 = currentHash[0]
-        var B:UInt32 = currentHash[1]
-        var C:UInt32 = currentHash[2]
-        var D:UInt32 = currentHash[3]
+        var A: UInt32 = currentHash[0]
+        var B: UInt32 = currentHash[1]
+        var C: UInt32 = currentHash[2]
+        var D: UInt32 = currentHash[3]
 
 
-        var dTemp:UInt32 = 0
+        var dTemp: UInt32 = 0
 
 
         // Main loop
         // Main loop
-        for j in 0..<k.count {
+        for j in 0 ..< k.count {
             var g = 0
             var g = 0
-            var F:UInt32 = 0
+            var F: UInt32 = 0
 
 
             switch (j) {
             switch (j) {
-            case 0...15:
+            case 0 ... 15:
                 F = (B & C) | ((~B) & D)
                 F = (B & C) | ((~B) & D)
                 g = j
                 g = j
                 break
                 break
-            case 16...31:
+            case 16 ... 31:
                 F = (D & B) | (~D & C)
                 F = (D & B) | (~D & C)
                 g = (5 * j + 1) % 16
                 g = (5 * j + 1) % 16
                 break
                 break
-            case 32...47:
+            case 32 ... 47:
                 F = B ^ C ^ D
                 F = B ^ C ^ D
                 g = (3 * j + 5) % 16
                 g = (3 * j + 5) % 16
                 break
                 break
-            case 48...63:
+            case 48 ... 63:
                 F = C ^ (B | (~D))
                 F = C ^ (B | (~D))
                 g = (7 * j) % 16
                 g = (7 * j) % 16
                 break
                 break
@@ -106,6 +105,7 @@ public final class MD5: DigestType  {
 }
 }
 
 
 extension MD5: Updatable {
 extension MD5: Updatable {
+
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
         self.accumulated += bytes
         self.accumulated += bytes
 
 
@@ -147,4 +147,3 @@ extension MD5: Updatable {
         return result
         return result
     }
     }
 }
 }
-

+ 6 - 6
Sources/CryptoSwift/NoPadding.swift

@@ -7,15 +7,15 @@
 //
 //
 
 
 public struct NoPadding: Padding {
 public struct NoPadding: Padding {
+
     public init() {
     public init() {
-    
     }
     }
-    
-    public func add(to data: Array<UInt8>, blockSize:Int) -> Array<UInt8> {
-        return data;
+
+    public func add(to data: Array<UInt8>, blockSize: Int) -> Array<UInt8> {
+        return data
     }
     }
 
 
-    public func remove(from data: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
-        return data;
+    public func remove(from data: Array<UInt8>, blockSize: Int?) -> Array<UInt8> {
+        return data
     }
     }
 }
 }

+ 12 - 12
Sources/CryptoSwift/Operators.swift

@@ -6,19 +6,19 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 //
 /*
 /*
-Bit shifting with overflow protection using overflow operator "&".
-Approach is consistent with standard overflow operators &+, &-, &*, &/
-and introduce new overflow operators for shifting: &<<, &>>
+ Bit shifting with overflow protection using overflow operator "&".
+ Approach is consistent with standard overflow operators &+, &-, &*, &/
+ and introduce new overflow operators for shifting: &<<, &>>
 
 
-Note: Works with unsigned integers values only
+ Note: Works with unsigned integers values only
 
 
-Usage
+ Usage
 
 
-var i = 1       // init
-var j = i &<< 2 //shift left
-j &<<= 2        //shift left and assign
+ var i = 1       // init
+ var j = i &<< 2 //shift left
+ j &<<= 2        //shift left and assign
 
 
-@see: https://medium.com/@krzyzanowskim/swiftly-shift-bits-and-protect-yourself-be33016ce071
- 
-This fuctonality is now implemented as part of Swift 3, SE-0104 https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md
-*/
+ @see: https://medium.com/@krzyzanowskim/swiftly-shift-bits-and-protect-yourself-be33016ce071
+
+ This fuctonality is now implemented as part of Swift 3, SE-0104 https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md
+ */

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

@@ -23,7 +23,7 @@ public extension PKCS5 {
         public enum Variant {
         public enum Variant {
             case md5, sha1
             case md5, sha1
 
 
-            var size:Int {
+            var size: Int {
                 switch (self) {
                 switch (self) {
                 case .md5:
                 case .md5:
                     return MD5.digestLength
                     return MD5.digestLength
@@ -32,7 +32,7 @@ public extension PKCS5 {
                 }
                 }
             }
             }
 
 
-            fileprivate func calculateHash(_ bytes:Array<UInt8>) -> Array<UInt8>? {
+            fileprivate func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8>? {
                 switch (self) {
                 switch (self) {
                 case .sha1:
                 case .sha1:
                     return Digest.sha1(bytes)
                     return Digest.sha1(bytes)
@@ -52,10 +52,10 @@ public extension PKCS5 {
         ///   - variant: hash variant
         ///   - variant: hash variant
         ///   - iterations: iteration count, a positive integer
         ///   - iterations: iteration count, a positive integer
         ///   - keyLength: intended length of derived key
         ///   - keyLength: intended length of derived key
-        public init(password: Array<UInt8>, salt: Array<UInt8>, variant: Variant = .sha1, iterations: Int = 4096 /* c */, keyLength: Int? = nil /* dkLen */) throws {
+        public init(password: Array<UInt8>, salt: Array<UInt8>, variant: Variant = .sha1, iterations: Int = 4096 /* c */ , keyLength: Int? = nil /* dkLen */ ) throws {
             precondition(iterations > 0)
             precondition(iterations > 0)
             precondition(salt.count == 8)
             precondition(salt.count == 8)
-            
+
             let keyLength = keyLength ?? variant.size
             let keyLength = keyLength ?? variant.size
 
 
             if (keyLength > variant.size) {
             if (keyLength > variant.size) {
@@ -75,10 +75,10 @@ public extension PKCS5 {
         /// Apply the underlying hash function Hash for c iterations
         /// Apply the underlying hash function Hash for c iterations
         public func calculate() -> Array<UInt8> {
         public func calculate() -> Array<UInt8> {
             var t = t1
             var t = t1
-            for _ in 2...self.iterations {
+            for _ in 2 ... self.iterations {
                 t = self.variant.calculateHash(t)!
                 t = self.variant.calculateHash(t)!
             }
             }
-            return Array(t[0..<self.keyLength])
+            return Array(t[0 ..< self.keyLength])
         }
         }
     }
     }
 }
 }

+ 11 - 9
Sources/CryptoSwift/PKCS5/PBKDF2.swift

@@ -15,6 +15,7 @@
 #endif
 #endif
 
 
 public extension PKCS5 {
 public extension PKCS5 {
+
     /// A key derivation function.
     /// A key derivation function.
     ///
     ///
     /// PBKDF2 - Password-Based Key Derivation Function 2. Key stretching technique.
     /// PBKDF2 - Password-Based Key Derivation Function 2. Key stretching technique.
@@ -26,10 +27,10 @@ public extension PKCS5 {
             case derivedKeyTooLong
             case derivedKeyTooLong
         }
         }
 
 
-        private let salt: Array<UInt8>   // S
+        private let salt: Array<UInt8> // S
         fileprivate let iterations: Int // c
         fileprivate let iterations: Int // c
         private let numBlocks: Int // l
         private let numBlocks: Int // l
-        private let dkLen: Int;
+        private let dkLen: Int
         fileprivate let prf: HMAC
         fileprivate let prf: HMAC
 
 
         /// - parameters:
         /// - parameters:
@@ -37,11 +38,11 @@ public extension PKCS5 {
         ///   - variant: hash variant
         ///   - variant: hash variant
         ///   - iterations: iteration count, a positive integer
         ///   - iterations: iteration count, a positive integer
         ///   - keyLength: intended length of derived key
         ///   - 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 {
+        public init(password: Array<UInt8>, salt: Array<UInt8>, iterations: Int = 4096 /* c */ , keyLength: Int? = nil /* dkLen */ , variant: HMAC.Variant = .sha256) throws {
             precondition(iterations > 0)
             precondition(iterations > 0)
 
 
             let prf = HMAC(key: password, variant: variant)
             let prf = HMAC(key: password, variant: variant)
-            
+
             guard iterations > 0 && !password.isEmpty && !salt.isEmpty else {
             guard iterations > 0 && !password.isEmpty && !salt.isEmpty else {
                 throw Error.invalidInput
                 throw Error.invalidInput
             }
             }
@@ -49,7 +50,7 @@ public extension PKCS5 {
             self.dkLen = keyLength ?? variant.digestLength
             self.dkLen = keyLength ?? variant.digestLength
             let keyLengthFinal = Double(self.dkLen)
             let keyLengthFinal = Double(self.dkLen)
             let hLen = Double(prf.variant.digestLength)
             let hLen = Double(prf.variant.digestLength)
-            if keyLengthFinal > (pow(2,32) - 1) * hLen {
+            if keyLengthFinal > (pow(2, 32) - 1) * hLen {
                 throw Error.derivedKeyTooLong
                 throw Error.derivedKeyTooLong
             }
             }
 
 
@@ -57,13 +58,13 @@ public extension PKCS5 {
             self.iterations = iterations
             self.iterations = iterations
             self.prf = prf
             self.prf = prf
 
 
-            self.numBlocks = Int(ceil(Double(keyLengthFinal) / hLen))  // l = ceil(keyLength / hLen)
+            self.numBlocks = Int(ceil(Double(keyLengthFinal) / hLen)) // l = ceil(keyLength / hLen)
         }
         }
 
 
         public func calculate() throws -> Array<UInt8> {
         public func calculate() throws -> Array<UInt8> {
             var ret = Array<UInt8>()
             var ret = Array<UInt8>()
             ret.reserveCapacity(self.numBlocks * self.prf.variant.digestLength)
             ret.reserveCapacity(self.numBlocks * self.prf.variant.digestLength)
-            for i in 1...self.numBlocks {
+            for i in 1 ... self.numBlocks {
                 // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
                 // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
                 if let value = try calculateBlock(self.salt, blockNum: i) {
                 if let value = try calculateBlock(self.salt, blockNum: i) {
                     ret.append(contentsOf: value)
                     ret.append(contentsOf: value)
@@ -75,6 +76,7 @@ public extension PKCS5 {
 }
 }
 
 
 fileprivate extension PKCS5.PBKDF2 {
 fileprivate extension PKCS5.PBKDF2 {
+
     func ARR(_ i: Int) -> Array<UInt8> {
     func ARR(_ i: Int) -> Array<UInt8> {
         var inti = Array<UInt8>(repeating: 0, count: 4)
         var inti = Array<UInt8>(repeating: 0, count: 4)
         inti[0] = UInt8((i >> 24) & 0xFF)
         inti[0] = UInt8((i >> 24) & 0xFF)
@@ -96,9 +98,9 @@ fileprivate extension PKCS5.PBKDF2 {
         if self.iterations > 1 {
         if self.iterations > 1 {
             // U_2 = PRF (P, U_1) ,
             // U_2 = PRF (P, U_1) ,
             // U_c = PRF (P, U_{c-1}) .
             // U_c = PRF (P, U_{c-1}) .
-            for _ in 2...self.iterations {
+            for _ in 2 ... self.iterations {
                 u = try prf.authenticate(u)
                 u = try prf.authenticate(u)
-                for x in 0..<ret.count {
+                for x in 0 ..< ret.count {
                     ret[x] = ret[x] ^ u[x]
                     ret[x] = ret[x] ^ u[x]
                 }
                 }
             }
             }

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

@@ -5,11 +5,10 @@
 //  Created by Marcin Krzyzanowski on 12/03/15.
 //  Created by Marcin Krzyzanowski on 12/03/15.
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
 //
 //
-//  PKCS is a group of public-key cryptography standards devised 
+//  PKCS is a group of public-key cryptography standards devised
 //  and published by RSA Security Inc, starting in the early 1990s.
 //  and published by RSA Security Inc, starting in the early 1990s.
 //
 //
 
 
 public enum PKCS5 {
 public enum PKCS5 {
     //  PKCS#5 http://tools.ietf.org/html/rfc2898
     //  PKCS#5 http://tools.ietf.org/html/rfc2898
 }
 }
-

+ 7 - 8
Sources/CryptoSwift/PKCS7.swift

@@ -14,29 +14,28 @@ public struct PKCS7: Padding {
     public enum Error: Swift.Error {
     public enum Error: Swift.Error {
         case invalidPaddingValue
         case invalidPaddingValue
     }
     }
-    
+
     public init() {
     public init() {
-        
     }
     }
-    
-    public func add(to bytes: Array<UInt8> , blockSize:Int) -> Array<UInt8> {
+
+    public func add(to bytes: Array<UInt8>, blockSize: Int) -> Array<UInt8> {
         let padding = UInt8(blockSize - (bytes.count % blockSize))
         let padding = UInt8(blockSize - (bytes.count % blockSize))
         var withPadding = bytes
         var withPadding = bytes
         if (padding == 0) {
         if (padding == 0) {
             // If the original data is a multiple of N bytes, then an extra block of bytes with value N is added.
             // If the original data is a multiple of N bytes, then an extra block of bytes with value N is added.
-            for _ in 0..<blockSize {
+            for _ in 0 ..< blockSize {
                 withPadding += [UInt8(blockSize)]
                 withPadding += [UInt8(blockSize)]
             }
             }
         } else {
         } else {
             // The value of each added byte is the number of bytes that are added
             // The value of each added byte is the number of bytes that are added
-            for _ in 0..<padding {
+            for _ in 0 ..< padding {
                 withPadding += [UInt8(padding)]
                 withPadding += [UInt8(padding)]
             }
             }
         }
         }
         return withPadding
         return withPadding
     }
     }
 
 
-    public func remove(from bytes: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
+    public func remove(from bytes: Array<UInt8>, blockSize: Int?) -> Array<UInt8> {
         guard !bytes.isEmpty, let lastByte = bytes.last else {
         guard !bytes.isEmpty, let lastByte = bytes.last else {
             return bytes
             return bytes
         }
         }
@@ -51,7 +50,7 @@ public struct PKCS7: Padding {
         }
         }
 
 
         if padding >= 1 {
         if padding >= 1 {
-            return Array(bytes[0..<finalLength])
+            return Array(bytes[0 ..< finalLength])
         }
         }
         return bytes
         return bytes
     }
     }

+ 2 - 2
Sources/CryptoSwift/Padding.swift

@@ -7,6 +7,6 @@
 //
 //
 
 
 public protocol Padding {
 public protocol Padding {
-    func add(to: Array<UInt8>, blockSize:Int) -> Array<UInt8>
-    func remove(from: Array<UInt8>, blockSize:Int?) -> Array<UInt8>
+    func add(to: Array<UInt8>, blockSize: Int) -> Array<UInt8>
+    func remove(from: Array<UInt8>, blockSize: Int?) -> Array<UInt8>
 }
 }

+ 115 - 116
Sources/CryptoSwift/Poly1305.swift

@@ -9,64 +9,64 @@
 //
 //
 ///  Poly1305 takes a 32-byte, one-time key and a message and produces a 16-byte tag that authenticates the
 ///  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.
 ///  message such that an attacker has a negligible chance of producing a valid tag for an inauthentic message.
-final public class Poly1305: Authenticator {
+public final class Poly1305: Authenticator {
 
 
     public enum Error: Swift.Error {
     public enum Error: Swift.Error {
         case authenticateError
         case authenticateError
     }
     }
 
 
     let blockSize = 16
     let blockSize = 16
-    private var ctx:Context?
-    
+    private var ctx: Context?
+
     private final class Context {
     private final class Context {
-        var r            = Array<UInt8>(repeating: 0, count: 17)
-        var h            = Array<UInt8>(repeating: 0, count: 17)
-        var pad          = Array<UInt8>(repeating: 0, count: 17)
-        var buffer       = Array<UInt8>(repeating: 0, count: 16)
-        
-        var final:UInt8   = 0
-        var leftover:Int = 0
-        
+        var r = Array<UInt8>(repeating: 0, count: 17)
+        var h = Array<UInt8>(repeating: 0, count: 17)
+        var pad = Array<UInt8>(repeating: 0, count: 17)
+        var buffer = Array<UInt8>(repeating: 0, count: 16)
+
+        var final: UInt8 = 0
+        var leftover: Int = 0
+
         init(_ key: Array<UInt8>) {
         init(_ key: Array<UInt8>) {
             precondition(key.count == 32, "Invalid key length")
             precondition(key.count == 32, "Invalid key length")
 
 
-            for i in 0..<17 {
+            for i in 0 ..< 17 {
                 h[i] = 0
                 h[i] = 0
             }
             }
-            
-            r[0] = key[0] & 0xff;
-            r[1] = key[1] & 0xff;
-            r[2] = key[2] & 0xff;
-            r[3] = key[3] & 0x0f;
-            r[4] = key[4] & 0xfc;
-            r[5] = key[5] & 0xff;
-            r[6] = key[6] & 0xff;
-            r[7] = key[7] & 0x0f;
-            r[8] = key[8] & 0xfc;
-            r[9] = key[9] & 0xff;
-            r[10] = key[10] & 0xff;
-            r[11] = key[11] & 0x0f;
-            r[12] = key[12] & 0xfc;
-            r[13] = key[13] & 0xff;
-            r[14] = key[14] & 0xff;
-            r[15] = key[15] & 0x0f;
+
+            r[0] = key[0] & 0xff
+            r[1] = key[1] & 0xff
+            r[2] = key[2] & 0xff
+            r[3] = key[3] & 0x0f
+            r[4] = key[4] & 0xfc
+            r[5] = key[5] & 0xff
+            r[6] = key[6] & 0xff
+            r[7] = key[7] & 0x0f
+            r[8] = key[8] & 0xfc
+            r[9] = key[9] & 0xff
+            r[10] = key[10] & 0xff
+            r[11] = key[11] & 0x0f
+            r[12] = key[12] & 0xfc
+            r[13] = key[13] & 0xff
+            r[14] = key[14] & 0xff
+            r[15] = key[15] & 0x0f
             r[16] = 0
             r[16] = 0
-            
-            for i in 0..<16 {
+
+            for i in 0 ..< 16 {
                 pad[i] = key[i + 16]
                 pad[i] = key[i + 16]
             }
             }
             pad[16] = 0
             pad[16] = 0
-            
+
             leftover = 0
             leftover = 0
             final = 0
             final = 0
         }
         }
-        
+
         deinit {
         deinit {
-            for i in 0..<buffer.count {
+            for i in 0 ..< buffer.count {
                 buffer[i] = 0
                 buffer[i] = 0
             }
             }
-            
-            for i in 0..<r.count {
+
+            for i in 0 ..< r.count {
                 r[i] = 0
                 r[i] = 0
                 h[i] = 0
                 h[i] = 0
                 pad[i] = 0
                 pad[i] = 0
@@ -77,203 +77,202 @@ final public class Poly1305: Authenticator {
     }
     }
 
 
     /// - parameter key: 32-byte key
     /// - parameter key: 32-byte key
-    public init (key: Array<UInt8>) {
+    public init(key: Array<UInt8>) {
         ctx = Context(key)
         ctx = Context(key)
     }
     }
 
 
     // MARK: - Private
     // MARK: - Private
 
 
     /**
     /**
-    Add message to be processed
-    
-    - parameter context: Context
-    - parameter message: message
-    - parameter bytes:   length of the message fragment to be processed
-    */
-    private func update(_ context:Context, message:Array<UInt8>, bytes:Int? = nil) {
+     Add message to be processed
+
+     - parameter context: Context
+     - parameter message: message
+     - parameter bytes:   length of the message fragment to be processed
+     */
+    private func update(_ context: Context, message: Array<UInt8>, bytes: Int? = nil) {
         var bytes = bytes ?? message.count
         var bytes = bytes ?? message.count
         var mPos = 0
         var mPos = 0
-        
+
         /* handle leftover */
         /* handle leftover */
         if (context.leftover > 0) {
         if (context.leftover > 0) {
             var want = blockSize - context.leftover
             var want = blockSize - context.leftover
             if (want > bytes) {
             if (want > bytes) {
                 want = bytes
                 want = bytes
             }
             }
-            
-            for i in 0..<want {
+
+            for i in 0 ..< want {
                 context.buffer[context.leftover + i] = message[mPos + i]
                 context.buffer[context.leftover + i] = message[mPos + i]
             }
             }
-            
+
             bytes -= want
             bytes -= want
             mPos += want
             mPos += want
             context.leftover += want
             context.leftover += want
-            
+
             if (context.leftover < blockSize) {
             if (context.leftover < blockSize) {
                 return
                 return
             }
             }
-            
+
             blocks(context, m: context.buffer)
             blocks(context, m: context.buffer)
             context.leftover = 0
             context.leftover = 0
         }
         }
-        
+
         /* process full blocks */
         /* process full blocks */
         if (bytes >= blockSize) {
         if (bytes >= blockSize) {
             let want = bytes & ~(blockSize - 1)
             let want = bytes & ~(blockSize - 1)
             blocks(context, m: message, startPos: mPos)
             blocks(context, m: message, startPos: mPos)
             mPos += want
             mPos += want
-            bytes -= want;
+            bytes -= want
         }
         }
-        
+
         /* store leftover */
         /* store leftover */
         if (bytes > 0) {
         if (bytes > 0) {
-            for i in 0..<bytes {
+            for i in 0 ..< bytes {
                 context.buffer[context.leftover + i] = message[mPos + i]
                 context.buffer[context.leftover + i] = message[mPos + i]
             }
             }
-            
+
             context.leftover += bytes
             context.leftover += bytes
         }
         }
     }
     }
-    
-    private func finish(_ context:Context) -> Array<UInt8>? {
-        var mac = Array<UInt8>(repeating: 0, count: 16);
-        
+
+    private func finish(_ context: Context) -> Array<UInt8>? {
+        var mac = Array<UInt8>(repeating: 0, count: 16)
+
         /* process the remaining block */
         /* process the remaining block */
         if (context.leftover > 0) {
         if (context.leftover > 0) {
 
 
             context.buffer[context.leftover] = 1
             context.buffer[context.leftover] = 1
-            for i in (context.leftover + 1)..<blockSize {
+            for i in (context.leftover + 1) ..< blockSize {
                 context.buffer[i] = 0
                 context.buffer[i] = 0
             }
             }
             context.final = 1
             context.final = 1
-            
+
             blocks(context, m: context.buffer)
             blocks(context, m: context.buffer)
         }
         }
-        
-        
+
         /* fully reduce h */
         /* fully reduce h */
         freeze(context)
         freeze(context)
-        
+
         /* h = (h + pad) % (1 << 128) */
         /* h = (h + pad) % (1 << 128) */
         add(context, c: context.pad)
         add(context, c: context.pad)
-        for i in 0..<mac.count {
+        for i in 0 ..< mac.count {
             mac[i] = context.h[i]
             mac[i] = context.h[i]
         }
         }
-        
+
         return mac
         return mac
     }
     }
-    
+
     // MARK: - Utils
     // MARK: - Utils
-    
-    private func add(_ context:Context, c:Array<UInt8>) {
+
+    private func add(_ context: Context, c: Array<UInt8>) {
         if (context.h.count != 17 && c.count != 17) {
         if (context.h.count != 17 && c.count != 17) {
             assertionFailure()
             assertionFailure()
             return
             return
         }
         }
-        
-        var u:UInt16 = 0
-        for i in 0..<17 {
+
+        var u: UInt16 = 0
+        for i in 0 ..< 17 {
             u += UInt16(context.h[i]) + UInt16(c[i])
             u += UInt16(context.h[i]) + UInt16(c[i])
             context.h[i] = UInt8.with(value: u)
             context.h[i] = UInt8.with(value: u)
             u = u >> 8
             u = u >> 8
         }
         }
         return
         return
     }
     }
-    
-    private func squeeze(_ context:Context, hr:Array<UInt32>) {
+
+    private func squeeze(_ context: Context, hr: Array<UInt32>) {
         if (context.h.count != 17 && hr.count != 17) {
         if (context.h.count != 17 && hr.count != 17) {
             assertionFailure()
             assertionFailure()
             return
             return
         }
         }
-        
-        var u:UInt32 = 0
-        
-        for i in 0..<16 {
-            u += hr[i];
+
+        var u: UInt32 = 0
+
+        for i in 0 ..< 16 {
+            u += hr[i]
             context.h[i] = UInt8.with(value: u) // crash! h[i] = UInt8(u) & 0xff
             context.h[i] = UInt8.with(value: u) // crash! h[i] = UInt8(u) & 0xff
-            u >>= 8;
+            u >>= 8
         }
         }
-        
+
         u += hr[16]
         u += hr[16]
         context.h[16] = UInt8.with(value: u) & 0x03
         context.h[16] = UInt8.with(value: u) & 0x03
         u >>= 2
         u >>= 2
-        u += (u << 2); /* u *= 5; */
-        for i in 0..<16 {
+        u += (u << 2) /* u *= 5; */
+        for i in 0 ..< 16 {
             u += UInt32(context.h[i])
             u += UInt32(context.h[i])
             context.h[i] = UInt8.with(value: u) // crash! h[i] = UInt8(u) & 0xff
             context.h[i] = UInt8.with(value: u) // crash! h[i] = UInt8(u) & 0xff
             u >>= 8
             u >>= 8
         }
         }
-        context.h[16] += UInt8.with(value: u);
+        context.h[16] += UInt8.with(value: u)
     }
     }
-    
-    private func freeze(_ context:Context) {
-        assert(context.h.count == 17,"Invalid length")
+
+    private func freeze(_ context: Context) {
+        assert(context.h.count == 17, "Invalid length")
         if (context.h.count != 17) {
         if (context.h.count != 17) {
             return
             return
         }
         }
-        
-        let minusp:Array<UInt8> = [0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfc]
-        var horig:Array<UInt8> = Array<UInt8>(repeating: 0, count: 17)
-        
+
+        let minusp: Array<UInt8> = [0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc]
+        var horig: Array<UInt8> = Array<UInt8>(repeating: 0, count: 17)
+
         /* compute h + -p */
         /* compute h + -p */
-        for i in 0..<17 {
+        for i in 0 ..< 17 {
             horig[i] = context.h[i]
             horig[i] = context.h[i]
         }
         }
-        
+
         add(context, c: minusp)
         add(context, c: minusp)
-        
+
         /* select h if h < p, or h + -p if h >= p */
         /* select h if h < p, or h + -p if h >= p */
-        let bits:[Bit] = (context.h[16] >> 7).bits()
+        let bits: [Bit] = (context.h[16] >> 7).bits()
         let invertedBits = bits.map({ (bit) -> Bit in
         let invertedBits = bits.map({ (bit) -> Bit in
             return bit.inverted()
             return bit.inverted()
         })
         })
-        
+
         let negative = UInt8(bits: invertedBits)
         let negative = UInt8(bits: invertedBits)
-        for i in 0..<17 {
-            context.h[i] ^= negative & (horig[i] ^ context.h[i]);
+        for i in 0 ..< 17 {
+            context.h[i] ^= negative & (horig[i] ^ context.h[i])
         }
         }
     }
     }
-    
-    private func blocks(_ context:Context, m:Array<UInt8>, startPos:Int = 0) {
+
+    private func blocks(_ context: Context, m: Array<UInt8>, startPos: Int = 0) {
         var bytes = m.count
         var bytes = m.count
         let hibit = context.final ^ 1 // 1 <<128
         let hibit = context.final ^ 1 // 1 <<128
         var mPos = startPos
         var mPos = startPos
-        
+
         while (bytes >= Int(blockSize)) {
         while (bytes >= Int(blockSize)) {
-            var hr:Array<UInt32> = Array<UInt32>(repeating: 0, count: 17)
-            var u:UInt32 = 0
-            var c:Array<UInt8> = Array<UInt8>(repeating: 0, count: 17)
-            
+            var hr: Array<UInt32> = Array<UInt32>(repeating: 0, count: 17)
+            var u: UInt32 = 0
+            var c: Array<UInt8> = Array<UInt8>(repeating: 0, count: 17)
+
             /* h += m */
             /* h += m */
-            for i in 0..<16 {
+            for i in 0 ..< 16 {
                 c[i] = m[mPos + i]
                 c[i] = m[mPos + i]
             }
             }
             c[16] = hibit
             c[16] = hibit
-            
+
             add(context, c: c)
             add(context, c: c)
-            
+
             /* h *= r */
             /* h *= r */
-            for i in 0..<17 {
+            for i in 0 ..< 17 {
                 u = 0
                 u = 0
-                for j in 0...i {
+                for j in 0 ... i {
                     u = u + UInt32(UInt16(context.h[j])) * UInt32(context.r[i - j]) // u += (unsigned short)st->h[j] * st->r[i - j];
                     u = u + UInt32(UInt16(context.h[j])) * UInt32(context.r[i - j]) // u += (unsigned short)st->h[j] * st->r[i - j];
                 }
                 }
-                for j in (i+1)..<17 {
-                    var v:UInt32 = UInt32(UInt16(context.h[j])) * UInt32(context.r[i + 17 - j])  // unsigned long v = (unsigned short)st->h[j] * st->r[i + 17 - j];
+                for j in (i + 1) ..< 17 {
+                    var v: UInt32 = UInt32(UInt16(context.h[j])) * UInt32(context.r[i + 17 - j]) // unsigned long v = (unsigned short)st->h[j] * st->r[i + 17 - j];
                     v = ((v << 8) &+ (v << 6))
                     v = ((v << 8) &+ (v << 6))
                     u = u &+ v
                     u = u &+ v
                 }
                 }
                 hr[i] = u
                 hr[i] = u
             }
             }
-            
+
             squeeze(context, hr: hr)
             squeeze(context, hr: hr)
-            
+
             mPos += blockSize
             mPos += blockSize
             bytes -= blockSize
             bytes -= blockSize
         }
         }
     }
     }
 
 
-    //MARK: - Authenticator
+    // MARK: - Authenticator
 
 
     /**
     /**
      Calculate Message Authentication Code (MAC) for message.
      Calculate Message Authentication Code (MAC) for message.
@@ -283,7 +282,7 @@ final public class Poly1305: Authenticator {
 
 
      - returns: 16-byte tag that authenticates the message
      - returns: 16-byte tag that authenticates the message
      */
      */
-    public func authenticate(_ bytes:Array<UInt8>) throws -> Array<UInt8> {
+    public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
         guard let ctx = self.ctx else {
         guard let ctx = self.ctx else {
             throw Error.authenticateError
             throw Error.authenticateError
         }
         }

+ 45 - 44
Sources/CryptoSwift/Rabbit.swift

@@ -8,7 +8,7 @@
 
 
 private typealias Key = SecureBytes
 private typealias Key = SecureBytes
 
 
-final public class Rabbit: BlockCipher {
+public final class Rabbit: BlockCipher {
 
 
     public enum Error: Swift.Error {
     public enum Error: Swift.Error {
         case invalidKeyOrInitializationVector
         case invalidKeyOrInitializationVector
@@ -16,28 +16,28 @@ final public class Rabbit: BlockCipher {
 
 
     /// Size of IV in bytes
     /// Size of IV in bytes
     public static let ivSize = 64 / 8
     public static let ivSize = 64 / 8
-    
+
     /// Size of key in bytes
     /// Size of key in bytes
     public static let keySize = 128 / 8
     public static let keySize = 128 / 8
-    
+
     /// Size of block in bytes
     /// Size of block in bytes
     public static let blockSize = 128 / 8
     public static let blockSize = 128 / 8
-    
+
     /// Key
     /// Key
     private let key: Key
     private let key: Key
-    
+
     /// IV (optional)
     /// IV (optional)
     private let iv: Array<UInt8>?
     private let iv: Array<UInt8>?
-    
+
     /// State variables
     /// State variables
     private var x = Array<UInt32>(repeating: 0, count: 8)
     private var x = Array<UInt32>(repeating: 0, count: 8)
-    
+
     /// Counter variables
     /// Counter variables
     private var c = Array<UInt32>(repeating: 0, count: 8)
     private var c = Array<UInt32>(repeating: 0, count: 8)
-    
+
     /// Counter carry
     /// Counter carry
     private var p7: UInt32 = 0
     private var p7: UInt32 = 0
-    
+
     /// 'a' constants
     /// 'a' constants
     private var a: Array<UInt32> = [
     private var a: Array<UInt32> = [
         0x4D34D34D,
         0x4D34D34D,
@@ -49,58 +49,58 @@ final public class Rabbit: BlockCipher {
         0x4D34D34D,
         0x4D34D34D,
         0xD34D34D3,
         0xD34D34D3,
     ]
     ]
-    
+
     // MARK: - Initializers
     // MARK: - Initializers
-    convenience public init(key:Array<UInt8>) throws {
+    public convenience init(key: Array<UInt8>) throws {
         try self.init(key: key, iv: nil)
         try self.init(key: key, iv: nil)
     }
     }
-    
-    public init(key:Array<UInt8>, iv:Array<UInt8>?) throws {
+
+    public init(key: Array<UInt8>, iv: Array<UInt8>?) throws {
         self.key = Key(bytes: key)
         self.key = Key(bytes: key)
         self.iv = iv
         self.iv = iv
-        
+
         guard key.count == Rabbit.keySize && (iv == nil || iv!.count == Rabbit.ivSize) else {
         guard key.count == Rabbit.keySize && (iv == nil || iv!.count == Rabbit.ivSize) else {
             throw Error.invalidKeyOrInitializationVector
             throw Error.invalidKeyOrInitializationVector
         }
         }
     }
     }
-    
+
     // MARK: -
     // MARK: -
     fileprivate func setup() {
     fileprivate func setup() {
         p7 = 0
         p7 = 0
-        
+
         // Key divided into 8 subkeys
         // Key divided into 8 subkeys
         var k = Array<UInt32>(repeating: 0, count: 8)
         var k = Array<UInt32>(repeating: 0, count: 8)
-        for j in 0..<8 {
-            k[j] = UInt32(key[Rabbit.blockSize - (2*j + 1)]) | (UInt32(key[Rabbit.blockSize - (2*j + 2)]) << 8)
+        for j in 0 ..< 8 {
+            k[j] = UInt32(key[Rabbit.blockSize - (2 * j + 1)]) | (UInt32(key[Rabbit.blockSize - (2 * j + 2)]) << 8)
         }
         }
-        
+
         // Initialize state and counter variables from subkeys
         // Initialize state and counter variables from subkeys
-        for j in 0..<8 {
+        for j in 0 ..< 8 {
             if j % 2 == 0 {
             if j % 2 == 0 {
-                x[j] = (k[(j+1) % 8] << 16) | k[j]
-                c[j] = (k[(j+4) % 8] << 16) | k[(j+5) % 8]
+                x[j] = (k[(j + 1) % 8] << 16) | k[j]
+                c[j] = (k[(j + 4) % 8] << 16) | k[(j + 5) % 8]
             } else {
             } else {
-                x[j] = (k[(j+5) % 8] << 16) | k[(j+4) % 8]
-                c[j] = (k[j] << 16)         | k[(j+1) % 8]
+                x[j] = (k[(j + 5) % 8] << 16) | k[(j + 4) % 8]
+                c[j] = (k[j] << 16) | k[(j + 1) % 8]
             }
             }
         }
         }
-        
+
         // Iterate system four times
         // Iterate system four times
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
-        
+
         // Reinitialize counter variables
         // Reinitialize counter variables
-        for j in 0..<8 {
-            c[j] = c[j] ^ x[(j+4) % 8]
+        for j in 0 ..< 8 {
+            c[j] = c[j] ^ x[(j + 4) % 8]
         }
         }
-        
+
         if let iv = iv {
         if let iv = iv {
             setupIV(iv)
             setupIV(iv)
         }
         }
     }
     }
-    
+
     private func setupIV(_ iv: Array<UInt8>) {
     private func setupIV(_ iv: Array<UInt8>) {
         // 63...56 55...48 47...40 39...32 31...24 23...16 15...8 7...0 IV bits
         // 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
         //    0       1       2       3       4       5       6     7   IV bytes in array
@@ -108,7 +108,7 @@ final public class Rabbit: BlockCipher {
         let iv1 = UInt32(bytes: [iv[0], iv[1], iv[4], iv[5]])
         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 iv2 = UInt32(bytes: [iv[0], iv[1], iv[2], iv[3]])
         let iv3 = UInt32(bytes: [iv[2], iv[3], iv[6], iv[7]])
         let iv3 = UInt32(bytes: [iv[2], iv[3], iv[6], iv[7]])
-        
+
         // Modify the counter state as function of the IV
         // Modify the counter state as function of the IV
         c[0] = c[0] ^ iv0
         c[0] = c[0] ^ iv0
         c[1] = c[1] ^ iv1
         c[1] = c[1] ^ iv1
@@ -118,46 +118,46 @@ final public class Rabbit: BlockCipher {
         c[5] = c[5] ^ iv1
         c[5] = c[5] ^ iv1
         c[6] = c[6] ^ iv2
         c[6] = c[6] ^ iv2
         c[7] = c[7] ^ iv3
         c[7] = c[7] ^ iv3
-        
+
         // Iterate system four times
         // Iterate system four times
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
         nextState()
     }
     }
-    
+
     private func nextState() {
     private func nextState() {
         // Before an iteration the counters are incremented
         // Before an iteration the counters are incremented
         var carry = p7
         var carry = p7
-        for j in 0..<8 {
+        for j in 0 ..< 8 {
             let prev = c[j]
             let prev = c[j]
             c[j] = prev &+ a[j] &+ carry
             c[j] = prev &+ a[j] &+ carry
             carry = prev > c[j] ? 1 : 0 // detect overflow
             carry = prev > c[j] ? 1 : 0 // detect overflow
         }
         }
         p7 = carry // save last carry bit
         p7 = carry // save last carry bit
-        
+
         // Iteration of the system
         // Iteration of the system
         var newX = Array<UInt32>(repeating: 0, count: 8)
         var newX = Array<UInt32>(repeating: 0, count: 8)
         newX[0] = g(0) &+ rotateLeft(g(7), by: 16) &+ rotateLeft(g(6), by: 16)
         newX[0] = g(0) &+ rotateLeft(g(7), by: 16) &+ rotateLeft(g(6), by: 16)
-        newX[1] = g(1) &+ rotateLeft(g(0), by: 8)  &+ g(7)
+        newX[1] = g(1) &+ rotateLeft(g(0), by: 8) &+ g(7)
         newX[2] = g(2) &+ rotateLeft(g(1), by: 16) &+ rotateLeft(g(0), by: 16)
         newX[2] = g(2) &+ rotateLeft(g(1), by: 16) &+ rotateLeft(g(0), by: 16)
-        newX[3] = g(3) &+ rotateLeft(g(2), by: 8)  &+ g(1)
+        newX[3] = g(3) &+ rotateLeft(g(2), by: 8) &+ g(1)
         newX[4] = g(4) &+ rotateLeft(g(3), by: 16) &+ rotateLeft(g(2), by: 16)
         newX[4] = g(4) &+ rotateLeft(g(3), by: 16) &+ rotateLeft(g(2), by: 16)
-        newX[5] = g(5) &+ rotateLeft(g(4), by: 8)  &+ g(3)
+        newX[5] = g(5) &+ rotateLeft(g(4), by: 8) &+ g(3)
         newX[6] = g(6) &+ rotateLeft(g(5), by: 16) &+ rotateLeft(g(4), by: 16)
         newX[6] = g(6) &+ rotateLeft(g(5), by: 16) &+ rotateLeft(g(4), by: 16)
-        newX[7] = g(7) &+ rotateLeft(g(6), by: 8)  &+ g(5)
+        newX[7] = g(7) &+ rotateLeft(g(6), by: 8) &+ g(5)
         x = newX
         x = newX
     }
     }
-    
+
     private func g(_ j: Int) -> UInt32 {
     private func g(_ j: Int) -> UInt32 {
         let sum = x[j] &+ c[j]
         let sum = x[j] &+ c[j]
         let square = UInt64(sum) * UInt64(sum)
         let square = UInt64(sum) * UInt64(sum)
         return UInt32(truncatingBitPattern: square ^ (square >> 32))
         return UInt32(truncatingBitPattern: square ^ (square >> 32))
     }
     }
-    
+
     fileprivate func nextOutput() -> Array<UInt8> {
     fileprivate func nextOutput() -> Array<UInt8> {
         nextState()
         nextState()
-        
+
         var output16 = [UInt16](repeating: 0, count: Rabbit.blockSize / 2)
         var output16 = [UInt16](repeating: 0, count: Rabbit.blockSize / 2)
         output16[7] = UInt16(truncatingBitPattern: x[0]) ^ UInt16(truncatingBitPattern: x[5] >> 16)
         output16[7] = UInt16(truncatingBitPattern: x[0]) ^ UInt16(truncatingBitPattern: x[5] >> 16)
         output16[6] = UInt16(truncatingBitPattern: x[0] >> 16) ^ UInt16(truncatingBitPattern: x[3])
         output16[6] = UInt16(truncatingBitPattern: x[0] >> 16) ^ UInt16(truncatingBitPattern: x[3])
@@ -167,9 +167,9 @@ final public class Rabbit: BlockCipher {
         output16[2] = UInt16(truncatingBitPattern: x[4] >> 16) ^ UInt16(truncatingBitPattern: x[7])
         output16[2] = UInt16(truncatingBitPattern: x[4] >> 16) ^ UInt16(truncatingBitPattern: x[7])
         output16[1] = UInt16(truncatingBitPattern: x[6]) ^ UInt16(truncatingBitPattern: x[3] >> 16)
         output16[1] = UInt16(truncatingBitPattern: x[6]) ^ UInt16(truncatingBitPattern: x[3] >> 16)
         output16[0] = UInt16(truncatingBitPattern: x[6] >> 16) ^ UInt16(truncatingBitPattern: x[1])
         output16[0] = UInt16(truncatingBitPattern: x[6] >> 16) ^ UInt16(truncatingBitPattern: x[1])
-        
+
         var output8 = Array<UInt8>(repeating: 0, count: Rabbit.blockSize)
         var output8 = Array<UInt8>(repeating: 0, count: Rabbit.blockSize)
-        for j in 0..<output16.count {
+        for j in 0 ..< output16.count {
             output8[j * 2] = UInt8(truncatingBitPattern: output16[j] >> 8)
             output8[j * 2] = UInt8(truncatingBitPattern: output16[j] >> 8)
             output8[j * 2 + 1] = UInt8(truncatingBitPattern: output16[j])
             output8[j * 2 + 1] = UInt8(truncatingBitPattern: output16[j])
         }
         }
@@ -179,6 +179,7 @@ final public class Rabbit: BlockCipher {
 
 
 // MARK: Cipher
 // MARK: Cipher
 extension Rabbit: Cipher {
 extension Rabbit: Cipher {
+
     public func encrypt<C: Collection>(_ bytes: C) -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
     public func encrypt<C: Collection>(_ bytes: C) -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
         setup()
         setup()
 
 

+ 19 - 20
Sources/CryptoSwift/SHA1.swift

@@ -7,7 +7,7 @@
 //
 //
 
 
 public final class SHA1: DigestType {
 public final class SHA1: DigestType {
-    static let digestLength:Int = 20 // 160 / 8
+    static let digestLength: Int = 20 // 160 / 8
     static let blockSize: Int = 64
     static let blockSize: Int = 64
     fileprivate static let hashInitialValue: ContiguousArray<UInt32> = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
     fileprivate static let hashInitialValue: ContiguousArray<UInt32> = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
 
 
@@ -16,7 +16,6 @@ public final class SHA1: DigestType {
     fileprivate var accumulatedHash: ContiguousArray<UInt32> = SHA1.hashInitialValue
     fileprivate var accumulatedHash: ContiguousArray<UInt32> = SHA1.hashInitialValue
 
 
     public init() {
     public init() {
-
     }
     }
 
 
     public func calculate(for bytes: Array<UInt8>) -> Array<UInt8> {
     public func calculate(for bytes: Array<UInt8>) -> Array<UInt8> {
@@ -31,50 +30,50 @@ public final class SHA1: DigestType {
         // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // Extend the sixteen 32-bit words into eighty 32-bit words:
         // Extend the sixteen 32-bit words into eighty 32-bit words:
         var M = ContiguousArray<UInt32>(repeating: 0, count: 80)
         var M = ContiguousArray<UInt32>(repeating: 0, count: 80)
-        for x in 0..<M.count {
+        for x in 0 ..< M.count {
             switch x {
             switch x {
-            case 0...15:
+            case 0 ... 15:
                 let start = chunk.startIndex.advanced(by: x * 4) // * MemoryLayout<UInt32>.size
                 let start = chunk.startIndex.advanced(by: x * 4) // * MemoryLayout<UInt32>.size
                 M[x] = UInt32(bytes: chunk, fromIndex: start)
                 M[x] = UInt32(bytes: chunk, fromIndex: start)
                 break
                 break
             default:
             default:
-                M[x] = rotateLeft(M[x-3] ^ M[x-8] ^ M[x-14] ^ M[x-16], by: 1)
+                M[x] = rotateLeft(M[x - 3] ^ M[x - 8] ^ M[x - 14] ^ M[x - 16], by: 1)
                 break
                 break
             }
             }
         }
         }
-        
+
         var A = hh[0]
         var A = hh[0]
         var B = hh[1]
         var B = hh[1]
         var C = hh[2]
         var C = hh[2]
         var D = hh[3]
         var D = hh[3]
         var E = hh[4]
         var E = hh[4]
-        
+
         // Main loop
         // Main loop
-        for j in 0...79 {
-            var f: UInt32 = 0;
+        for j in 0 ... 79 {
+            var f: UInt32 = 0
             var k: UInt32 = 0
             var k: UInt32 = 0
-            
+
             switch (j) {
             switch (j) {
-            case 0...19:
+            case 0 ... 19:
                 f = (B & C) | ((~B) & D)
                 f = (B & C) | ((~B) & D)
                 k = 0x5A827999
                 k = 0x5A827999
                 break
                 break
-            case 20...39:
+            case 20 ... 39:
                 f = B ^ C ^ D
                 f = B ^ C ^ D
                 k = 0x6ED9EBA1
                 k = 0x6ED9EBA1
                 break
                 break
-            case 40...59:
+            case 40 ... 59:
                 f = (B & C) | (B & D) | (C & D)
                 f = (B & C) | (B & D) | (C & D)
                 k = 0x8F1BBCDC
                 k = 0x8F1BBCDC
                 break
                 break
-            case 60...79:
+            case 60 ... 79:
                 f = B ^ C ^ D
                 f = B ^ C ^ D
                 k = 0xCA62C1D6
                 k = 0xCA62C1D6
                 break
                 break
             default:
             default:
                 break
                 break
             }
             }
-            
+
             let temp = (rotateLeft(A, by: 5) &+ f &+ E &+ M[j] &+ k) & 0xffffffff
             let temp = (rotateLeft(A, by: 5) &+ f &+ E &+ M[j] &+ k) & 0xffffffff
             E = D
             E = D
             D = C
             D = C
@@ -82,7 +81,7 @@ public final class SHA1: DigestType {
             B = A
             B = A
             A = temp
             A = temp
         }
         }
-        
+
         hh[0] = (hh[0] &+ A) & 0xffffffff
         hh[0] = (hh[0] &+ A) & 0xffffffff
         hh[1] = (hh[1] &+ B) & 0xffffffff
         hh[1] = (hh[1] &+ B) & 0xffffffff
         hh[2] = (hh[2] &+ C) & 0xffffffff
         hh[2] = (hh[2] &+ C) & 0xffffffff
@@ -92,6 +91,7 @@ public final class SHA1: DigestType {
 }
 }
 
 
 extension SHA1: Updatable {
 extension SHA1: Updatable {
+
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
         self.accumulated += bytes
         self.accumulated += bytes
 
 
@@ -119,9 +119,9 @@ extension SHA1: Updatable {
         // output current hash
         // output current hash
         var result = Array<UInt8>(repeating: 0, count: SHA1.digestLength)
         var result = Array<UInt8>(repeating: 0, count: SHA1.digestLength)
         var pos = 0
         var pos = 0
-        for idx in 0..<self.accumulatedHash.count {
+        for idx in 0 ..< self.accumulatedHash.count {
             let h = self.accumulatedHash[idx].bigEndian
             let h = self.accumulatedHash[idx].bigEndian
-            result[pos]     = UInt8(h & 0xff)
+            result[pos] = UInt8(h & 0xff)
             result[pos + 1] = UInt8((h >> 8) & 0xff)
             result[pos + 1] = UInt8((h >> 8) & 0xff)
             result[pos + 2] = UInt8((h >> 16) & 0xff)
             result[pos + 2] = UInt8((h >> 16) & 0xff)
             result[pos + 3] = UInt8((h >> 24) & 0xff)
             result[pos + 3] = UInt8((h >> 24) & 0xff)
@@ -132,8 +132,7 @@ extension SHA1: Updatable {
         if isLast {
         if isLast {
             self.accumulatedHash = SHA1.hashInitialValue
             self.accumulatedHash = SHA1.hashInitialValue
         }
         }
-        
+
         return result
         return result
     }
     }
 }
 }
-

+ 83 - 83
Sources/CryptoSwift/SHA2.swift

@@ -13,7 +13,7 @@ public final class SHA2: DigestType {
     let size: Int
     let size: Int
     let blockSize: Int
     let blockSize: Int
     let digestLength: Int
     let digestLength: Int
-    private let k:Array<UInt64>
+    private let k: Array<UInt64>
 
 
     fileprivate var accumulated = Array<UInt8>()
     fileprivate var accumulated = Array<UInt8>()
     fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var processedBytesTotalCount: Int = 0
@@ -23,7 +23,7 @@ public final class SHA2: DigestType {
     public enum Variant: RawRepresentable {
     public enum Variant: RawRepresentable {
         case sha224, sha256, sha384, sha512
         case sha224, sha256, sha384, sha512
 
 
-        public var digestLength:Int {
+        public var digestLength: Int {
             return self.rawValue / 8
             return self.rawValue / 8
         }
         }
 
 
@@ -54,22 +54,22 @@ public final class SHA2: DigestType {
             switch (rawValue) {
             switch (rawValue) {
             case 224:
             case 224:
                 self = .sha224
                 self = .sha224
-                break;
+                break
             case 256:
             case 256:
                 self = .sha256
                 self = .sha256
-                break;
+                break
             case 384:
             case 384:
                 self = .sha384
                 self = .sha384
-                break;
+                break
             case 512:
             case 512:
                 self = .sha512
                 self = .sha512
-                break;
+                break
             default:
             default:
                 return nil
                 return nil
             }
             }
         }
         }
 
 
-        fileprivate var h:Array<UInt64> {
+        fileprivate var h: Array<UInt64> {
             switch self {
             switch self {
             case .sha224:
             case .sha224:
                 return [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]
                 return [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4]
@@ -97,40 +97,40 @@ public final class SHA2: DigestType {
     public init(variant: SHA2.Variant) {
     public init(variant: SHA2.Variant) {
         self.variant = variant
         self.variant = variant
         switch self.variant {
         switch self.variant {
-            case .sha224, .sha256:
-                self.accumulatedHash32 = variant.h.map { UInt32($0) } //FIXME: UInt64 for process64
-                self.blockSize = variant.blockSize
-                self.size = variant.rawValue
-                self.digestLength = variant.digestLength
-                self.k = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
-                        0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
-                        0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
-                        0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
-                        0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
-                        0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
-                        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
-                        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
-            case .sha384, .sha512:
-                self.accumulatedHash64 = variant.h
-                self.blockSize = variant.blockSize
-                self.size = variant.rawValue
-                self.digestLength = variant.digestLength
-                self.k = [0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
-                          0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
-                          0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
-                          0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
-                          0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
-                          0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
-                          0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
-                          0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
-                          0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
-                          0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
-                          0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
-                          0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
-                          0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
-                          0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
-                          0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
-                          0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817]
+        case .sha224, .sha256:
+            self.accumulatedHash32 = variant.h.map { UInt32($0) } // FIXME: UInt64 for process64
+            self.blockSize = variant.blockSize
+            self.size = variant.rawValue
+            self.digestLength = variant.digestLength
+            self.k = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+                      0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+                      0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+                      0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+                      0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+                      0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+                      0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+                      0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
+        case .sha384, .sha512:
+            self.accumulatedHash64 = variant.h
+            self.blockSize = variant.blockSize
+            self.size = variant.rawValue
+            self.digestLength = variant.digestLength
+            self.k = [0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
+                      0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
+                      0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
+                      0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
+                      0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
+                      0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
+                      0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
+                      0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
+                      0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
+                      0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
+                      0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
+                      0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
+                      0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
+                      0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
+                      0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
+                      0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817]
         }
         }
     }
     }
 
 
@@ -146,16 +146,16 @@ public final class SHA2: DigestType {
         // break chunk into sixteen 64-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // break chunk into sixteen 64-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // Extend the sixteen 64-bit words into eighty 64-bit words:
         // Extend the sixteen 64-bit words into eighty 64-bit words:
         var M = Array<UInt64>(repeating: 0, count: self.k.count)
         var M = Array<UInt64>(repeating: 0, count: self.k.count)
-        for x in 0..<M.count {
+        for x in 0 ..< M.count {
             switch (x) {
             switch (x) {
-            case 0...15:
+            case 0 ... 15:
                 let start = chunk.startIndex.advanced(by: x * 8) // * MemoryLayout<UInt64>.size
                 let start = chunk.startIndex.advanced(by: x * 8) // * MemoryLayout<UInt64>.size
                 M[x] = UInt64(bytes: chunk, fromIndex: start)
                 M[x] = UInt64(bytes: chunk, fromIndex: start)
                 break
                 break
             default:
             default:
-                let s0 = rotateRight(M[x-15], by: 1) ^ rotateRight(M[x-15], by: 8) ^ (M[x-15] >> 7)
-                let s1 = rotateRight(M[x-2], by: 19) ^ rotateRight(M[x-2], by: 61) ^ (M[x-2] >> 6)
-                M[x] = M[x-16] &+ s0 &+ M[x-7] &+ s1
+                let s0 = rotateRight(M[x - 15], by: 1) ^ rotateRight(M[x - 15], by: 8) ^ (M[x - 15] >> 7)
+                let s1 = rotateRight(M[x - 2], by: 19) ^ rotateRight(M[x - 2], by: 61) ^ (M[x - 2] >> 6)
+                M[x] = M[x - 16] &+ s0 &+ M[x - 7] &+ s1
                 break
                 break
             }
             }
         }
         }
@@ -170,7 +170,7 @@ public final class SHA2: DigestType {
         var H = hh[7]
         var H = hh[7]
 
 
         // Main loop
         // Main loop
-        for j in 0..<self.k.count {
+        for j in 0 ..< self.k.count {
             let s0 = rotateRight(A, by: 28) ^ rotateRight(A, by: 34) ^ rotateRight(A, by: 39)
             let s0 = rotateRight(A, by: 28) ^ rotateRight(A, by: 34) ^ rotateRight(A, by: 39)
             let maj = (A & B) ^ (A & C) ^ (B & C)
             let maj = (A & B) ^ (A & C) ^ (B & C)
             let t2 = s0 &+ maj
             let t2 = s0 &+ maj
@@ -187,7 +187,7 @@ public final class SHA2: DigestType {
             B = A
             B = A
             A = t1 &+ t2
             A = t1 &+ t2
         }
         }
-        
+
         hh[0] = (hh[0] &+ A)
         hh[0] = (hh[0] &+ A)
         hh[1] = (hh[1] &+ B)
         hh[1] = (hh[1] &+ B)
         hh[2] = (hh[2] &+ C)
         hh[2] = (hh[2] &+ C)
@@ -203,16 +203,16 @@ public final class SHA2: DigestType {
         // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // 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:
         // Extend the sixteen 32-bit words into sixty-four 32-bit words:
         var M = Array<UInt32>(repeating: 0, count: self.k.count)
         var M = Array<UInt32>(repeating: 0, count: self.k.count)
-        for x in 0..<M.count {
+        for x in 0 ..< M.count {
             switch (x) {
             switch (x) {
-            case 0...15:
+            case 0 ... 15:
                 let start = chunk.startIndex.advanced(by: x * 4) // * MemoryLayout<UInt32>.size
                 let start = chunk.startIndex.advanced(by: x * 4) // * MemoryLayout<UInt32>.size
                 M[x] = UInt32(bytes: chunk, fromIndex: start)
                 M[x] = UInt32(bytes: chunk, fromIndex: start)
                 break
                 break
             default:
             default:
-                let s0 = rotateRight(M[x-15], by: 7) ^ rotateRight(M[x-15], by: 18) ^ (M[x-15] >> 3)
-                let s1 = rotateRight(M[x-2], by: 17) ^ rotateRight(M[x-2], by: 19) ^ (M[x-2] >> 10)
-                M[x] = M[x-16] &+ s0 &+ M[x-7] &+ s1
+                let s0 = rotateRight(M[x - 15], by: 7) ^ rotateRight(M[x - 15], by: 18) ^ (M[x - 15] >> 3)
+                let s1 = rotateRight(M[x - 2], by: 17) ^ rotateRight(M[x - 2], by: 19) ^ (M[x - 2] >> 10)
+                M[x] = M[x - 16] &+ s0 &+ M[x - 7] &+ s1
                 break
                 break
             }
             }
         }
         }
@@ -227,7 +227,7 @@ public final class SHA2: DigestType {
         var H = hh[7]
         var H = hh[7]
 
 
         // Main loop
         // Main loop
-        for j in 0..<self.k.count {
+        for j in 0 ..< self.k.count {
             let s0 = rotateRight(A, by: 2) ^ rotateRight(A, by: 13) ^ rotateRight(A, by: 22)
             let s0 = rotateRight(A, by: 2) ^ rotateRight(A, by: 13) ^ rotateRight(A, by: 22)
             let maj = (A & B) ^ (A & C) ^ (B & C)
             let maj = (A & B) ^ (A & C) ^ (B & C)
             let t2 = s0 &+ maj
             let t2 = s0 &+ maj
@@ -276,10 +276,10 @@ extension SHA2: Updatable {
         for chunk in BytesSequence(chunkSize: self.blockSize, data: self.accumulated) {
         for chunk in BytesSequence(chunkSize: self.blockSize, data: self.accumulated) {
             if (isLast || (self.accumulated.count - processedBytes) >= self.blockSize) {
             if (isLast || (self.accumulated.count - processedBytes) >= self.blockSize) {
                 switch self.variant {
                 switch self.variant {
-                    case .sha224, .sha256:
-                        self.process32(block: chunk, currentHash: &self.accumulatedHash32)
-                    case .sha384, .sha512:
-                        self.process64(block: chunk, currentHash: &self.accumulatedHash64)
+                case .sha224, .sha256:
+                    self.process32(block: chunk, currentHash: &self.accumulatedHash32)
+                case .sha384, .sha512:
+                    self.process64(block: chunk, currentHash: &self.accumulatedHash64)
                 }
                 }
                 processedBytes += chunk.count
                 processedBytes += chunk.count
             }
             }
@@ -290,37 +290,37 @@ extension SHA2: Updatable {
         // output current hash
         // output current hash
         var result = Array<UInt8>(repeating: 0, count: self.variant.digestLength)
         var result = Array<UInt8>(repeating: 0, count: self.variant.digestLength)
         switch self.variant {
         switch self.variant {
-            case .sha224, .sha256:
-                var pos = 0
-                for idx in 0..<self.accumulatedHash32.count where idx < self.variant.finalLength {
-                    let h = self.accumulatedHash32[idx].bigEndian
-                    result[pos]     = UInt8(h & 0xff)
-                    result[pos + 1] = UInt8((h >> 8) & 0xff)
-                    result[pos + 2] = UInt8((h >> 16) & 0xff)
-                    result[pos + 3] = UInt8((h >> 24) & 0xff)
-                    pos += 4
-                }
-            case .sha384, .sha512:
-                var pos = 0
-                for idx in 0..<self.accumulatedHash64.count where idx < self.variant.finalLength {
-                    let h = self.accumulatedHash64[idx].bigEndian
-                    result[pos]     = UInt8(h & 0xff)
-                    result[pos + 1] = UInt8((h >> 8) & 0xff)
-                    result[pos + 2] = UInt8((h >> 16) & 0xff)
-                    result[pos + 3] = UInt8((h >> 24) & 0xff)
-                    result[pos + 4] = UInt8((h >> 32) & 0xff)
-                    result[pos + 5] = UInt8((h >> 40) & 0xff)
-                    result[pos + 6] = UInt8((h >> 48) & 0xff)
-                    result[pos + 7] = UInt8((h >> 56) & 0xff)
-                    pos += 8
-                }
+        case .sha224, .sha256:
+            var pos = 0
+            for idx in 0 ..< self.accumulatedHash32.count where idx < self.variant.finalLength {
+                let h = self.accumulatedHash32[idx].bigEndian
+                result[pos] = UInt8(h & 0xff)
+                result[pos + 1] = UInt8((h >> 8) & 0xff)
+                result[pos + 2] = UInt8((h >> 16) & 0xff)
+                result[pos + 3] = UInt8((h >> 24) & 0xff)
+                pos += 4
+            }
+        case .sha384, .sha512:
+            var pos = 0
+            for idx in 0 ..< self.accumulatedHash64.count where idx < self.variant.finalLength {
+                let h = self.accumulatedHash64[idx].bigEndian
+                result[pos] = UInt8(h & 0xff)
+                result[pos + 1] = UInt8((h >> 8) & 0xff)
+                result[pos + 2] = UInt8((h >> 16) & 0xff)
+                result[pos + 3] = UInt8((h >> 24) & 0xff)
+                result[pos + 4] = UInt8((h >> 32) & 0xff)
+                result[pos + 5] = UInt8((h >> 40) & 0xff)
+                result[pos + 6] = UInt8((h >> 48) & 0xff)
+                result[pos + 7] = UInt8((h >> 56) & 0xff)
+                pos += 8
+            }
         }
         }
 
 
         // reset hash value for instance
         // reset hash value for instance
         if isLast {
         if isLast {
             switch self.variant {
             switch self.variant {
             case .sha224, .sha256:
             case .sha224, .sha256:
-                self.accumulatedHash32 = variant.h.lazy.map { UInt32($0) } //FIXME: UInt64 for process64
+                self.accumulatedHash32 = variant.h.lazy.map { UInt32($0) } // FIXME: UInt64 for process64
             case .sha384, .sha512:
             case .sha384, .sha512:
                 self.accumulatedHash64 = variant.h
                 self.accumulatedHash64 = variant.h
             }
             }

+ 18 - 18
Sources/CryptoSwift/SHA3.swift

@@ -27,15 +27,15 @@ public final class SHA3: DigestType {
 
 
     fileprivate var accumulated = Array<UInt8>()
     fileprivate var accumulated = Array<UInt8>()
     fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var processedBytesTotalCount: Int = 0
-    fileprivate var accumulatedHash:Array<UInt64>
+    fileprivate var accumulatedHash: Array<UInt64>
 
 
     public enum Variant: RawRepresentable {
     public enum Variant: RawRepresentable {
         case sha224, sha256, sha384, sha512
         case sha224, sha256, sha384, sha512
 
 
-        public var digestLength:Int {
+        public var digestLength: Int {
             return 100 - (self.blockSize / 2)
             return 100 - (self.blockSize / 2)
         }
         }
-        
+
         public var blockSize: Int {
         public var blockSize: Int {
             return (1600 - self.rawValue * 2) / 8
             return (1600 - self.rawValue * 2) / 8
         }
         }
@@ -53,21 +53,21 @@ public final class SHA3: DigestType {
                 return 512
                 return 512
             }
             }
         }
         }
-        
+
         public init?(rawValue: RawValue) {
         public init?(rawValue: RawValue) {
             switch (rawValue) {
             switch (rawValue) {
             case 224:
             case 224:
                 self = .sha224
                 self = .sha224
-                break;
+                break
             case 256:
             case 256:
                 self = .sha256
                 self = .sha256
-                break;
+                break
             case 384:
             case 384:
                 self = .sha384
                 self = .sha384
-                break;
+                break
             case 512:
             case 512:
                 self = .sha512
                 self = .sha512
-                break;
+                break
             default:
             default:
                 return nil
                 return nil
             }
             }
@@ -97,7 +97,7 @@ public final class SHA3: DigestType {
         var c = Array<UInt64>(repeating: 0, count: 5)
         var c = Array<UInt64>(repeating: 0, count: 5)
         var d = Array<UInt64>(repeating: 0, count: 5)
         var d = Array<UInt64>(repeating: 0, count: 5)
 
 
-        for i in 0..<5 {
+        for i in 0 ..< 5 {
             c[i] = a[i] ^ a[i + 5] ^ a[i + 10] ^ a[i + 15] ^ a[i + 20]
             c[i] = a[i] ^ a[i + 5] ^ a[i + 10] ^ a[i + 15] ^ a[i + 20]
         }
         }
 
 
@@ -107,9 +107,9 @@ public final class SHA3: DigestType {
         d[3] = rotateLeft(c[4], by: 1) ^ c[2]
         d[3] = rotateLeft(c[4], by: 1) ^ c[2]
         d[4] = rotateLeft(c[0], by: 1) ^ c[3]
         d[4] = rotateLeft(c[0], by: 1) ^ c[3]
 
 
-        for i in 0..<5 {
+        for i in 0 ..< 5 {
             a[i] ^= d[i]
             a[i] ^= d[i]
-            a[i + 5]  ^= d[i]
+            a[i + 5] ^= d[i]
             a[i + 10] ^= d[i]
             a[i + 10] ^= d[i]
             a[i + 15] ^= d[i]
             a[i + 15] ^= d[i]
             a[i + 20] ^= d[i]
             a[i + 20] ^= d[i]
@@ -175,7 +175,7 @@ public final class SHA3: DigestType {
         hh[7] ^= chunk[7].littleEndian
         hh[7] ^= chunk[7].littleEndian
         hh[8] ^= chunk[8].littleEndian
         hh[8] ^= chunk[8].littleEndian
         if self.variant.blockSize > 72 { // 72 / 8, sha-512
         if self.variant.blockSize > 72 { // 72 / 8, sha-512
-            hh[9]  ^= chunk[9 ].littleEndian
+            hh[9] ^= chunk[9].littleEndian
             hh[10] ^= chunk[10].littleEndian
             hh[10] ^= chunk[10].littleEndian
             hh[11] ^= chunk[11].littleEndian
             hh[11] ^= chunk[11].littleEndian
             hh[12] ^= chunk[12].littleEndian
             hh[12] ^= chunk[12].littleEndian
@@ -201,7 +201,7 @@ public final class SHA3: DigestType {
         }
         }
 
 
         // Keccak-f
         // Keccak-f
-        for round in 0..<24 {
+        for round in 0 ..< 24 {
             θ(&hh)
             θ(&hh)
 
 
             hh[1] = rotateLeft(hh[1], by: 1)
             hh[1] = rotateLeft(hh[1], by: 1)
@@ -234,10 +234,10 @@ public final class SHA3: DigestType {
             ι(&hh, round: round)
             ι(&hh, round: round)
         }
         }
     }
     }
-
 }
 }
 
 
 extension SHA3: Updatable {
 extension SHA3: Updatable {
+
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
         self.accumulated += bytes
         self.accumulated += bytes
 
 
@@ -246,7 +246,7 @@ extension SHA3: Updatable {
             let markByteIndex = self.processedBytesTotalCount + self.accumulated.count
             let markByteIndex = self.processedBytesTotalCount + self.accumulated.count
             if self.accumulated.count == 0 || self.accumulated.count % self.variant.blockSize != 0 {
             if self.accumulated.count == 0 || self.accumulated.count % self.variant.blockSize != 0 {
                 let r = self.variant.blockSize * 8
                 let r = self.variant.blockSize * 8
-                let q = (r/8) - (self.accumulated.count % (r/8))
+                let q = (r / 8) - (self.accumulated.count % (r / 8))
                 self.accumulated += Array<UInt8>(repeating: 0, count: q)
                 self.accumulated += Array<UInt8>(repeating: 0, count: q)
             }
             }
 
 
@@ -264,7 +264,7 @@ extension SHA3: Updatable {
         self.accumulated.removeFirst(processedBytes)
         self.accumulated.removeFirst(processedBytes)
         self.processedBytesTotalCount += processedBytes
         self.processedBytesTotalCount += processedBytes
 
 
-        //TODO: verify performance, reduce vs for..in
+        // TODO: verify performance, reduce vs for..in
         let result = self.accumulatedHash.reduce(Array<UInt8>()) { (result, value) -> Array<UInt8> in
         let result = self.accumulatedHash.reduce(Array<UInt8>()) { (result, value) -> Array<UInt8> in
             return result + value.bigEndian.bytes()
             return result + value.bigEndian.bytes()
         }
         }
@@ -273,7 +273,7 @@ extension SHA3: Updatable {
         if isLast {
         if isLast {
             self.accumulatedHash = Array<UInt64>(repeating: 0, count: self.variant.digestLength)
             self.accumulatedHash = Array<UInt64>(repeating: 0, count: self.variant.digestLength)
         }
         }
-        
-        return Array(result[0..<self.variant.digestLength])
+
+        return Array(result[0 ..< self.variant.digestLength])
     }
     }
 }
 }

+ 3 - 4
Sources/CryptoSwift/String+Extension.swift

@@ -8,11 +8,11 @@
 
 
 /** String extension */
 /** String extension */
 extension String {
 extension String {
-    
+
     public func md5() -> String {
     public func md5() -> String {
         return self.utf8.lazy.map({ $0 as UInt8 }).md5().toHexString()
         return self.utf8.lazy.map({ $0 as UInt8 }).md5().toHexString()
     }
     }
-    
+
     public func sha1() -> String {
     public func sha1() -> String {
         return self.utf8.lazy.map({ $0 as UInt8 }).sha1().toHexString()
         return self.utf8.lazy.map({ $0 as UInt8 }).sha1().toHexString()
     }
     }
@@ -37,7 +37,7 @@ extension String {
         return self.utf8.lazy.map({ $0 as UInt8 }).sha3(variant).toHexString()
         return self.utf8.lazy.map({ $0 as UInt8 }).sha3(variant).toHexString()
     }
     }
 
 
-    public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> String {
+    public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> String {
         return self.utf8.lazy.map({ $0 as UInt8 }).crc32(seed: seed, reflect: reflect).bytes().toHexString()
         return self.utf8.lazy.map({ $0 as UInt8 }).crc32(seed: seed, reflect: reflect).bytes().toHexString()
     }
     }
 
 
@@ -45,7 +45,6 @@ extension String {
         return self.utf8.lazy.map({ $0 as UInt8 }).crc16(seed: seed).bytes().toHexString()
         return self.utf8.lazy.map({ $0 as UInt8 }).crc16(seed: seed).bytes().toHexString()
     }
     }
 
 
-
     /// - parameter cipher: Instance of `Cipher`
     /// - parameter cipher: Instance of `Cipher`
     /// - returns: hex string of bytes
     /// - returns: hex string of bytes
     public func encrypt(cipher: Cipher) throws -> String {
     public func encrypt(cipher: Cipher) throws -> String {

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

@@ -8,6 +8,7 @@
 
 
 /** array of bytes */
 /** array of bytes */
 extension UInt16 {
 extension UInt16 {
+
     @_specialize(ArraySlice<UInt8>)
     @_specialize(ArraySlice<UInt8>)
     init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
     init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
         self = UInt16(bytes: bytes, fromIndex: bytes.startIndex)
         self = UInt16(bytes: bytes, fromIndex: bytes.startIndex)

+ 2 - 2
Sources/CryptoSwift/UInt32+Extension.swift

@@ -12,12 +12,12 @@
     import Darwin
     import Darwin
 #endif
 #endif
 
 
-
-protocol _UInt32Type { }
+protocol _UInt32Type {}
 extension UInt32: _UInt32Type {}
 extension UInt32: _UInt32Type {}
 
 
 /** array of bytes */
 /** array of bytes */
 extension UInt32 {
 extension UInt32 {
+
     @_specialize(ArraySlice<UInt8>)
     @_specialize(ArraySlice<UInt8>)
     init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
     init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
         self = UInt32(bytes: bytes, fromIndex: bytes.startIndex)
         self = UInt32(bytes: bytes, fromIndex: bytes.startIndex)

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

@@ -8,6 +8,7 @@
 
 
 /** array of bytes */
 /** array of bytes */
 extension UInt64 {
 extension UInt64 {
+
     @_specialize(ArraySlice<UInt8>)
     @_specialize(ArraySlice<UInt8>)
     init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
     init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
         self = UInt64(bytes: bytes, fromIndex: bytes.startIndex)
         self = UInt64(bytes: bytes, fromIndex: bytes.startIndex)
@@ -26,6 +27,7 @@ extension UInt64 {
 
 
         self = val0 | val1 | val2 | val3 | val4 | val5 | val6 | val7
         self = val0 | val1 | val2 | val3 | val4 | val5 | val6 | val7
     }
     }
+
     func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
     func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
         return arrayOfBytes(value: self, length: totalBytes)
     }
     }

+ 12 - 14
Sources/CryptoSwift/UInt8+Extension.swift

@@ -12,15 +12,14 @@
     import Darwin
     import Darwin
 #endif
 #endif
 
 
-
-public protocol _UInt8Type { }
+public protocol _UInt8Type {}
 extension UInt8: _UInt8Type {}
 extension UInt8: _UInt8Type {}
 
 
 /** casting */
 /** casting */
 extension UInt8 {
 extension UInt8 {
-    
+
     /** cast because UInt8(<UInt32>) because std initializer crash if value is > byte */
     /** cast because UInt8(<UInt32>) because std initializer crash if value is > byte */
-    static func with(value:UInt64) -> UInt8 {
+    static func with(value: UInt64) -> UInt8 {
         let tmp = value & 0xFF
         let tmp = value & 0xFF
         return UInt8(tmp)
         return UInt8(tmp)
     }
     }
@@ -29,12 +28,11 @@ extension UInt8 {
         let tmp = value & 0xFF
         let tmp = value & 0xFF
         return UInt8(tmp)
         return UInt8(tmp)
     }
     }
-    
+
     static func with(value: UInt16) -> UInt8 {
     static func with(value: UInt16) -> UInt8 {
         let tmp = value & 0xFF
         let tmp = value & 0xFF
         return UInt8(tmp)
         return UInt8(tmp)
     }
     }
-
 }
 }
 
 
 /** Bits */
 /** Bits */
@@ -43,19 +41,19 @@ extension UInt8 {
     init(bits: [Bit]) {
     init(bits: [Bit]) {
         self.init(integerFrom(bits) as UInt8)
         self.init(integerFrom(bits) as UInt8)
     }
     }
-    
+
     /** array of bits */
     /** array of bits */
     func bits() -> [Bit] {
     func bits() -> [Bit] {
         let totalBitsCount = MemoryLayout<UInt8>.size * 8
         let totalBitsCount = MemoryLayout<UInt8>.size * 8
-        
+
         var bitsArray = [Bit](repeating: Bit.zero, count: totalBitsCount)
         var bitsArray = [Bit](repeating: Bit.zero, count: totalBitsCount)
-        
-        for j in 0..<totalBitsCount {
-            let bitVal:UInt8 = 1 << UInt8(totalBitsCount - 1 - j)
+
+        for j in 0 ..< totalBitsCount {
+            let bitVal: UInt8 = 1 << UInt8(totalBitsCount - 1 - j)
             let check = self & bitVal
             let check = self & bitVal
-            
+
             if (check != 0) {
             if (check != 0) {
-                bitsArray[j] = Bit.one;
+                bitsArray[j] = Bit.one
             }
             }
         }
         }
         return bitsArray
         return bitsArray
@@ -63,7 +61,7 @@ extension UInt8 {
 
 
     func bits() -> String {
     func bits() -> String {
         var s = String()
         var s = String()
-        let arr:[Bit] = self.bits()
+        let arr: [Bit] = self.bits()
         for idx in arr.indices {
         for idx in arr.indices {
             s += (arr[idx] == Bit.one ? "1" : "0")
             s += (arr[idx] == Bit.one ? "1" : "0")
             if (idx.advanced(by: 1) % 8 == 0) { s += " " }
             if (idx.advanced(by: 1) % 8 == 0) { s += " " }

+ 11 - 10
Sources/CryptoSwift/Updatable.swift

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

+ 11 - 12
Sources/CryptoSwift/Utils.swift

@@ -6,35 +6,35 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 //
 
 
-func rotateLeft(_ value:UInt8, by:UInt8) -> UInt8 {
+func rotateLeft(_ value: UInt8, by: UInt8) -> UInt8 {
     return ((value << by) & 0xFF) | (value >> (8 - by))
     return ((value << by) & 0xFF) | (value >> (8 - by))
 }
 }
 
 
-func rotateLeft(_ value:UInt16, by:UInt16) -> UInt16 {
+func rotateLeft(_ value: UInt16, by: UInt16) -> UInt16 {
     return ((value << by) & 0xFFFF) | (value >> (16 - by))
     return ((value << by) & 0xFFFF) | (value >> (16 - by))
 }
 }
 
 
-func rotateLeft(_ value:UInt32, by:UInt32) -> UInt32 {
+func rotateLeft(_ value: UInt32, by: UInt32) -> UInt32 {
     return ((value << by) & 0xFFFFFFFF) | (value >> (32 - by))
     return ((value << by) & 0xFFFFFFFF) | (value >> (32 - by))
 }
 }
 
 
-func rotateLeft(_ value:UInt64, by:UInt64) -> UInt64 {
+func rotateLeft(_ value: UInt64, by: UInt64) -> UInt64 {
     return (value << by) | (value >> (64 - by))
     return (value << by) | (value >> (64 - by))
 }
 }
 
 
-func rotateRight(_ value:UInt16, by:UInt16) -> UInt16 {
+func rotateRight(_ value: UInt16, by: UInt16) -> UInt16 {
     return (value >> by) | (value << (16 - by))
     return (value >> by) | (value << (16 - by))
 }
 }
 
 
-func rotateRight(_ value:UInt32, by:UInt32) -> UInt32 {
+func rotateRight(_ value: UInt32, by: UInt32) -> UInt32 {
     return (value >> by) | (value << (32 - by))
     return (value >> by) | (value << (32 - by))
 }
 }
 
 
-func rotateRight(_ value:UInt64, by:UInt64) -> UInt64 {
+func rotateRight(_ value: UInt64, by: UInt64) -> UInt64 {
     return ((value >> by) | (value << (64 - by)))
     return ((value >> by) | (value << (64 - by)))
 }
 }
 
 
-func reversed(_ uint8 : UInt8) -> UInt8 {
+func reversed(_ uint8: UInt8) -> UInt8 {
     var v = uint8
     var v = uint8
     v = (v & 0xF0) >> 4 | (v & 0x0F) << 4
     v = (v & 0xF0) >> 4 | (v & 0x0F) << 4
     v = (v & 0xCC) >> 2 | (v & 0x33) << 2
     v = (v & 0xCC) >> 2 | (v & 0x33) << 2
@@ -42,7 +42,7 @@ func reversed(_ uint8 : UInt8) -> UInt8 {
     return v
     return v
 }
 }
 
 
-func reversed(_ uint32 : UInt32) -> UInt32 {
+func reversed(_ uint32: UInt32) -> UInt32 {
     var v = uint32
     var v = uint32
     v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1)
     v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1)
     v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2)
     v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2)
@@ -52,9 +52,9 @@ func reversed(_ uint32 : UInt32) -> UInt32 {
     return v
     return v
 }
 }
 
 
-func xor(_ a: Array<UInt8>, _ b:Array<UInt8>) -> Array<UInt8> {
+func xor(_ a: Array<UInt8>, _ b: Array<UInt8>) -> Array<UInt8> {
     var xored = Array<UInt8>(repeating: 0, count: min(a.count, b.count))
     var xored = Array<UInt8>(repeating: 0, count: min(a.count, b.count))
-    for i in 0..<xored.count {
+    for i in 0 ..< xored.count {
         xored[i] = a[i] ^ b[i]
         xored[i] = a[i] ^ b[i]
     }
     }
     return xored
     return xored
@@ -82,5 +82,4 @@ func bitPadding(to data: inout Array<UInt8>, blockSize: Int, allowance: Int = 0)
     } else {
     } else {
         data += Array<UInt8>(repeating: 0, count: blockSize + max - 1 - (msgLength % blockSize))
         data += Array<UInt8>(repeating: 0, count: blockSize + max - 1 - (msgLength % blockSize))
     }
     }
-
 }
 }

+ 5 - 5
Sources/CryptoSwift/ZeroPadding.swift

@@ -9,10 +9,11 @@
 /// All the bytes that are required to be padded are padded with zero.
 /// All the bytes that are required to be padded are padded with zero.
 /// Zero padding may not be reversible if the original file ends with one or more zero bytes.
 /// Zero padding may not be reversible if the original file ends with one or more zero bytes.
 public struct ZeroPadding: Padding {
 public struct ZeroPadding: Padding {
+
     public init() {
     public init() {
     }
     }
 
 
-    public func add(to bytes: Array<UInt8>, blockSize:Int) -> Array<UInt8> {
+    public func add(to bytes: Array<UInt8>, blockSize: Int) -> Array<UInt8> {
         let paddingCount = blockSize - (bytes.count % blockSize)
         let paddingCount = blockSize - (bytes.count % blockSize)
         if paddingCount > 0 {
         if paddingCount > 0 {
             return bytes + Array<UInt8>(repeating: 0, count: paddingCount)
             return bytes + Array<UInt8>(repeating: 0, count: paddingCount)
@@ -20,13 +21,12 @@ public struct ZeroPadding: Padding {
         return bytes
         return bytes
     }
     }
 
 
-    public func remove(from bytes: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
+    public func remove(from bytes: Array<UInt8>, blockSize: Int?) -> Array<UInt8> {
         for (idx, value) in bytes.reversed().enumerated() {
         for (idx, value) in bytes.reversed().enumerated() {
             if value != 0 {
             if value != 0 {
-                return Array(bytes[0..<bytes.count - idx])
+                return Array(bytes[0 ..< bytes.count - idx])
             }
             }
         }
         }
-        return bytes;
-
+        return bytes
     }
     }
 }
 }

+ 139 - 137
Tests/CryptoSwiftTests/AESTests.swift

@@ -11,15 +11,15 @@ import Foundation
 
 
 final class AESTests: XCTestCase {
 final class AESTests: XCTestCase {
     // 128 bit key
     // 128 bit key
-    let aesKey:Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
+    let aesKey: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
 
 
     func testAESEncrypt2() {
     func testAESEncrypt2() {
-        let key:Array<UInt8>   = [0x36, 0x37, 0x39, 0x66, 0x62, 0x31, 0x64, 0x64, 0x66, 0x37, 0x64, 0x38, 0x31, 0x62, 0x65, 0x65];
-        let iv:Array<UInt8>    = [0x6b, 0x64, 0x66, 0x36, 0x37, 0x33, 0x39, 0x38, 0x44, 0x46, 0x37, 0x33, 0x38, 0x33, 0x66, 0x64]
-        let input:Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
-        
-        let expected:Array<UInt8> = [0xae,0x8c,0x59,0x95,0xb2,0x6f,0x8e,0x3d,0xb0,0x6f,0x0a,0xa5,0xfe,0xc4,0xf0,0xc2];
-        
+        let key: Array<UInt8> = [0x36, 0x37, 0x39, 0x66, 0x62, 0x31, 0x64, 0x64, 0x66, 0x37, 0x64, 0x38, 0x31, 0x62, 0x65, 0x65]
+        let iv: Array<UInt8> = [0x6b, 0x64, 0x66, 0x36, 0x37, 0x33, 0x39, 0x38, 0x44, 0x46, 0x37, 0x33, 0x38, 0x33, 0x66, 0x64]
+        let input: Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
+
+        let expected: Array<UInt8> = [0xae, 0x8c, 0x59, 0x95, 0xb2, 0x6f, 0x8e, 0x3d, 0xb0, 0x6f, 0x0a, 0xa5, 0xfe, 0xc4, 0xf0, 0xc2]
+
         let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: NoPadding())
         let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: NoPadding())
         let encrypted = try! aes.encrypt(input)
         let encrypted = try! aes.encrypt(input)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
@@ -30,20 +30,20 @@ final class AESTests: XCTestCase {
     func testAESEncrypt3() {
     func testAESEncrypt3() {
         let key = "679fb1ddf7d81bee"
         let key = "679fb1ddf7d81bee"
         let iv = "kdf67398DF7383fd"
         let iv = "kdf67398DF7383fd"
-        let input:Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
-        let expected:Array<UInt8> = [0xae,0x8c,0x59,0x95,0xb2,0x6f,0x8e,0x3d,0xb0,0x6f,0x0a,0xa5,0xfe,0xc4,0xf0,0xc2];
-        
+        let input: Array<UInt8> = [0x62, 0x72, 0x61, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
+        let expected: Array<UInt8> = [0xae, 0x8c, 0x59, 0x95, 0xb2, 0x6f, 0x8e, 0x3d, 0xb0, 0x6f, 0x0a, 0xa5, 0xfe, 0xc4, 0xf0, 0xc2]
+
         let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: NoPadding())
         let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: NoPadding())
         let encrypted = try! aes.encrypt(input)
         let encrypted = try! aes.encrypt(input)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
         XCTAssertEqual(decrypted, input, "decryption failed")
         XCTAssertEqual(decrypted, input, "decryption failed")
     }
     }
-    
+
     func testAESEncrypt() {
     func testAESEncrypt() {
-        let input:Array<UInt8> = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];
-        let expected:Array<UInt8> = [0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x4, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a];
-        
+        let input: Array<UInt8> = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]
+        let expected: Array<UInt8> = [0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x4, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a]
+
         let aes = try! AES(key: aesKey, blockMode: .ECB, padding: NoPadding())
         let aes = try! AES(key: aesKey, blockMode: .ECB, padding: NoPadding())
         let encrypted = try! aes.encrypt(input)
         let encrypted = try! aes.encrypt(input)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
@@ -52,12 +52,12 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptCBCNoPadding() {
     func testAESEncryptCBCNoPadding() {
-        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]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:Array<UInt8> = [0x76,0x49,0xab,0xac,0x81,0x19,0xb2,0x46,0xce,0xe9,0x8e,0x9b,0x12,0xe9,0x19,0x7d];
+        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]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
+        let expected: Array<UInt8> = [0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: NoPadding())
+        let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
@@ -65,12 +65,12 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptCBCWithPadding() {
     func testAESEncryptCBCWithPadding() {
-        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]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:Array<UInt8> = [0x76,0x49,0xab,0xac,0x81,0x19,0xb2,0x46,0xce,0xe9,0x8e,0x9b,0x12,0xe9,0x19,0x7d,0x89,0x64,0xe0,0xb1,0x49,0xc1,0x0b,0x7b,0x68,0x2e,0x6e,0x39,0xaa,0xeb,0x73,0x1c]
-        
-        let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
+        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]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
+        let expected: Array<UInt8> = [0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d, 0x89, 0x64, 0xe0, 0xb1, 0x49, 0xc1, 0x0b, 0x7b, 0x68, 0x2e, 0x6e, 0x39, 0xaa, 0xeb, 0x73, 0x1c]
+
+        let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
@@ -78,17 +78,17 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptCBCWithPaddingPartial() {
     func testAESEncryptCBCWithPaddingPartial() {
-        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]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
+        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]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
+        let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
 
 
         var ciphertext = Array<UInt8>()
         var ciphertext = Array<UInt8>()
         var encryptor = aes.makeEncryptor()
         var encryptor = aes.makeEncryptor()
-        ciphertext += try! encryptor.update(withBytes: plaintext[0..<8])
-        ciphertext += try! encryptor.update(withBytes: plaintext[8..<16])
-        ciphertext += try! encryptor.update(withBytes: plaintext[16..<32])
+        ciphertext += try! encryptor.update(withBytes: plaintext[0 ..< 8])
+        ciphertext += try! encryptor.update(withBytes: plaintext[8 ..< 16])
+        ciphertext += try! encryptor.update(withBytes: plaintext[16 ..< 32])
         ciphertext += try! encryptor.finish()
         ciphertext += try! encryptor.finish()
         XCTAssertEqual(try! aes.encrypt(plaintext), ciphertext, "encryption failed")
         XCTAssertEqual(try! aes.encrypt(plaintext), ciphertext, "encryption failed")
     }
     }
@@ -97,39 +97,39 @@ final class AESTests: XCTestCase {
         do {
         do {
             var ciphertext = Array<UInt8>()
             var ciphertext = Array<UInt8>()
             let plaintext = "Today Apple launched the open source Swift community, as well as amazing new tools and resources."
             let plaintext = "Today Apple launched the open source Swift community, as well as amazing new tools and resources."
-            let aes = try AES(key: "passwordpassword".utf8.map({$0}), iv: "drowssapdrowssap".utf8.map({$0}))
+            let aes = try AES(key: "passwordpassword".utf8.map({ $0 }), iv: "drowssapdrowssap".utf8.map({ $0 }))
             var encryptor = aes.makeEncryptor()
             var encryptor = aes.makeEncryptor()
 
 
-            ciphertext += try encryptor.update(withBytes: plaintext.utf8.map({$0}))
+            ciphertext += try encryptor.update(withBytes: plaintext.utf8.map({ $0 }))
             ciphertext += try encryptor.finish()
             ciphertext += try encryptor.finish()
-            XCTAssertEqual(try aes.encrypt(plaintext.utf8.map({$0})), ciphertext, "encryption failed")
+            XCTAssertEqual(try aes.encrypt(plaintext.utf8.map({ $0 })), ciphertext, "encryption failed")
         } catch {
         } catch {
             XCTAssert(false, "\(error)")
             XCTAssert(false, "\(error)")
         }
         }
     }
     }
 
 
     func testAESDecryptCBCWithPaddingPartial() {
     func testAESDecryptCBCWithPaddingPartial() {
-        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]
-        let ciphertext:Array<UInt8> = [118, 73, 171, 172, 129, 25, 178, 70, 206, 233, 142, 155, 18, 233, 25, 125, 76, 187, 200, 88, 117, 107, 53, 129, 37, 82, 158, 150, 152, 163, 143, 68, 169, 105, 137, 234, 93, 98, 239, 215, 41, 45, 51, 254, 138, 92, 251, 17]
+        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]
+        let ciphertext: Array<UInt8> = [118, 73, 171, 172, 129, 25, 178, 70, 206, 233, 142, 155, 18, 233, 25, 125, 76, 187, 200, 88, 117, 107, 53, 129, 37, 82, 158, 150, 152, 163, 143, 68, 169, 105, 137, 234, 93, 98, 239, 215, 41, 45, 51, 254, 138, 92, 251, 17]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
+        let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
         var plaintext = Array<UInt8>()
         var plaintext = Array<UInt8>()
         var decryptor = aes.makeDecryptor()
         var decryptor = aes.makeDecryptor()
-        plaintext += try! decryptor.update(withBytes: ciphertext[0..<8])
-        plaintext += try! decryptor.update(withBytes: ciphertext[8..<16])
-        plaintext += try! decryptor.update(withBytes: ciphertext[16..<32])
+        plaintext += try! decryptor.update(withBytes: ciphertext[0 ..< 8])
+        plaintext += try! decryptor.update(withBytes: ciphertext[8 ..< 16])
+        plaintext += try! decryptor.update(withBytes: ciphertext[16 ..< 32])
         plaintext += try! decryptor.finish()
         plaintext += try! decryptor.finish()
         XCTAssertEqual(try! aes.decrypt(ciphertext), plaintext, "encryption failed")
         XCTAssertEqual(try! aes.decrypt(ciphertext), plaintext, "encryption failed")
     }
     }
 
 
     func testAESEncryptCFB() {
     func testAESEncryptCFB() {
-        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]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:Array<UInt8> = [0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a];
-        
-        let aes = try! AES(key: key, iv:iv, blockMode: .CFB, padding: NoPadding())
+        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]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
+        let expected: Array<UInt8> = [0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a]
+
+        let aes = try! AES(key: key, iv: iv, blockMode: .CFB, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
@@ -147,12 +147,12 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptOFB128() {
     func testAESEncryptOFB128() {
-        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]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:Array<UInt8> = [0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a];
+        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]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
+        let expected: Array<UInt8> = [0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .OFB, padding: NoPadding())
+        let aes = try! AES(key: key, iv: iv, blockMode: .OFB, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
@@ -160,12 +160,12 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptOFB256() {
     func testAESEncryptOFB256() {
-        let key: Array<UInt8> = [0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4]
-        let iv: Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
-        let plaintext: Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:Array<UInt8> = [0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60];
+        let key: Array<UInt8> = [0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4]
+        let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
+        let expected: Array<UInt8> = [0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .OFB, padding: NoPadding())
+        let aes = try! AES(key: key, iv: iv, blockMode: .OFB, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
@@ -173,12 +173,12 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptPCBC256() {
     func testAESEncryptPCBC256() {
-        let key: Array<UInt8> = [0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4]
-        let iv: Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
-        let plaintext: Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:Array<UInt8> = [0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6];
+        let key: Array<UInt8> = [0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4]
+        let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
+        let expected: Array<UInt8> = [0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .PCBC, padding: NoPadding())
+        let aes = try! AES(key: key, iv: iv, blockMode: .PCBC, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         print(encrypted.toHexString())
         print(encrypted.toHexString())
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
@@ -187,12 +187,12 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptCTR() {
     func testAESEncryptCTR() {
-        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
-        let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:Array<UInt8> = [0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce]
-        
-        let aes = try! AES(key: key, iv:iv, blockMode: .CTR, padding: NoPadding())
+        let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
+        let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
+        let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce]
+
+        let aes = try! AES(key: key, iv: iv, blockMode: .CTR, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
@@ -200,12 +200,12 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESEncryptCTRIrregularLength() {
     func testAESEncryptCTRIrregularLength() {
-        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
-        let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01]
-        let expected:Array<UInt8> = [0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,0x37]
+        let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
+        let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x01]
+        let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x37]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .CTR, padding: NoPadding())
+        let aes = try! AES(key: key, iv: iv, blockMode: .CTR, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
         let decrypted = try! aes.decrypt(encrypted)
         let decrypted = try! aes.decrypt(encrypted)
@@ -214,66 +214,66 @@ final class AESTests: XCTestCase {
 
 
     // https://github.com/krzyzanowskim/CryptoSwift/pull/290
     // https://github.com/krzyzanowskim/CryptoSwift/pull/290
     func testAESDecryptCTRSeek() {
     func testAESDecryptCTRSeek() {
-        let key:Array<UInt8> = [0x52, 0x72, 0xb5, 0x9c, 0xab, 0x07, 0xc5, 0x01, 0x11, 0x7a, 0x39, 0xb6, 0x10, 0x35, 0x87, 0x02];
-        let iv:Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
-        var plaintext:Array<UInt8> = Array<UInt8>(repeating: 0, count: 6000)
-        
-        for i in 0..<plaintext.count / 6 {
-            let s = String(format: "%05d", i).utf8.map {$0}
-            plaintext[i * 6 + 0] = s[0];
-            plaintext[i * 6 + 1] = s[1];
-            plaintext[i * 6 + 2] = s[2];
-            plaintext[i * 6 + 3] = s[3];
-            plaintext[i * 6 + 4] = s[4];
-            plaintext[i * 6 + 5] = "|".utf8.first!;
+        let key: Array<UInt8> = [0x52, 0x72, 0xb5, 0x9c, 0xab, 0x07, 0xc5, 0x01, 0x11, 0x7a, 0x39, 0xb6, 0x10, 0x35, 0x87, 0x02]
+        let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
+        var plaintext: Array<UInt8> = Array<UInt8>(repeating: 0, count: 6000)
+
+        for i in 0 ..< plaintext.count / 6 {
+            let s = String(format: "%05d", i).utf8.map { $0 }
+            plaintext[i * 6 + 0] = s[0]
+            plaintext[i * 6 + 1] = s[1]
+            plaintext[i * 6 + 2] = s[2]
+            plaintext[i * 6 + 3] = s[3]
+            plaintext[i * 6 + 4] = s[4]
+            plaintext[i * 6 + 5] = "|".utf8.first!
         }
         }
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .CTR, padding: NoPadding())
+        let aes = try! AES(key: key, iv: iv, blockMode: .CTR, padding: NoPadding())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
 
 
         var decryptor = aes.makeDecryptor()
         var decryptor = aes.makeDecryptor()
         decryptor.seek(to: 2)
         decryptor.seek(to: 2)
-        var part1 = try! decryptor.update(withBytes: Array(encrypted[2..<5]))
+        var part1 = try! decryptor.update(withBytes: Array(encrypted[2 ..< 5]))
         part1 += try! decryptor.finish()
         part1 += try! decryptor.finish()
-        XCTAssertEqual(part1, Array(plaintext[2..<5]), "seek decryption failed")
+        XCTAssertEqual(part1, Array(plaintext[2 ..< 5]), "seek decryption failed")
 
 
         decryptor.seek(to: 1000)
         decryptor.seek(to: 1000)
-        var part2 = try! decryptor.update(withBytes: Array(encrypted[1000..<1200]))
+        var part2 = try! decryptor.update(withBytes: Array(encrypted[1000 ..< 1200]))
         part2 += try! decryptor.finish()
         part2 += try! decryptor.finish()
-        XCTAssertEqual(part2, Array(plaintext[1000..<1200]), "seek decryption failed")
+        XCTAssertEqual(part2, Array(plaintext[1000 ..< 1200]), "seek decryption failed")
 
 
         decryptor.seek(to: 5500)
         decryptor.seek(to: 5500)
-        var part3 = try! decryptor.update(withBytes: Array(encrypted[5500..<6000]))
+        var part3 = try! decryptor.update(withBytes: Array(encrypted[5500 ..< 6000]))
         part3 += try! decryptor.finish()
         part3 += try! decryptor.finish()
-        XCTAssertEqual(part3, Array(plaintext[5500..<6000]), "seek decryption failed")
+        XCTAssertEqual(part3, Array(plaintext[5500 ..< 6000]), "seek decryption failed")
 
 
         decryptor.seek(to: 0)
         decryptor.seek(to: 0)
-        var part4 = try! decryptor.update(withBytes: Array(encrypted[0..<80]))
+        var part4 = try! decryptor.update(withBytes: Array(encrypted[0 ..< 80]))
         part4 += try! decryptor.finish()
         part4 += try! decryptor.finish()
-        XCTAssertEqual(part4, Array(plaintext[0..<80]), "seek decryption failed")
+        XCTAssertEqual(part4, Array(plaintext[0 ..< 80]), "seek decryption failed")
     }
     }
-    
+
     // https://github.com/krzyzanowskim/CryptoSwift/pull/289
     // https://github.com/krzyzanowskim/CryptoSwift/pull/289
     func testAESEncryptCTRIrregularLengthIncrementalUpdate() {
     func testAESEncryptCTRIrregularLengthIncrementalUpdate() {
-        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
-        let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01,0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01]
-        let expected:Array<UInt8> = [0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0xd,0xb6,0xce,0x37,0x40,0xbd,0x82,0x85,0x5d,0x11,0xfc,0x8e,0x49,0x4a,0xa9,0xed,0x23,0xe0,0xb9,0x40,0x2d]
+        let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
+        let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x01, 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x01]
+        let expected: Array<UInt8> = [0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0xd, 0xb6, 0xce, 0x37, 0x40, 0xbd, 0x82, 0x85, 0x5d, 0x11, 0xfc, 0x8e, 0x49, 0x4a, 0xa9, 0xed, 0x23, 0xe0, 0xb9, 0x40, 0x2d]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .CTR, padding: NoPadding())
+        let aes = try! AES(key: key, iv: iv, blockMode: .CTR, padding: NoPadding())
         var encryptor = aes.makeEncryptor()
         var encryptor = aes.makeEncryptor()
         var encrypted = Array<UInt8>()
         var encrypted = Array<UInt8>()
-        encrypted += try! encryptor.update(withBytes: plaintext[0..<5])
-        encrypted += try! encryptor.update(withBytes: plaintext[5..<15])
-        encrypted += try! encryptor.update(withBytes: plaintext[15..<plaintext.count])
+        encrypted += try! encryptor.update(withBytes: plaintext[0 ..< 5])
+        encrypted += try! encryptor.update(withBytes: plaintext[5 ..< 15])
+        encrypted += try! encryptor.update(withBytes: plaintext[15 ..< plaintext.count])
         encrypted += try! encryptor.finish()
         encrypted += try! encryptor.finish()
         XCTAssertEqual(encrypted, expected, "encryption failed")
         XCTAssertEqual(encrypted, expected, "encryption failed")
-        
+
         var decryptor = aes.makeDecryptor()
         var decryptor = aes.makeDecryptor()
         var decrypted = Array<UInt8>()
         var decrypted = Array<UInt8>()
-        decrypted += try! decryptor.update(withBytes: expected[0..<5])
-        decrypted += try! decryptor.update(withBytes: expected[5..<15])
-        decrypted += try! decryptor.update(withBytes: expected[15..<plaintext.count])
+        decrypted += try! decryptor.update(withBytes: expected[0 ..< 5])
+        decrypted += try! decryptor.update(withBytes: expected[5 ..< 15])
+        decrypted += try! decryptor.update(withBytes: expected[15 ..< plaintext.count])
         decrypted += try! decryptor.finish()
         decrypted += try! decryptor.finish()
         XCTAssertEqual(decrypted, plaintext, "decryption failed")
         XCTAssertEqual(decrypted, plaintext, "decryption failed")
     }
     }
@@ -281,7 +281,7 @@ final class AESTests: XCTestCase {
     // https://github.com/krzyzanowskim/CryptoSwift/issues/298
     // https://github.com/krzyzanowskim/CryptoSwift/issues/298
     func testIssue298() {
     func testIssue298() {
         let encryptedValue = "47595cfa90f7b0b0e0d9d7240a2e035f7f4acde27d7ca778a7d8b05add32a0a92d945c0a59f7f0e029d7f2fbb258b2f0"
         let encryptedValue = "47595cfa90f7b0b0e0d9d7240a2e035f7f4acde27d7ca778a7d8b05add32a0a92d945c0a59f7f0e029d7f2fbb258b2f0"
-        let expected:Array<UInt8> = [55, 52, 98, 54, 53, 51, 101, 51, 54, 52, 51, 48, 100, 55, 97, 57, 99, 100, 57, 49, 97, 50, 52, 100, 57, 57, 52, 52, 98, 48, 51, 50, 79, 114, 70, 101, 99, 107, 114, 87, 111, 0, 0, 0, 0, 0, 0, 0]
+        let expected: Array<UInt8> = [55, 52, 98, 54, 53, 51, 101, 51, 54, 52, 51, 48, 100, 55, 97, 57, 99, 100, 57, 49, 97, 50, 52, 100, 57, 57, 52, 52, 98, 48, 51, 50, 79, 114, 70, 101, 99, 107, 114, 87, 111, 0, 0, 0, 0, 0, 0, 0]
         let key = "0123456789abcdef"
         let key = "0123456789abcdef"
         let iv = "fedcba9876543210"
         let iv = "fedcba9876543210"
 
 
@@ -295,13 +295,13 @@ final class AESTests: XCTestCase {
     }
     }
 
 
     func testAESWithWrongKey() {
     func testAESWithWrongKey() {
-        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
-        let key2:Array<UInt8> = [0x22,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x33];
-        let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
-        let plaintext:Array<UInt8> = [49, 46, 50, 50, 50, 51, 51, 51, 51]
+        let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
+        let key2: Array<UInt8> = [0x22, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x33]
+        let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]
+        let plaintext: Array<UInt8> = [49, 46, 50, 50, 50, 51, 51, 51, 51]
 
 
-        let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
-        let aes2 = try! AES(key: key2, iv:iv, blockMode: .CBC, padding: PKCS7())
+        let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
+        let aes2 = try! AES(key: key2, iv: iv, blockMode: .CBC, padding: PKCS7())
         let encrypted = try! aes.encrypt(plaintext)
         let encrypted = try! aes.encrypt(plaintext)
         let decrypted = try? aes2.decrypt(encrypted)
         let decrypted = try? aes2.decrypt(encrypted)
         XCTAssertTrue(decrypted! != plaintext, "failed")
         XCTAssertTrue(decrypted! != plaintext, "failed")
@@ -309,31 +309,33 @@ final class AESTests: XCTestCase {
 }
 }
 
 
 #if !CI
 #if !CI
-extension AESTests {
-    func testAESEncryptPerformance() {
-        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]
-        let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
-        let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
-        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
-            _ = try! aes.encrypt(message)
-        })
-    }
 
 
-    func testAESDecryptPerformance() {
-        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]
-        let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
-        let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
-        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
-            _ = try! aes.decrypt(message)
-        })
+    extension AESTests {
+
+        func testAESEncryptPerformance() {
+            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]
+            let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
+            let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
+            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+                _ = try! aes.encrypt(message)
+            })
+        }
+
+        func testAESDecryptPerformance() {
+            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]
+            let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
+            let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
+            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+                _ = try! aes.decrypt(message)
+            })
+        }
     }
     }
-}
 #endif
 #endif
 
 
-
 extension AESTests {
 extension AESTests {
+
     static func allTests() -> [(String, (AESTests) -> () -> Void)] {
     static func allTests() -> [(String, (AESTests) -> () -> Void)] {
         var tests = [
         var tests = [
             ("testAESEncrypt2", testAESEncrypt2),
             ("testAESEncrypt2", testAESEncrypt2),
@@ -354,13 +356,13 @@ extension AESTests {
             ("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
             ("testAESDecryptCTRSeek", testAESDecryptCTRSeek),
             ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
             ("testAESEncryptCTRIrregularLengthIncrementalUpdate", testAESEncryptCTRIrregularLengthIncrementalUpdate),
             ("testIssue298", testIssue298),
             ("testIssue298", testIssue298),
-            ("testAESWithWrongKey", testAESWithWrongKey)
+            ("testAESWithWrongKey", testAESWithWrongKey),
         ]
         ]
 
 
         #if !CI
         #if !CI
             tests += [
             tests += [
                 ("testAESEncryptPerformance", testAESEncryptPerformance),
                 ("testAESEncryptPerformance", testAESEncryptPerformance),
-                ("testAESDecryptPerformance", testAESDecryptPerformance)
+                ("testAESDecryptPerformance", testAESDecryptPerformance),
             ]
             ]
         #endif
         #endif
         return tests
         return tests

+ 33 - 33
Tests/CryptoSwiftTests/Access.swift

@@ -16,8 +16,8 @@ class Access: XCTestCase {
     let authenticator = HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1)
     let authenticator = HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1)
 
 
     func testChecksum() {
     func testChecksum() {
-        let _ = Checksum.crc32([1,2,3])
-        let _ = Checksum.crc16([1,2,3])
+        let _ = Checksum.crc32([1, 2, 3])
+        let _ = Checksum.crc16([1, 2, 3])
     }
     }
 
 
     func testRandomIV() {
     func testRandomIV() {
@@ -26,13 +26,13 @@ class Access: XCTestCase {
     }
     }
 
 
     func testDigest() {
     func testDigest() {
-        let _ = Digest.md5([1,2,3])
-        let _ = Digest.sha1([1,2,3])
-        let _ = Digest.sha224([1,2,3])
-        let _ = Digest.sha256([1,2,3])
-        let _ = Digest.sha384([1,2,3])
-        let _ = Digest.sha512([1,2,3])
-        let _ = Digest.sha3([1,2,3], variant: .sha224)
+        let _ = Digest.md5([1, 2, 3])
+        let _ = Digest.sha1([1, 2, 3])
+        let _ = Digest.sha224([1, 2, 3])
+        let _ = Digest.sha256([1, 2, 3])
+        let _ = Digest.sha384([1, 2, 3])
+        let _ = Digest.sha512([1, 2, 3])
+        let _ = Digest.sha3([1, 2, 3], variant: .sha224)
 
 
         let _ = SHA1().calculate(for: [0])
         let _ = SHA1().calculate(for: [0])
         let _ = SHA2(variant: .sha224).calculate(for: [0])
         let _ = SHA2(variant: .sha224).calculate(for: [0])
@@ -118,7 +118,7 @@ class Access: XCTestCase {
     }
     }
 
 
     func testDataExtension() {
     func testDataExtension() {
-        let data = Data(bytes: [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8])
+        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.checksum()
         let _ = data.md5()
         let _ = data.md5()
         let _ = data.sha1()
         let _ = data.sha1()
@@ -144,23 +144,23 @@ class Access: XCTestCase {
 
 
     func testPadding() {
     func testPadding() {
         // PKCS7
         // PKCS7
-        let _ = PKCS7().add(to: [1,2,3], blockSize: 16)
-        let _ = PKCS7().remove(from: [1,2,3], blockSize: 16)
+        let _ = PKCS7().add(to: [1, 2, 3], blockSize: 16)
+        let _ = PKCS7().remove(from: [1, 2, 3], blockSize: 16)
 
 
         // NoPadding
         // NoPadding
-        let _ = NoPadding().add(to: [1,2,3], blockSize: 16)
-        let _ = NoPadding().remove(from: [1,2,3], blockSize: 16)
+        let _ = NoPadding().add(to: [1, 2, 3], blockSize: 16)
+        let _ = NoPadding().remove(from: [1, 2, 3], blockSize: 16)
 
 
         // ZeroPadding
         // ZeroPadding
-        let _ = ZeroPadding().add(to: [1,2,3], blockSize: 16)
-        let _ = ZeroPadding().remove(from: [1,2,3], blockSize: 16)
+        let _ = ZeroPadding().add(to: [1, 2, 3], blockSize: 16)
+        let _ = ZeroPadding().remove(from: [1, 2, 3], blockSize: 16)
     }
     }
 
 
     func testPBKDF() {
     func testPBKDF() {
         do {
         do {
             let _ = PKCS5.PBKDF1.Variant.md5
             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()
+            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 {
         } catch {
             XCTFail(error.localizedDescription)
             XCTFail(error.localizedDescription)
         }
         }
@@ -168,8 +168,8 @@ class Access: XCTestCase {
 
 
     func testAuthenticators() {
     func testAuthenticators() {
         do {
         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])
+            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 {
         } catch {
             XCTFail(error.localizedDescription)
             XCTFail(error.localizedDescription)
         }
         }
@@ -179,17 +179,17 @@ class Access: XCTestCase {
         do {
         do {
             let aes = try AES(key: "secret0key000000", iv: "0123456789012345")
             let aes = try AES(key: "secret0key000000", iv: "0123456789012345")
             var encryptor = aes.makeEncryptor()
             var encryptor = aes.makeEncryptor()
-            let _ = try encryptor.update(withBytes: [1,2,3])
+            let _ = try encryptor.update(withBytes: [1, 2, 3])
             let _ = try encryptor.finish()
             let _ = try encryptor.finish()
 
 
             var decryptor = aes.makeDecryptor()
             var decryptor = aes.makeDecryptor()
-            let _ = try decryptor.update(withBytes: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
+            let _ = try decryptor.update(withBytes: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
             let _ = try decryptor.finish()
             let _ = try decryptor.finish()
 
 
-            let enc = try aes.encrypt([1,2,3])
+            let enc = try aes.encrypt([1, 2, 3])
             let _ = try aes.decrypt(enc)
             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 _ = 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.Variant.aes128
             let _ = AES.blockSize
             let _ = AES.blockSize
@@ -201,8 +201,8 @@ class Access: XCTestCase {
     func testRabbit() {
     func testRabbit() {
         do {
         do {
             let rabbit = try Rabbit(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
             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)
+            let enc = rabbit.encrypt([1, 2, 3])
+            let _ = rabbit.decrypt(enc)
 
 
             XCTAssertThrowsError(try Rabbit(key: "123"))
             XCTAssertThrowsError(try Rabbit(key: "123"))
 
 
@@ -215,13 +215,13 @@ class Access: XCTestCase {
     }
     }
 
 
     func testChaCha20() {
     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]
+        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 {
         do {
             let _ = ChaCha20.blockSize
             let _ = ChaCha20.blockSize
             let chacha20 = try ChaCha20(key: key, iv: iv)
             let chacha20 = try ChaCha20(key: key, iv: iv)
-            let enc = try chacha20.encrypt([1,3,4])
+            let enc = try chacha20.encrypt([1, 3, 4])
             let _ = try chacha20.decrypt(enc)
             let _ = try chacha20.decrypt(enc)
 
 
             XCTAssertThrowsError(try ChaCha20(key: "123", iv: "12345678"))
             XCTAssertThrowsError(try ChaCha20(key: "123", iv: "12345678"))
@@ -235,10 +235,10 @@ class Access: XCTestCase {
     }
     }
 
 
     func testUpdatable() {
     func testUpdatable() {
-        // TODO
+        // TODO: 
     }
     }
-    
-    static let allTests =  [
+
+    static let allTests = [
         ("testChecksum", testChecksum),
         ("testChecksum", testChecksum),
         ("testDigest", testDigest),
         ("testDigest", testDigest),
         ("testArrayExtension", testArrayExtension),
         ("testArrayExtension", testArrayExtension),
@@ -258,6 +258,6 @@ class Access: XCTestCase {
         ("testRabbit", testRabbit),
         ("testRabbit", testRabbit),
         ("testChaCha20", testChaCha20),
         ("testChaCha20", testChaCha20),
         ("testUpdatable", testUpdatable),
         ("testUpdatable", testUpdatable),
-        ("testRandomIV", testRandomIV)
+        ("testRandomIV", testRandomIV),
     ]
     ]
 }
 }

+ 50 - 48
Tests/CryptoSwiftTests/ChaCha20Tests.swift

@@ -12,41 +12,41 @@ import Foundation
 final class ChaCha20Tests: XCTestCase {
 final class ChaCha20Tests: XCTestCase {
 
 
     func testChaCha20() {
     func testChaCha20() {
-        let keys:[Array<UInt8>] = [
-            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
-            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01],
-            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
-            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
-            [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F]
+        let keys: [Array<UInt8>] = [
+            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
+            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
+            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
+            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
+            [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F],
         ]
         ]
-        
-        let ivs:[Array<UInt8>] = [
-            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
-            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
-            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01],
-            [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
-            [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07]
+
+        let ivs: [Array<UInt8>] = [
+            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
+            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
+            [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
+            [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
+            [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07],
         ]
         ]
-        
+
         let expectedHexes = [
         let expectedHexes = [
             "76B8E0ADA0F13D90405D6AE55386BD28BDD219B8A08DED1AA836EFCC8B770DC7DA41597C5157488D7724E03FB8D84A376A43B8F41518A11CC387B669B2EE6586",
             "76B8E0ADA0F13D90405D6AE55386BD28BDD219B8A08DED1AA836EFCC8B770DC7DA41597C5157488D7724E03FB8D84A376A43B8F41518A11CC387B669B2EE6586",
             "4540F05A9F1FB296D7736E7B208E3C96EB4FE1834688D2604F450952ED432D41BBE2A0B6EA7566D2A5D1E7E20D42AF2C53D792B1C43FEA817E9AD275AE546963",
             "4540F05A9F1FB296D7736E7B208E3C96EB4FE1834688D2604F450952ED432D41BBE2A0B6EA7566D2A5D1E7E20D42AF2C53D792B1C43FEA817E9AD275AE546963",
             "DE9CBA7BF3D69EF5E786DC63973F653A0B49E015ADBFF7134FCB7DF137821031E85A050278A7084527214F73EFC7FA5B5277062EB7A0433E445F41E3",
             "DE9CBA7BF3D69EF5E786DC63973F653A0B49E015ADBFF7134FCB7DF137821031E85A050278A7084527214F73EFC7FA5B5277062EB7A0433E445F41E3",
             "EF3FDFD6C61578FBF5CF35BD3DD33B8009631634D21E42AC33960BD138E50D32111E4CAF237EE53CA8AD6426194A88545DDC497A0B466E7D6BBDB0041B2F586B",
             "EF3FDFD6C61578FBF5CF35BD3DD33B8009631634D21E42AC33960BD138E50D32111E4CAF237EE53CA8AD6426194A88545DDC497A0B466E7D6BBDB0041B2F586B",
-            "F798A189F195E66982105FFB640BB7757F579DA31602FC93EC01AC56F85AC3C134A4547B733B46413042C9440049176905D3BE59EA1C53F15916155C2BE8241A38008B9A26BC35941E2444177C8ADE6689DE95264986D95889FB60E84629C9BD9A5ACB1CC118BE563EB9B3A4A472F82E09A7E778492B562EF7130E88DFE031C79DB9D4F7C7A899151B9A475032B63FC385245FE054E3DD5A97A5F576FE064025D3CE042C566AB2C507B138DB853E3D6959660996546CC9C4A6EAFDC777C040D70EAF46F76DAD3979E5C5360C3317166A1C894C94A371876A94DF7628FE4EAAF2CCB27D5AAAE0AD7AD0F9D4B6AD3B54098746D4524D38407A6DEB3AB78FAB78C9"
+            "F798A189F195E66982105FFB640BB7757F579DA31602FC93EC01AC56F85AC3C134A4547B733B46413042C9440049176905D3BE59EA1C53F15916155C2BE8241A38008B9A26BC35941E2444177C8ADE6689DE95264986D95889FB60E84629C9BD9A5ACB1CC118BE563EB9B3A4A472F82E09A7E778492B562EF7130E88DFE031C79DB9D4F7C7A899151B9A475032B63FC385245FE054E3DD5A97A5F576FE064025D3CE042C566AB2C507B138DB853E3D6959660996546CC9C4A6EAFDC777C040D70EAF46F76DAD3979E5C5360C3317166A1C894C94A371876A94DF7628FE4EAAF2CCB27D5AAAE0AD7AD0F9D4B6AD3B54098746D4524D38407A6DEB3AB78FAB78C9",
         ]
         ]
 
 
-        for idx in 0..<keys.count {            
+        for idx in 0 ..< keys.count {
             let expectedHex = expectedHexes[idx]
             let expectedHex = expectedHexes[idx]
             let message = Array<UInt8>(repeating: 0, count: (expectedHex.characters.count / 2))
             let message = Array<UInt8>(repeating: 0, count: (expectedHex.characters.count / 2))
-            
+
             do {
             do {
                 let encrypted = try ChaCha20(key: keys[idx], iv: ivs[idx]).encrypt(message)
                 let encrypted = try ChaCha20(key: keys[idx], iv: ivs[idx]).encrypt(message)
                 let decrypted = try ChaCha20(key: keys[idx], iv: ivs[idx]).decrypt(encrypted)
                 let decrypted = try ChaCha20(key: keys[idx], iv: ivs[idx]).decrypt(encrypted)
-                XCTAssertEqual(message, decrypted, "ChaCha20 decryption failed");
-                
+                XCTAssertEqual(message, decrypted, "ChaCha20 decryption failed")
+
                 // check extension
                 // check extension
-                let messageData = Data(bytes: message);
+                let messageData = Data(bytes: message)
                 let encrypted2 = try messageData.encrypt(cipher: ChaCha20(key: keys[idx], iv: ivs[idx]))
                 let encrypted2 = try messageData.encrypt(cipher: ChaCha20(key: keys[idx], iv: ivs[idx]))
                 XCTAssertNotNil(encrypted2, "")
                 XCTAssertNotNil(encrypted2, "")
                 XCTAssertEqual(Data(bytes: encrypted), encrypted2, "ChaCha20 extension failed")
                 XCTAssertEqual(Data(bytes: encrypted), encrypted2, "ChaCha20 extension failed")
@@ -61,9 +61,9 @@ final class ChaCha20Tests: XCTestCase {
     }
     }
 
 
     func testVector1Py() {
     func testVector1Py() {
-        let key:Array<UInt8> = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
-        let iv:Array<UInt8>  = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
-        let expected:Array<UInt8> = [0x76,0xB8,0xE0,0xAD,0xA0,0xF1,0x3D,0x90,0x40,0x5D,0x6A,0xE5,0x53,0x86,0xBD,0x28,0xBD,0xD2,0x19,0xB8,0xA0,0x8D,0xED,0x1A,0xA8,0x36,0xEF,0xCC,0x8B,0x77,0x0D,0xC7,0xDA,0x41,0x59,0x7C,0x51,0x57,0x48,0x8D,0x77,0x24,0xE0,0x3F,0xB8,0xD8,0x4A,0x37,0x6A,0x43,0xB8,0xF4,0x15,0x18,0xA1,0x1C,0xC3,0x87,0xB6,0x69,0xB2,0xEE,0x65,0x86]
+        let key: Array<UInt8> = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
+        let iv: Array<UInt8> = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
+        let expected: Array<UInt8> = [0x76, 0xB8, 0xE0, 0xAD, 0xA0, 0xF1, 0x3D, 0x90, 0x40, 0x5D, 0x6A, 0xE5, 0x53, 0x86, 0xBD, 0x28, 0xBD, 0xD2, 0x19, 0xB8, 0xA0, 0x8D, 0xED, 0x1A, 0xA8, 0x36, 0xEF, 0xCC, 0x8B, 0x77, 0x0D, 0xC7, 0xDA, 0x41, 0x59, 0x7C, 0x51, 0x57, 0x48, 0x8D, 0x77, 0x24, 0xE0, 0x3F, 0xB8, 0xD8, 0x4A, 0x37, 0x6A, 0x43, 0xB8, 0xF4, 0x15, 0x18, 0xA1, 0x1C, 0xC3, 0x87, 0xB6, 0x69, 0xB2, 0xEE, 0x65, 0x86]
         let message = Array<UInt8>(repeating: 0, count: expected.count)
         let message = Array<UInt8>(repeating: 0, count: expected.count)
 
 
         do {
         do {
@@ -75,18 +75,18 @@ final class ChaCha20Tests: XCTestCase {
     }
     }
 
 
     func testChaCha20EncryptPartial() {
     func testChaCha20EncryptPartial() {
-        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]
-        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
+        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]
+        let plaintext: Array<UInt8> = [0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]
 
 
         do {
         do {
-            let cipher = try ChaCha20(key: key, iv:iv)
+            let cipher = try ChaCha20(key: key, iv: iv)
 
 
             var ciphertext = Array<UInt8>()
             var ciphertext = Array<UInt8>()
             var encryptor = cipher.makeEncryptor()
             var encryptor = cipher.makeEncryptor()
-            ciphertext += try encryptor.update(withBytes: Array(plaintext[0..<8]))
-            ciphertext += try encryptor.update(withBytes: Array(plaintext[8..<16]))
-            ciphertext += try encryptor.update(withBytes: Array(plaintext[16..<32]))
+            ciphertext += try encryptor.update(withBytes: Array(plaintext[0 ..< 8]))
+            ciphertext += try encryptor.update(withBytes: Array(plaintext[8 ..< 16]))
+            ciphertext += try encryptor.update(withBytes: Array(plaintext[16 ..< 32]))
             ciphertext += try encryptor.finish()
             ciphertext += try encryptor.finish()
             XCTAssertEqual(try cipher.encrypt(plaintext), ciphertext, "encryption failed")
             XCTAssertEqual(try cipher.encrypt(plaintext), ciphertext, "encryption failed")
         } catch {
         } catch {
@@ -96,35 +96,37 @@ final class ChaCha20Tests: XCTestCase {
 }
 }
 
 
 #if !CI
 #if !CI
-extension ChaCha20Tests {
-    func testChaCha20Performance() {
-        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]
-        let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
-        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
-            do {
-                let _ = try ChaCha20(key: key, iv: iv).encrypt(message)
-            } catch {
-                XCTFail()
-            }
-            self.stopMeasuring()
-        })
+
+    extension ChaCha20Tests {
+
+        func testChaCha20Performance() {
+            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]
+            let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
+            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
+                do {
+                    let _ = try ChaCha20(key: key, iv: iv).encrypt(message)
+                } catch {
+                    XCTFail()
+                }
+                self.stopMeasuring()
+            })
+        }
     }
     }
-}
 #endif
 #endif
 
 
-
 extension ChaCha20Tests {
 extension ChaCha20Tests {
+
     static func allTests() -> [(String, (ChaCha20Tests) -> () -> Void)] {
     static func allTests() -> [(String, (ChaCha20Tests) -> () -> Void)] {
         var tests = [
         var tests = [
             ("testChaCha20", testChaCha20),
             ("testChaCha20", testChaCha20),
             ("testVector1Py", testVector1Py),
             ("testVector1Py", testVector1Py),
-            ("testChaCha20EncryptPartial", testChaCha20EncryptPartial)
-            ]
+            ("testChaCha20EncryptPartial", testChaCha20EncryptPartial),
+        ]
 
 
         #if !CI
         #if !CI
             tests += [
             tests += [
-                ("testChaCha20Performance", testChaCha20Performance)
+                ("testChaCha20Performance", testChaCha20Performance),
             ]
             ]
         #endif
         #endif
 
 

+ 36 - 34
Tests/CryptoSwiftTests/DigestTests.swift

@@ -15,7 +15,7 @@ import Foundation
 final class DigestTests: XCTestCase {
 final class DigestTests: XCTestCase {
 
 
     func testMD5() {
     func testMD5() {
-        XCTAssertEqual("123".md5(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed");
+        XCTAssertEqual("123".md5(), "202cb962ac59075b964b07152d234b70", "MD5 calculation failed")
         XCTAssertEqual("".md5(), "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
         XCTAssertEqual("".md5(), "d41d8cd98f00b204e9800998ecf8427e", "MD5 calculation failed")
         XCTAssertEqual("a".md5(), "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
         XCTAssertEqual("a".md5(), "0cc175b9c0f1b6a831c399e269772661", "MD5 calculation failed")
         XCTAssertEqual("abc".md5(), "900150983cd24fb0d6963f7d28e17f72", "MD5 calculation failed")
         XCTAssertEqual("abc".md5(), "900150983cd24fb0d6963f7d28e17f72", "MD5 calculation failed")
@@ -38,7 +38,6 @@ final class DigestTests: XCTestCase {
         XCTAssertEqual("1477304791&1XpnGSRhOlZz2etXMeOdfNaHILTjW16U&8mpBBbzwsg".sha1(), "c106aa0a98606294ee35fd2d937e928ebb5339e0", "Failed")
         XCTAssertEqual("1477304791&1XpnGSRhOlZz2etXMeOdfNaHILTjW16U&8mpBBbzwsg".sha1(), "c106aa0a98606294ee35fd2d937e928ebb5339e0", "Failed")
     }
     }
 
 
-
     func testSHA2() {
     func testSHA2() {
         XCTAssertEqual(SHA2(variant: .sha224).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
         XCTAssertEqual(SHA2(variant: .sha224).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
         XCTAssertEqual(SHA2(variant: .sha256).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
         XCTAssertEqual(SHA2(variant: .sha256).calculate(for: Array<UInt8>(hex: "616263")).toHexString(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
@@ -66,7 +65,7 @@ final class DigestTests: XCTestCase {
         XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha512(), Array<UInt8>(hex: "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
         XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha512(), Array<UInt8>(hex: "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
 
 
         XCTAssertEqual("Rosetta code".sha256(), "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
         XCTAssertEqual("Rosetta code".sha256(), "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf", "SHA256 calculation failed")
-        XCTAssertEqual("The quick brown fox jumps over the lazy dog.".sha384(), "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed");
+        XCTAssertEqual("The quick brown fox jumps over the lazy dog.".sha384(), "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7", "SHA384 calculation failed")
     }
     }
 
 
     func testSHA3() {
     func testSHA3() {
@@ -95,10 +94,10 @@ final class DigestTests: XCTestCase {
         XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha384), Array<UInt8>(hex: "eee9e24d78c1855337983451df97c8ad9eedf256c6334f8e948d252d5e0e76847aa0774ddb90a842190d2c558b4b8340"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
         XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha384), Array<UInt8>(hex: "eee9e24d78c1855337983451df97c8ad9eedf256c6334f8e948d252d5e0e76847aa0774ddb90a842190d2c558b4b8340"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
         XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha512), Array<UInt8>(hex: "3c3a876da14034ab60627c077bb98f7e120a2a5370212dffb3385a18d4f38859ed311d0a9d5141ce9cc5c66ee689b266a8aa18ace8282a0e0db596c90b0a7b87"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
         XCTAssertEqual(Array<UInt8>(repeating: 0x61, count: 1_000_000).sha3(.sha512), Array<UInt8>(hex: "3c3a876da14034ab60627c077bb98f7e120a2a5370212dffb3385a18d4f38859ed311d0a9d5141ce9cc5c66ee689b266a8aa18ace8282a0e0db596c90b0a7b87"), "One million (1,000,000) repetitions of the character 'a' (0x61)")
     }
     }
-    
+
     func testMD5Data() {
     func testMD5Data() {
         let data = [0x31, 0x32, 0x33] as Array<UInt8> // "1", "2", "3"
         let data = [0x31, 0x32, 0x33] as Array<UInt8> // "1", "2", "3"
-        XCTAssertEqual(Digest.md5(data), [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70], "MD5 calculation failed");
+        XCTAssertEqual(Digest.md5(data), [0x20, 0x2c, 0xb9, 0x62, 0xac, 0x59, 0x07, 0x5b, 0x96, 0x4b, 0x07, 0x15, 0x2d, 0x23, 0x4b, 0x70], "MD5 calculation failed")
     }
     }
 
 
     func testMD5Updates() {
     func testMD5Updates() {
@@ -107,7 +106,7 @@ final class DigestTests: XCTestCase {
             let _ = try hash.update(withBytes: [0x31, 0x32])
             let _ = try hash.update(withBytes: [0x31, 0x32])
             let _ = try hash.update(withBytes: [0x33])
             let _ = try hash.update(withBytes: [0x33])
             let result = try hash.finish()
             let result = try hash.finish()
-            XCTAssertEqual(result, [0x20,0x2c,0xb9,0x62,0xac,0x59,0x07,0x5b,0x96,0x4b,0x07,0x15,0x2d,0x23,0x4b,0x70])
+            XCTAssertEqual(result, [0x20, 0x2c, 0xb9, 0x62, 0xac, 0x59, 0x07, 0x5b, 0x96, 0x4b, 0x07, 0x15, 0x2d, 0x23, 0x4b, 0x70])
         } catch {
         } catch {
             XCTFail()
             XCTFail()
         }
         }
@@ -137,53 +136,56 @@ final class DigestTests: XCTestCase {
     }
     }
 
 
     func testCRC32() {
     func testCRC32() {
-        let data:Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
-        XCTAssertEqual(data.crc32(seed: nil).toHexString(), "884863d2", "CRC32 calculation failed");
+        let data: Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
+        XCTAssertEqual(data.crc32(seed: nil).toHexString(), "884863d2", "CRC32 calculation failed")
 
 
-        XCTAssertEqual("".crc32(seed: nil), "00000000", "CRC32 calculation failed");
+        XCTAssertEqual("".crc32(seed: nil), "00000000", "CRC32 calculation failed")
     }
     }
-    
+
     func testCRC32NotReflected() {
     func testCRC32NotReflected() {
-        let bytes : Array<UInt8> = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39]
-        let data:Data = Data(bytes: UnsafePointer<UInt8>(bytes), count: bytes.count)
-        XCTAssertEqual(data.crc32(seed: nil, reflect: false).toHexString(), "fc891918", "CRC32 (with reflection) calculation failed");
+        let bytes: Array<UInt8> = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39]
+        let data: Data = Data(bytes: UnsafePointer<UInt8>(bytes), count: bytes.count)
+        XCTAssertEqual(data.crc32(seed: nil, reflect: false).toHexString(), "fc891918", "CRC32 (with reflection) calculation failed")
 
 
-        XCTAssertEqual("".crc32(seed: nil, reflect: false), "00000000", "CRC32 (with reflection) calculation failed");
+        XCTAssertEqual("".crc32(seed: nil, reflect: false), "00000000", "CRC32 (with reflection) calculation failed")
     }
     }
-    
+
     func testCRC16() {
     func testCRC16() {
-        let result = Checksum.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")
         XCTAssert(result == 0xBB3D, "CRC16 failed")
     }
     }
-    
+
     func testChecksum() {
     func testChecksum() {
-        let data:Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
+        let data: Data = Data(bytes: UnsafePointer<UInt8>([49, 50, 51] as Array<UInt8>), count: 3)
         XCTAssert(data.checksum() == 0x96, "Invalid checksum")
         XCTAssert(data.checksum() == 0x96, "Invalid checksum")
     }
     }
 }
 }
 
 
 #if !CI
 #if !CI
-extension DigestTests {
-    func testMD5Performance() {
-        self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
-            let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
-            self.startMeasuring()
-            _ = Digest.md5(arr)
-            self.stopMeasuring()
-        })
-    }
 
 
-    func testSHA1Performance() {
-        self.measure {
-            for _ in 0..<10_000 {
-                let _ = "".sha1()
+    extension DigestTests {
+
+        func testMD5Performance() {
+            self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
+                let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
+                self.startMeasuring()
+                _ = Digest.md5(arr)
+                self.stopMeasuring()
+            })
+        }
+
+        func testSHA1Performance() {
+            self.measure {
+                for _ in 0 ..< 10_000 {
+                    let _ = "".sha1()
+                }
             }
             }
         }
         }
     }
     }
-}
 #endif
 #endif
 
 
 extension DigestTests {
 extension DigestTests {
+
     static func allTests() -> [(String, (DigestTests) -> () -> Void)] {
     static func allTests() -> [(String, (DigestTests) -> () -> Void)] {
         var tests = [
         var tests = [
             ("testMD5", testMD5),
             ("testMD5", testMD5),
@@ -197,13 +199,13 @@ extension DigestTests {
             ("testCRC32", testCRC32),
             ("testCRC32", testCRC32),
             ("testCRC32NotReflected", testCRC32NotReflected),
             ("testCRC32NotReflected", testCRC32NotReflected),
             ("testCRC15", testCRC16),
             ("testCRC15", testCRC16),
-            ("testChecksum", testChecksum)
+            ("testChecksum", testChecksum),
         ]
         ]
 
 
         #if !CI
         #if !CI
             tests += [
             tests += [
                 ("testMD5Performance", testMD5Performance),
                 ("testMD5Performance", testMD5Performance),
-                ("testSHA1Performance", testSHA1Performance)
+                ("testSHA1Performance", testSHA1Performance),
             ]
             ]
         #endif
         #endif
 
 

+ 5 - 4
Tests/CryptoSwiftTests/Error+Extension.swift

@@ -9,9 +9,10 @@
 import Foundation
 import Foundation
 
 
 #if !_runtime(_ObjC)
 #if !_runtime(_ObjC)
-extension Error {
-    var localizedDescription: String {
-        return "\(self)"
+
+    extension Error {
+        var localizedDescription: String {
+            return "\(self)"
+        }
     }
     }
-}
 #endif
 #endif

+ 21 - 18
Tests/CryptoSwiftTests/ExtensionsTest.swift

@@ -13,21 +13,21 @@ final class ExtensionsTest: XCTestCase {
 
 
     func testBytes() {
     func testBytes() {
         let size = MemoryLayout<UInt32>.size // 32 or 64  bit
         let size = MemoryLayout<UInt32>.size // 32 or 64  bit
-        
-        let i:UInt32 = 1024
+
+        let i: UInt32 = 1024
         var bytes = i.bytes()
         var bytes = i.bytes()
         XCTAssertTrue(bytes.count == size, "Invalid bytes length =  \(bytes.count)")
         XCTAssertTrue(bytes.count == size, "Invalid bytes length =  \(bytes.count)")
-        
+
         // test padding
         // test padding
         bytes = i.bytes(totalBytes: 16)
         bytes = i.bytes(totalBytes: 16)
         XCTAssertTrue(bytes.count == 16, "Invalid return type \(bytes.count)")
         XCTAssertTrue(bytes.count == 16, "Invalid return type \(bytes.count)")
         XCTAssertTrue(bytes[14] == 4, "Invalid return type \(bytes.count)")
         XCTAssertTrue(bytes[14] == 4, "Invalid return type \(bytes.count)")
     }
     }
-    
+
     func testToUInt32Array() {
     func testToUInt32Array() {
-        let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
+        let chunk: ArraySlice<UInt8> = [1, 1, 1, 7, 2, 3, 4, 5]
         let result = chunk.toUInt32Array()
         let result = chunk.toUInt32Array()
-        
+
         XCTAssert(result.count == 2, "Invalid conversion")
         XCTAssert(result.count == 2, "Invalid conversion")
         XCTAssert(result[0] == 117506305, "Invalid conversion")
         XCTAssert(result[0] == 117506305, "Invalid conversion")
         XCTAssert(result[1] == 84148994, "Invalid conversion")
         XCTAssert(result[1] == 84148994, "Invalid conversion")
@@ -55,7 +55,7 @@ final class ExtensionsTest: XCTestCase {
 
 
     func testArrayInitHex() {
     func testArrayInitHex() {
         let bytes = Array<UInt8>(hex: "0xb1b1b2b2")
         let bytes = Array<UInt8>(hex: "0xb1b1b2b2")
-        XCTAssertEqual(bytes, [177,177,178,178])
+        XCTAssertEqual(bytes, [177, 177, 178, 178])
 
 
         let str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
         let str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
         let array = Array<UInt8>(hex: str)
         let array = Array<UInt8>(hex: str)
@@ -65,19 +65,22 @@ final class ExtensionsTest: XCTestCase {
 }
 }
 
 
 #if !CI
 #if !CI
-extension ExtensionsTest {
-    func testArrayChunksPerformance() {
-        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
-            let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
-            self.startMeasuring()
-            _ = message.chunks(size: AES.blockSize)
-            self.stopMeasuring()
-        })
+
+    extension ExtensionsTest {
+
+        func testArrayChunksPerformance() {
+            measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
+                let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
+                self.startMeasuring()
+                _ = message.chunks(size: AES.blockSize)
+                self.stopMeasuring()
+            })
+        }
     }
     }
-}
 #endif
 #endif
 
 
 extension ExtensionsTest {
 extension ExtensionsTest {
+
     static func allTests() -> [(String, (ExtensionsTest) -> () -> Void)] {
     static func allTests() -> [(String, (ExtensionsTest) -> () -> Void)] {
         var tests = [
         var tests = [
             ("testBytes", testBytes),
             ("testBytes", testBytes),
@@ -85,12 +88,12 @@ extension ExtensionsTest {
             ("testDataInit", testDataInit),
             ("testDataInit", testDataInit),
             ("testStringEncrypt", testStringEncrypt),
             ("testStringEncrypt", testStringEncrypt),
             ("testStringDecryptBase64", testStringDecryptBase64),
             ("testStringDecryptBase64", testStringDecryptBase64),
-            ("testArrayInitHex", testArrayInitHex)
+            ("testArrayInitHex", testArrayInitHex),
         ]
         ]
 
 
         #if !CI
         #if !CI
             tests += [
             tests += [
-                ("testArrayChunksPerformance", testArrayChunksPerformance)
+                ("testArrayChunksPerformance", testArrayChunksPerformance),
             ]
             ]
         #endif
         #endif
         return tests
         return tests

+ 21 - 22
Tests/CryptoSwiftTests/HMACTests.swift

@@ -9,57 +9,56 @@ import XCTest
 @testable import CryptoSwift
 @testable import CryptoSwift
 
 
 final class HMACTests: XCTestCase {
 final class HMACTests: XCTestCase {
- 
+
     func testMD5() {
     func testMD5() {
-        let key:Array<UInt8> = []
-        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 key: Array<UInt8> = []
+        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! HMAC(key: key, variant: .md5).authenticate(msg)
         let hmac = try! HMAC(key: key, variant: .md5).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
     }
-    
+
     func testSHA1() {
     func testSHA1() {
-        XCTAssertEqual(try HMAC(key: [], variant: .sha1).authenticate([]), Array<UInt8>(hex:"fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"))
+        XCTAssertEqual(try HMAC(key: [], variant: .sha1).authenticate([]), Array<UInt8>(hex: "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"))
         // echo -n "test" | openssl sha1 -hmac 'test'
         // echo -n "test" | openssl sha1 -hmac 'test'
-        XCTAssertEqual(try HMAC(key: Array<UInt8>(hex: "74657374"), variant: .sha1).authenticate(Array<UInt8>(hex: "74657374")), Array<UInt8>(hex:"0c94515c15e5095b8a87a50ba0df3bf38ed05fe6"))
+        XCTAssertEqual(try HMAC(key: Array<UInt8>(hex: "74657374"), variant: .sha1).authenticate(Array<UInt8>(hex: "74657374")), Array<UInt8>(hex: "0c94515c15e5095b8a87a50ba0df3bf38ed05fe6"))
         // echo -n "test" | openssl sha1 -hmac '0123456789012345678901234567890123456789012345678901234567890123'
         // echo -n "test" | openssl sha1 -hmac '0123456789012345678901234567890123456789012345678901234567890123'
-        XCTAssertEqual(try HMAC(key: Array<UInt8>(hex: "30313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233"), variant: .sha1).authenticate(Array<UInt8>(hex: "74657374")), Array<UInt8>(hex:"23cea58b0c484ed005434938ee70a938d7524e91"))
+        XCTAssertEqual(try HMAC(key: Array<UInt8>(hex: "30313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233"), variant: .sha1).authenticate(Array<UInt8>(hex: "74657374")), Array<UInt8>(hex: "23cea58b0c484ed005434938ee70a938d7524e91"))
     }
     }
 
 
     func testSHA256() {
     func testSHA256() {
-        let key:Array<UInt8> = []
-        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 key: Array<UInt8> = []
+        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! HMAC(key: key, variant: .sha256).authenticate(msg)
         let hmac = try! HMAC(key: key, variant: .sha256).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
     }
 
 
     func testSHA384() {
     func testSHA384() {
-        let key:Array<UInt8> = []
-        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 key: Array<UInt8> = []
+        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! HMAC(key: key, variant: .sha384).authenticate(msg)
         let hmac = try! HMAC(key: key, variant: .sha384).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
     }
 
 
     func testSHA512() {
     func testSHA512() {
-        let key:Array<UInt8> = []
-        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 key: Array<UInt8> = []
+        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! HMAC(key: key, variant: .sha512).authenticate(msg)
         let hmac = try! HMAC(key: key, variant: .sha512).authenticate(msg)
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
         XCTAssertEqual(hmac, expectedMac, "Invalid authentication result")
     }
     }
 
 
-    static let allTests =  [
+    static let allTests = [
         ("testMD5", testMD5),
         ("testMD5", testMD5),
         ("testSHA1", testSHA1),
         ("testSHA1", testSHA1),
         ("testSHA256", testSHA256),
         ("testSHA256", testSHA256),
         ("testSHA384", testSHA384),
         ("testSHA384", testSHA384),
-        ("testSHA512", testSHA512)
+        ("testSHA512", testSHA512),
     ]
     ]
-
 }
 }

+ 6 - 8
Tests/CryptoSwiftTests/PBKDF.swift

@@ -12,8 +12,8 @@ import XCTest
 class PBKDF: XCTestCase {
 class PBKDF: XCTestCase {
 
 
     func testPBKDF1() {
     func testPBKDF1() {
-        let password: Array<UInt8> = [0x70,0x61,0x73,0x73,0x77,0x6F,0x72,0x64]
-        let salt: Array<UInt8> = [0x78,0x57,0x8E,0x5A,0x5D,0x63,0xCB,0x06]
+        let password: Array<UInt8> = [0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64]
+        let salt: Array<UInt8> = [0x78, 0x57, 0x8E, 0x5A, 0x5D, 0x63, 0xCB, 0x06]
         let value = try! PKCS5.PBKDF1(password: password, salt: salt, iterations: 1000, keyLength: 16).calculate()
         let value = try! PKCS5.PBKDF1(password: password, salt: salt, iterations: 1000, keyLength: 16).calculate()
         XCTAssertEqual(value.toHexString(), "dc19847e05c64d2faf10ebfb4a3d2a20")
         XCTAssertEqual(value.toHexString(), "dc19847e05c64d2faf10ebfb4a3d2a20")
     }
     }
@@ -43,20 +43,18 @@ class PBKDF: XCTestCase {
         // P = "pass\0word", S = "sa\0lt", c = 4096, dkLen = 16
         // P = "pass\0word", S = "sa\0lt", c = 4096, dkLen = 16
         XCTAssertEqual([0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3],
         XCTAssertEqual([0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3],
                        try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x00, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x00, 0x6C, 0x74], iterations: 4096, keyLength: 16, variant: .sha1).calculate())
                        try PKCS5.PBKDF2(password: [0x70, 0x61, 0x73, 0x73, 0x00, 0x77, 0x6F, 0x72, 0x64], salt: [0x73, 0x61, 0x00, 0x6C, 0x74], iterations: 4096, keyLength: 16, variant: .sha1).calculate())
-
     }
     }
 
 
     func testPBKDF2Length() {
     func testPBKDF2Length() {
-        let password: Array<UInt8> = "s33krit".utf8.map {$0}
-        let salt: Array<UInt8> = "nacl".utf8.map {$0}
+        let password: Array<UInt8> = "s33krit".utf8.map { $0 }
+        let salt: Array<UInt8> = "nacl".utf8.map { $0 }
         let value = try! PKCS5.PBKDF2(password: password, salt: salt, iterations: 2, keyLength: 8, variant: .sha1).calculate()
         let value = try! PKCS5.PBKDF2(password: password, salt: salt, iterations: 2, keyLength: 8, variant: .sha1).calculate()
         XCTAssert(value.toHexString() == "a53cf3df485e5cd9", "PBKDF2 fail")
         XCTAssert(value.toHexString() == "a53cf3df485e5cd9", "PBKDF2 fail")
     }
     }
 
 
-    static let allTests =  [
+    static let allTests = [
         ("testPBKDF1", testPBKDF1),
         ("testPBKDF1", testPBKDF1),
         ("testPBKDF2", testPBKDF2),
         ("testPBKDF2", testPBKDF2),
-        ("testPBKDF2Length", testPBKDF2Length)
+        ("testPBKDF2Length", testPBKDF2Length),
     ]
     ]
-    
 }
 }

+ 14 - 13
Tests/CryptoSwiftTests/PaddingTests.swift

@@ -9,27 +9,28 @@ import XCTest
 @testable import CryptoSwift
 @testable import CryptoSwift
 
 
 final class PaddingTests: XCTestCase {
 final class PaddingTests: XCTestCase {
+
     func testPKCS7_0() {
     func testPKCS7_0() {
-        let input:Array<UInt8>    = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6]
-        let expected:Array<UInt8> = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16]
+        let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
+        let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16]
         let padded = PKCS7().add(to: input, blockSize: 16)
         let padded = PKCS7().add(to: input, blockSize: 16)
         XCTAssertEqual(padded, expected, "PKCS7 failed")
         XCTAssertEqual(padded, expected, "PKCS7 failed")
         let clean = PKCS7().remove(from: padded, blockSize: nil)
         let clean = PKCS7().remove(from: padded, blockSize: nil)
         XCTAssertEqual(clean, input, "PKCS7 failed")
         XCTAssertEqual(clean, input, "PKCS7 failed")
     }
     }
-    
+
     func testPKCS7_1() {
     func testPKCS7_1() {
-        let input:Array<UInt8>    = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
-        let expected:Array<UInt8> = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,1]
+        let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
+        let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 1]
         let padded = PKCS7().add(to: input, blockSize: 16)
         let padded = PKCS7().add(to: input, blockSize: 16)
         XCTAssertEqual(padded, expected, "PKCS7 failed")
         XCTAssertEqual(padded, expected, "PKCS7 failed")
         let clean = PKCS7().remove(from: padded, blockSize: nil)
         let clean = PKCS7().remove(from: padded, blockSize: nil)
         XCTAssertEqual(clean, input, "PKCS7 failed")
         XCTAssertEqual(clean, input, "PKCS7 failed")
     }
     }
-    
+
     func testPKCS7_2() {
     func testPKCS7_2() {
-        let input:Array<UInt8>    = [1,2,3,4,5,6,7,8,9,0,1,2,3,4]
-        let expected:Array<UInt8> = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,2,2]
+        let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
+        let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 2, 2]
         let padded = PKCS7().add(to: input, blockSize: 16)
         let padded = PKCS7().add(to: input, blockSize: 16)
         XCTAssertEqual(padded, expected, "PKCS7 failed")
         XCTAssertEqual(padded, expected, "PKCS7 failed")
         let clean = PKCS7().remove(from: padded, blockSize: nil)
         let clean = PKCS7().remove(from: padded, blockSize: nil)
@@ -37,17 +38,17 @@ final class PaddingTests: XCTestCase {
     }
     }
 
 
     func testZeroPadding() {
     func testZeroPadding() {
-        let input:Array<UInt8>    = [1,2,3,4,5,6,7,8,9]
-        let expected:Array<UInt8> = [1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0]
+        let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9]
+        let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0]
         let padding = ZeroPadding()
         let padding = ZeroPadding()
         XCTAssertEqual(padding.add(to: input, blockSize: 16), expected, "ZeroPadding failed")
         XCTAssertEqual(padding.add(to: input, blockSize: 16), expected, "ZeroPadding failed")
         XCTAssertEqual(padding.remove(from: padding.add(to: input, blockSize: 16), blockSize: 16), input, "ZeroPadding failed")
         XCTAssertEqual(padding.remove(from: padding.add(to: input, blockSize: 16), blockSize: 16), input, "ZeroPadding failed")
     }
     }
-    
-    static let allTests =  [
+
+    static let allTests = [
         ("testPKCS7_0", testPKCS7_0),
         ("testPKCS7_0", testPKCS7_0),
         ("testPKCS7_1", testPKCS7_1),
         ("testPKCS7_1", testPKCS7_1),
         ("testPKCS7_2", testPKCS7_2),
         ("testPKCS7_2", testPKCS7_2),
-        ("testZeroPadding", testZeroPadding)
+        ("testZeroPadding", testZeroPadding),
     ]
     ]
 }
 }

+ 9 - 9
Tests/CryptoSwiftTests/Poly1305Tests.swift

@@ -10,22 +10,22 @@ import Foundation
 @testable import CryptoSwift
 @testable import CryptoSwift
 
 
 final class Poly1305Tests: XCTestCase {
 final class Poly1305Tests: XCTestCase {
-    
+
     func testPoly1305() {
     func testPoly1305() {
-        let key:Array<UInt8> = [0xdd,0xde,0xdf,0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc]
-        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 key: Array<UInt8> = [0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc]
+        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! Poly1305(key: key).authenticate(msg)
         let mac = try! Poly1305(key: key).authenticate(msg)
         XCTAssertEqual(mac, expectedMac, "Invalid authentication result")
         XCTAssertEqual(mac, expectedMac, "Invalid authentication result")
-        
+
         // extensions
         // extensions
         let msgData = Data(bytes: msg)
         let msgData = Data(bytes: msg)
         let mac2 = try! msgData.authenticate(with: Poly1305(key: key))
         let mac2 = try! msgData.authenticate(with: Poly1305(key: key))
         XCTAssertEqual(mac2, Data(bytes: expectedMac), "Invalid authentication result")
         XCTAssertEqual(mac2, Data(bytes: expectedMac), "Invalid authentication result")
     }
     }
-    
-    static let allTests =  [
-        ("testPoly1305", testPoly1305)
+
+    static let allTests = [
+        ("testPoly1305", testPoly1305),
     ]
     ]
 }
 }

+ 23 - 20
Tests/CryptoSwiftTests/RabbitTests.swift

@@ -18,17 +18,17 @@ class RabbitTests: XCTestCase {
 
 
         key = Array<UInt8>(repeating: 0, count: Rabbit.keySize + 1)
         key = Array<UInt8>(repeating: 0, count: Rabbit.keySize + 1)
         XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
         XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
-        
+
         key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
         key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
         XCTAssertNotNil(try Rabbit(key: key, iv: iv))
         XCTAssertNotNil(try Rabbit(key: key, iv: iv))
-        
+
         iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize - 1)
         iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize - 1)
         XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
         XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
-        
+
         iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
         iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
         XCTAssertNotNil(try Rabbit(key: key, iv: iv))
         XCTAssertNotNil(try Rabbit(key: key, iv: iv))
     }
     }
-    
+
     func testRabbitWithoutIV() {
     func testRabbitWithoutIV() {
         // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
         // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
         let cases: [(Array<UInt8>, Array<UInt8>)] = [
         let cases: [(Array<UInt8>, Array<UInt8>)] = [
@@ -60,7 +60,7 @@ class RabbitTests: XCTestCase {
                 ]
                 ]
             ),
             ),
         ]
         ]
-        
+
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         for (key, expectedCipher) in cases {
         for (key, expectedCipher) in cases {
             let rabbit = try! Rabbit(key: key)
             let rabbit = try! Rabbit(key: key)
@@ -69,7 +69,7 @@ class RabbitTests: XCTestCase {
             XCTAssertEqual(rabbit.decrypt(cipherText), plainText)
             XCTAssertEqual(rabbit.decrypt(cipherText), plainText)
         }
         }
     }
     }
-    
+
     func testRabbitWithIV() {
     func testRabbitWithIV() {
         // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
         // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
         let key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
         let key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
@@ -99,7 +99,7 @@ class RabbitTests: XCTestCase {
                 ]
                 ]
             ),
             ),
         ]
         ]
-        
+
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         for (iv, expectedCipher) in cases {
         for (iv, expectedCipher) in cases {
             let rabbit = try! Rabbit(key: key, iv: iv)
             let rabbit = try! Rabbit(key: key, iv: iv)
@@ -111,31 +111,34 @@ class RabbitTests: XCTestCase {
 }
 }
 
 
 #if !CI
 #if !CI
-extension RabbitTests {
-    func testRabbitPerformance() {
-        let key: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
-        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 = try! Rabbit(key: key, iv: iv).encrypt(message)
-            self.stopMeasuring()
-            XCTAssert(!encrypted.isEmpty, "not encrypted")
-        })
+
+    extension RabbitTests {
+
+        func testRabbitPerformance() {
+            let key: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
+            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 = try! Rabbit(key: key, iv: iv).encrypt(message)
+                self.stopMeasuring()
+                XCTAssert(!encrypted.isEmpty, "not encrypted")
+            })
+        }
     }
     }
-}
 #endif
 #endif
 
 
 extension RabbitTests {
 extension RabbitTests {
+
     static func allTests() -> [(String, (RabbitTests) -> () -> Void)] {
     static func allTests() -> [(String, (RabbitTests) -> () -> Void)] {
         var tests = [
         var tests = [
             ("testInitialization", testInitialization),
             ("testInitialization", testInitialization),
             ("testRabbitWithoutIV", testRabbitWithoutIV),
             ("testRabbitWithoutIV", testRabbitWithoutIV),
-            ("testRabbitWithIV", testRabbitWithIV)
+            ("testRabbitWithIV", testRabbitWithIV),
         ]
         ]
 
 
         #if !CI
         #if !CI
             tests += [
             tests += [
-                ("testRabbitPerformance", testRabbitPerformance)
+                ("testRabbitPerformance", testRabbitPerformance),
             ]
             ]
         #endif
         #endif
         return tests
         return tests

+ 3 - 2
Tests/CryptoSwiftTests/RandomBytesSequenceTests.swift

@@ -10,6 +10,7 @@ import XCTest
 @testable import CryptoSwift
 @testable import CryptoSwift
 
 
 class RandomBytesSequenceTests: XCTestCase {
 class RandomBytesSequenceTests: XCTestCase {
+
     func testSequence() {
     func testSequence() {
         XCTAssertNil(RandomBytesSequence(size: 0).makeIterator().next())
         XCTAssertNil(RandomBytesSequence(size: 0).makeIterator().next())
 
 
@@ -18,7 +19,7 @@ class RandomBytesSequenceTests: XCTestCase {
         }
         }
     }
     }
 
 
-    static let allTests =  [
-        ("testSequence", testSequence)
+    static let allTests = [
+        ("testSequence", testSequence),
     ]
     ]
 }
 }

+ 1 - 1
Tests/LinuxMain.swift

@@ -12,5 +12,5 @@ XCTMain([
     testCase(PaddingTests.allTests),
     testCase(PaddingTests.allTests),
     testCase(PBKDF.allTests),
     testCase(PBKDF.allTests),
     testCase(RandomBytesSequenceTests.allTests),
     testCase(RandomBytesSequenceTests.allTests),
-    testCase(Access.allTests)
+    testCase(Access.allTests),
 ])
 ])

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно