Эх сурвалжийг харах

Using switch syntax when possible for segment size

Nathan Fallet 4 жил өмнө
parent
commit
085709b6a4

+ 6 - 12
Sources/CryptoSwift/BlockMode/CFB.swift

@@ -63,16 +63,14 @@ struct CFBModeWorker: BlockModeWorker {
   }
   }
 
 
   mutating func encrypt(block plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
   mutating func encrypt(block plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
-    // CFB128
-    if segmentSize == .cfb128 {
+    switch segmentSize {
+    case .cfb128:
       guard let ciphertext = cipherOperation(prev ?? iv) else {
       guard let ciphertext = cipherOperation(prev ?? iv) else {
         return Array(plaintext)
         return Array(plaintext)
       }
       }
       self.prev = xor(plaintext, ciphertext.slice)
       self.prev = xor(plaintext, ciphertext.slice)
       return Array(self.prev ?? [])
       return Array(self.prev ?? [])
-    }
-    // CFB8
-    else if segmentSize == .cfb8 {
+    case .cfb8:
       guard let ciphertext = cipherOperation(prev ?? iv) else {
       guard let ciphertext = cipherOperation(prev ?? iv) else {
         return Array(plaintext)
         return Array(plaintext)
       }
       }
@@ -80,27 +78,23 @@ struct CFBModeWorker: BlockModeWorker {
       self.prev = Array((prev ?? iv).dropFirst()) + [result[0]]
       self.prev = Array((prev ?? iv).dropFirst()) + [result[0]]
       return result
       return result
     }
     }
-    return Array(plaintext) // Unsupported segment size
   }
   }
 
 
   mutating func decrypt(block ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
   mutating func decrypt(block ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
-    // CFB128
-    if segmentSize == .cfb128 {
+    switch segmentSize {
+    case .cfb128:
       guard let plaintext = cipherOperation(prev ?? iv) else {
       guard let plaintext = cipherOperation(prev ?? iv) else {
         return Array(ciphertext)
         return Array(ciphertext)
       }
       }
       let result: Array<UInt8> = xor(plaintext, ciphertext)
       let result: Array<UInt8> = xor(plaintext, ciphertext)
       prev = ciphertext
       prev = ciphertext
       return result
       return result
-    }
-    // CFB8
-    else if segmentSize == .cfb8 {
+    case .cfb8:
       guard let plaintext = cipherOperation(prev ?? iv) else {
       guard let plaintext = cipherOperation(prev ?? iv) else {
         return Array(ciphertext)
         return Array(ciphertext)
       }
       }
       self.prev = Array((prev ?? iv).dropFirst()) + [Array(ciphertext)[0]]
       self.prev = Array((prev ?? iv).dropFirst()) + [Array(ciphertext)[0]]
       return [Array(ciphertext)[0] ^ Array(plaintext)[0]]
       return [Array(ciphertext)[0] ^ Array(plaintext)[0]]
     }
     }
-    return Array(ciphertext) // Unsupported segment size
   }
   }
 }
 }