浏览代码

Fix incremental encryption

Marcin Krzyżanowski 9 年之前
父节点
当前提交
a827813c79
共有 1 个文件被更改,包括 14 次插入17 次删除
  1. 14 17
      Sources/CryptoSwift/AES.swift

+ 14 - 17
Sources/CryptoSwift/AES.swift

@@ -411,17 +411,15 @@ extension AES {
             }
 
             //CTR does not require full block therefore work with anything
-            if (!self.paddingRequired || self.accumulated.count >= AES.blockSize) {
-                var encrypted = Array<UInt8>()
-                encrypted.reserveCapacity(self.accumulated.count)
-                for chunk in self.accumulated.chunks(AES.blockSize) {
+            var encrypted = Array<UInt8>()
+            encrypted.reserveCapacity(self.accumulated.count)
+            for chunk in self.accumulated.chunks(AES.blockSize) {
+                if (!self.paddingRequired || self.accumulated.count >= AES.blockSize) {
                     encrypted += worker.encrypt(chunk)
                     self.accumulated.removeFirst(chunk.count)
                 }
-                return encrypted
             }
-
-            return []
+            return encrypted
         }
     }
 }
@@ -451,21 +449,20 @@ extension AES {
         mutating public func update(withBytes bytes:[UInt8], isLast: Bool = false) throws -> [UInt8] {
             self.accumulated += bytes
 
-            if (!self.paddingRequired || self.accumulated.count >= AES.blockSize) {
-                var plaintext = Array<UInt8>()
-                plaintext.reserveCapacity(self.accumulated.count)
-                for chunk in self.accumulated.chunks(AES.blockSize) {
+            var plaintext = Array<UInt8>()
+            plaintext.reserveCapacity(self.accumulated.count)
+            for chunk in self.accumulated.chunks(AES.blockSize) {
+                if (!self.paddingRequired || self.accumulated.count >= AES.blockSize) {
                     plaintext += worker.decrypt(chunk)
                     self.accumulated.removeFirst(chunk.count)
                 }
+            }
 
-                if (isLast) {
-                    plaintext = padding.remove(plaintext, blockSize: AES.blockSize)
-                }
-
-                return plaintext
+            if (isLast) {
+                plaintext = padding.remove(plaintext, blockSize: AES.blockSize)
             }
-            return []
+
+            return plaintext
         }
     }
 }