Sfoglia il codice sorgente

CipherProtocol - rename cipherEncrypt -> encrypt and cipherDecrypt -> decrypt

Marcin Krzyżanowski 9 anni fa
parent
commit
a4bf8b8bc8

+ 38 - 46
Sources/CryptoSwift/AES.swift

@@ -107,46 +107,6 @@ final public class AES: BlockCipher {
             throw Error.InvalidInitializationVector
             throw Error.InvalidInitializationVector
         }
         }
     }
     }
-
-    /// Encrypt given bytes at once
-    ///
-    /// - parameter bytes: Plaintext data
-    /// - returns: Encrypted data
-    public func encrypt(bytes:[UInt8]) throws -> [UInt8] {
-        let chunks = bytes.chunks(AES.blockSize)
-
-        var oneTimeCryptor = Encryptor(aes: self)
-        var out = [UInt8]()
-        out.reserveCapacity(bytes.count)
-        for (idx, block) in chunks.enumerate() {
-            out.appendContentsOf(try oneTimeCryptor.update(block, isLast: idx == max(0,chunks.count - 1)))
-        }
-
-        if cipherBlockMode.options.contains(.PaddingRequired) && (out.count % AES.blockSize != 0) {
-            throw Error.DataPaddingRequired
-        }
-
-        return out
-    }
-    
-    /// Decrypt given bytes at once
-    ///
-    /// - parameter bytes: Ciphertext data
-    /// - returns: Plaintext data
-    public func decrypt(bytes:[UInt8]) throws -> [UInt8] {
-        if cipherBlockMode.options.contains(.PaddingRequired) && (bytes.count % AES.blockSize != 0) {
-            throw Error.DataPaddingRequired
-        }
-
-        var oneTimeCryptor = Decryptor(aes: self)
-        let chunks = bytes.chunks(AES.blockSize)
-        var out = [UInt8]()
-        out.reserveCapacity(bytes.count)
-        for (idx,chunk) in chunks.enumerate() {
-            out.appendContentsOf(try oneTimeCryptor.update(chunk, isLast: idx == max(0,chunks.count - 1)))
-        }
-        return out
-    }
 }
 }
 
 
 // MARK: Private
 // MARK: Private
@@ -492,12 +452,44 @@ extension AES: CipherProtocol {
     public func decryptor() -> Cryptor {
     public func decryptor() -> Cryptor {
         return Decryptor(aes: self)
         return Decryptor(aes: self)
     }
     }
-    
-    public func cipherEncrypt(bytes:[UInt8]) throws -> [UInt8] {
-        return try self.encrypt(bytes)
+
+    /// Encrypt given bytes at once
+    ///
+    /// - parameter bytes: Plaintext data
+    /// - returns: Encrypted data
+    public func encrypt(bytes:[UInt8]) throws -> [UInt8] {
+        let chunks = bytes.chunks(AES.blockSize)
+
+        var oneTimeCryptor = Encryptor(aes: self)
+        var out = [UInt8]()
+        out.reserveCapacity(bytes.count)
+        for (idx, block) in chunks.enumerate() {
+            out.appendContentsOf(try oneTimeCryptor.update(block, isLast: idx == max(0,chunks.count - 1)))
+        }
+
+        if cipherBlockMode.options.contains(.PaddingRequired) && (out.count % AES.blockSize != 0) {
+            throw Error.DataPaddingRequired
+        }
+
+        return out
     }
     }
-    
-    public func cipherDecrypt(bytes: [UInt8]) throws -> [UInt8] {
-        return try self.decrypt(bytes)
+
+    /// Decrypt given bytes at once
+    ///
+    /// - parameter bytes: Ciphertext data
+    /// - returns: Plaintext data
+    public func decrypt(bytes:[UInt8]) throws -> [UInt8] {
+        if cipherBlockMode.options.contains(.PaddingRequired) && (bytes.count % AES.blockSize != 0) {
+            throw Error.DataPaddingRequired
+        }
+
+        var oneTimeCryptor = Decryptor(aes: self)
+        let chunks = bytes.chunks(AES.blockSize)
+        var out = [UInt8]()
+        out.reserveCapacity(bytes.count)
+        for (idx,chunk) in chunks.enumerate() {
+            out.appendContentsOf(try oneTimeCryptor.update(chunk, isLast: idx == max(0,chunks.count - 1)))
+        }
+        return out
     }
     }
 }
 }

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

@@ -56,11 +56,11 @@ public extension CSArrayType where Generator.Element == UInt8 {
     }
     }
     
     
     public func encrypt(cipher: CipherProtocol) throws -> [Generator.Element] {
     public func encrypt(cipher: CipherProtocol) throws -> [Generator.Element] {
-        return try cipher.cipherEncrypt(cs_arrayValue())
+        return try cipher.encrypt(cs_arrayValue())
     }
     }
 
 
     public func decrypt(cipher: CipherProtocol) throws -> [Generator.Element] {
     public func decrypt(cipher: CipherProtocol) throws -> [Generator.Element] {
-        return try cipher.cipherDecrypt(cs_arrayValue())
+        return try cipher.decrypt(cs_arrayValue())
     }
     }
     
     
     public func authenticate(authenticator: Authenticator) throws -> [Generator.Element] {
     public func authenticate(authenticator: Authenticator) throws -> [Generator.Element] {

+ 9 - 17
Sources/CryptoSwift/ChaCha20.swift

@@ -34,18 +34,6 @@ final public class ChaCha20: BlockCipher {
         }
         }
     }
     }
     
     
-    public func encrypt(bytes:[UInt8]) throws -> [UInt8] {
-        guard context != nil else {
-            throw Error.MissingContext
-        }
-        
-        return try encryptBytes(bytes)
-    }
-    
-    public func decrypt(bytes:[UInt8]) throws -> [UInt8] {
-        return try encrypt(bytes)
-    }
-    
     private final func wordToByte(input:[UInt32] /* 64 */) -> [UInt8]? /* 16 */ {
     private final func wordToByte(input:[UInt32] /* 64 */) -> [UInt8]? /* 16 */ {
         if (input.count != stateSize) {
         if (input.count != stateSize) {
             return nil;
             return nil;
@@ -214,12 +202,16 @@ extension ChaCha20: CipherProtocol {
         return Decryptor(chacha20: self)
         return Decryptor(chacha20: self)
     }
     }
 
 
-    public func cipherEncrypt(bytes:[UInt8]) throws -> [UInt8] {
-        return try self.encrypt(bytes)
+    public func encrypt(bytes:[UInt8]) throws -> [UInt8] {
+        guard context != nil else {
+            throw Error.MissingContext
+        }
+
+        return try encryptBytes(bytes)
     }
     }
-    
-    public func cipherDecrypt(bytes: [UInt8]) throws -> [UInt8] {
-        return try self.decrypt(bytes)
+
+    public func decrypt(bytes:[UInt8]) throws -> [UInt8] {
+        return try encrypt(bytes)
     }
     }
 }
 }
 
 

+ 2 - 2
Sources/CryptoSwift/CipherProtocol.swift

@@ -22,8 +22,8 @@ public protocol CipherProtocol {
     func encryptor() -> Cryptor;
     func encryptor() -> Cryptor;
     func decryptor() -> Cryptor;
     func decryptor() -> Cryptor;
 
 
-    func cipherEncrypt(bytes: [UInt8]) throws -> [UInt8]
-    func cipherDecrypt(bytes: [UInt8]) throws -> [UInt8]
+    func encrypt(bytes: [UInt8]) throws -> [UInt8]
+    func decrypt(bytes: [UInt8]) throws -> [UInt8]
     
     
     static func randomIV(blockSize:Int) -> [UInt8]
     static func randomIV(blockSize:Int) -> [UInt8]
 }
 }

+ 2 - 2
Sources/CryptoSwift/Foundation/NSData+Extension.swift

@@ -71,12 +71,12 @@ extension NSData {
     }
     }
 
 
     public func encrypt(cipher: CipherProtocol) throws -> NSData {
     public func encrypt(cipher: CipherProtocol) throws -> NSData {
-        let encrypted = try cipher.cipherEncrypt(self.arrayOfBytes())
+        let encrypted = try cipher.encrypt(self.arrayOfBytes())
         return NSData.withBytes(encrypted)
         return NSData.withBytes(encrypted)
     }
     }
 
 
     public func decrypt(cipher: CipherProtocol) throws -> NSData {
     public func decrypt(cipher: CipherProtocol) throws -> NSData {
-        let decrypted = try cipher.cipherDecrypt(self.arrayOfBytes())
+        let decrypted = try cipher.decrypt(self.arrayOfBytes())
         return NSData.withBytes(decrypted)
         return NSData.withBytes(decrypted)
     }
     }
     
     

+ 23 - 32
Sources/CryptoSwift/Rabbit.swift

@@ -171,32 +171,6 @@ final public class Rabbit: BlockCipher {
         }
         }
         return output8
         return output8
     }
     }
-    
-    // MARK: - Public
-    public func encrypt(bytes: [UInt8]) -> [UInt8] {
-        setup()
-        
-        var result = [UInt8](count: bytes.count, repeatedValue: 0)
-        var output = nextOutput()
-        var byteIdx = 0
-        var outputIdx = 0
-        while byteIdx < bytes.count {
-            if (outputIdx == Rabbit.blockSize) {
-                output = nextOutput()
-                outputIdx = 0
-            }
-            
-            result[byteIdx] = bytes[byteIdx] ^ output[outputIdx]
-
-            byteIdx += 1
-            outputIdx += 1
-        }
-        return result
-    }
-    
-    public func decrypt(bytes: [UInt8]) -> [UInt8] {
-        return encrypt(bytes)
-    }
 }
 }
 
 
 extension Rabbit {
 extension Rabbit {
@@ -238,12 +212,29 @@ extension Rabbit: CipherProtocol {
     public func decryptor() -> Cryptor {
     public func decryptor() -> Cryptor {
         return Decryptor(rabbit: self)
         return Decryptor(rabbit: self)
     }
     }
-    
-    public func cipherEncrypt(bytes:[UInt8]) -> [UInt8] {
-        return self.encrypt(bytes)
+
+    public func encrypt(bytes: [UInt8]) -> [UInt8] {
+        setup()
+
+        var result = [UInt8](count: bytes.count, repeatedValue: 0)
+        var output = nextOutput()
+        var byteIdx = 0
+        var outputIdx = 0
+        while byteIdx < bytes.count {
+            if (outputIdx == Rabbit.blockSize) {
+                output = nextOutput()
+                outputIdx = 0
+            }
+
+            result[byteIdx] = bytes[byteIdx] ^ output[outputIdx]
+
+            byteIdx += 1
+            outputIdx += 1
+        }
+        return result
     }
     }
-    
-    public func cipherDecrypt(bytes: [UInt8]) -> [UInt8] {
-        return self.decrypt(bytes)
+
+    public func decrypt(bytes: [UInt8]) -> [UInt8] {
+        return encrypt(bytes)
     }
     }
 }
 }