Ver Fonte

format swift

Marcin Krzyżanowski há 7 anos atrás
pai
commit
2420815e5a

+ 2 - 2
CryptoSwift.playground/Contents.swift

@@ -30,8 +30,8 @@ Digest.sha1(bytes)
 //: Digest calculated incrementally
 do {
     var digest = MD5()
-    let _ = try digest.update(withBytes: [0x31, 0x32])
-    let _ = try digest.update(withBytes: [0x33])
+    _ = try digest.update(withBytes: [0x31, 0x32])
+    _ = try digest.update(withBytes: [0x33])
     let result = try digest.finish()
     print(result)
 } catch {}

+ 6 - 6
Package.swift

@@ -2,9 +2,9 @@
 
 import PackageDescription
 
-let _ = Package(name: "CryptoSwift", products: [.library(name: "CryptoSwift", targets: ["CryptoSwift"])],
-              targets: [
-                  .target(name:"CryptoSwift"),
-                  .testTarget(name:"CryptoSwiftTests",dependencies:["CryptoSwift"])
-              ],
-              swiftLanguageVersions: [4])
+_ = Package(name: "CryptoSwift", products: [.library(name: "CryptoSwift", targets: ["CryptoSwift"])],
+            targets: [
+                .target(name: "CryptoSwift"),
+                .testTarget(name: "CryptoSwiftTests", dependencies: ["CryptoSwift"]),
+            ],
+            swiftLanguageVersions: [4])

+ 30 - 31
Sources/CryptoSwift/AES.Cryptors.swift

@@ -25,7 +25,6 @@ extension AES: Cryptors {
     }
 }
 
-
 // MARK: Encryptor
 extension AES {
     public struct Encryptor: Updatable {
@@ -36,28 +35,28 @@ extension AES {
         private let paddingRequired: Bool
 
         init(aes: AES) throws {
-            self.padding = aes.padding
-            self.worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.encrypt)
-            self.paddingRequired = aes.blockMode.options.contains(.paddingRequired)
+            padding = aes.padding
+            worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.encrypt)
+            paddingRequired = aes.blockMode.options.contains(.paddingRequired)
         }
 
         public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
-            self.accumulated += bytes
+            accumulated += bytes
 
             if isLast {
-                self.accumulated = padding.add(to: self.accumulated, blockSize: AES.blockSize)
+                accumulated = padding.add(to: accumulated, blockSize: AES.blockSize)
             }
 
             var processedBytes = 0
-            var encrypted = Array<UInt8>(reserveCapacity: self.accumulated.count)
-            for chunk in self.accumulated.batched(by: AES.blockSize) {
-                if isLast || (self.accumulated.count - processedBytes) >= AES.blockSize {
+            var encrypted = Array<UInt8>(reserveCapacity: accumulated.count)
+            for chunk in accumulated.batched(by: AES.blockSize) {
+                if isLast || (accumulated.count - processedBytes) >= AES.blockSize {
                     encrypted += worker.encrypt(chunk)
                     processedBytes += chunk.count
                 }
             }
-            self.accumulated.removeFirst(processedBytes)
-            self.processedBytesTotalCount += processedBytes
+            accumulated.removeFirst(processedBytes)
+            processedBytesTotalCount += processedBytes
             return encrypted
         }
     }
@@ -77,46 +76,46 @@ extension AES {
         private var offsetToRemove: Int = 0
 
         init(aes: AES) throws {
-            self.padding = aes.padding
+            padding = aes.padding
 
             switch aes.blockMode {
             case .CFB, .OFB, .CTR:
                 // CFB, OFB, CTR uses encryptBlock to decrypt
-                self.worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.encrypt)
+                worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.encrypt)
             default:
-                self.worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.decrypt)
+                worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.decrypt)
             }
 
-            self.paddingRequired = aes.blockMode.options.contains(.paddingRequired)
+            paddingRequired = aes.blockMode.options.contains(.paddingRequired)
         }
 
         public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
             // prepend "offset" number of bytes at the begining
-            if self.offset > 0 {
-                self.accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes
-                self.offsetToRemove = offset
-                self.offset = 0
+            if offset > 0 {
+                accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes
+                offsetToRemove = offset
+                offset = 0
             } else {
-                self.accumulated += bytes
+                accumulated += bytes
             }
 
             var processedBytes = 0
-            var plaintext = Array<UInt8>(reserveCapacity: self.accumulated.count)
-            for chunk in self.accumulated.batched(by: AES.blockSize) {
-                if isLast || (self.accumulated.count - processedBytes) >= AES.blockSize {
-                    plaintext += self.worker.decrypt(chunk)
+            var plaintext = Array<UInt8>(reserveCapacity: accumulated.count)
+            for chunk in accumulated.batched(by: AES.blockSize) {
+                if isLast || (accumulated.count - processedBytes) >= AES.blockSize {
+                    plaintext += worker.decrypt(chunk)
 
                     // remove "offset" from the beginning of first chunk
-                    if self.offsetToRemove > 0 {
-                        plaintext.removeFirst(self.offsetToRemove)
-                        self.offsetToRemove = 0
+                    if offsetToRemove > 0 {
+                        plaintext.removeFirst(offsetToRemove)
+                        offsetToRemove = 0
                     }
 
                     processedBytes += chunk.count
                 }
             }
-            self.accumulated.removeFirst(processedBytes)
-            self.processedBytesTotalCount += processedBytes
+            accumulated.removeFirst(processedBytes)
+            processedBytesTotalCount += processedBytes
 
             if isLast {
                 plaintext = padding.remove(from: plaintext, blockSize: AES.blockSize)
@@ -133,9 +132,9 @@ extension AES {
             worker.counter = UInt(position / AES.blockSize)
             self.worker = worker
 
-            self.offset = position % AES.blockSize
+            offset = position % AES.blockSize
 
-            self.accumulated = []
+            accumulated = []
 
             return true
         }

+ 2 - 3
Sources/CryptoSwift/AES.swift

@@ -159,7 +159,6 @@ public final class AES: BlockCipher {
         let b33 = UInt32(block[block.startIndex.advanced(by: 15)]) << 24
         var b3 = b30 | b31 | b32 | b33
 
-
         let tLength = 4
         let t = UnsafeMutablePointer<UInt32>.allocate(capacity: tLength)
         t.initialize(to: 0, count: tLength)
@@ -316,7 +315,7 @@ public final class AES: BlockCipher {
 private extension AES {
     private func expandKeyInv(_ key: Key, variant: Variant) -> Array<Array<UInt32>> {
         let rounds = variantNr
-        var rk2: Array<Array<UInt32>> = self.expandKey(key, variant: variant)
+        var rk2: Array<Array<UInt32>> = expandKey(key, variant: variant)
 
         for r in 1..<rounds {
             for i in 0..<4 {
@@ -332,7 +331,7 @@ private extension AES {
         return rk2
     }
 
-    private func expandKey(_ key: Key, variant: Variant) -> Array<Array<UInt32>> {
+    private func expandKey(_ key: Key, variant _: Variant) -> Array<Array<UInt32>> {
 
         func convertExpandedKey(_ expanded: Array<UInt8>) -> Array<Array<UInt32>> {
             return expanded.batched(by: 4).map({ UInt32(bytes: $0.reversed()) }).batched(by: 4).map({ Array($0) })

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

@@ -70,11 +70,11 @@ public extension Array where Element == UInt8 {
     }
 
     public func encrypt(cipher: Cipher) throws -> [Element] {
-        return try cipher.encrypt(self.slice)
+        return try cipher.encrypt(slice)
     }
 
     public func decrypt(cipher: Cipher) throws -> [Element] {
-        return try cipher.decrypt(self.slice)
+        return try cipher.decrypt(slice)
     }
 
     public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Element] {

+ 10 - 11
Sources/CryptoSwift/BlockMode/BlockMode.swift

@@ -30,28 +30,28 @@ public enum BlockMode {
         switch self {
         case .ECB:
             return ECBModeWorker(cipherOperation: cipherOperation)
-        case .CBC(let iv):
-            if (iv.count != blockSize) {
+        case let .CBC(iv):
+            if iv.count != blockSize {
                 throw Error.invalidInitializationVector
             }
             return CBCModeWorker(iv: iv.slice, cipherOperation: cipherOperation)
-        case .PCBC(let iv):
-            if (iv.count != blockSize) {
+        case let .PCBC(iv):
+            if iv.count != blockSize {
                 throw Error.invalidInitializationVector
             }
             return PCBCModeWorker(iv: iv.slice, cipherOperation: cipherOperation)
-        case .CFB(let iv):
-            if (iv.count != blockSize) {
+        case let .CFB(iv):
+            if iv.count != blockSize {
                 throw Error.invalidInitializationVector
             }
             return CFBModeWorker(iv: iv.slice, cipherOperation: cipherOperation)
-        case .OFB(let iv):
-            if (iv.count != blockSize) {
+        case let .OFB(iv):
+            if iv.count != blockSize {
                 throw Error.invalidInitializationVector
             }
             return OFBModeWorker(iv: iv.slice, cipherOperation: cipherOperation)
-        case .CTR(let iv):
-            if (iv.count != blockSize) {
+        case let .CTR(iv):
+            if iv.count != blockSize {
                 throw Error.invalidInitializationVector
             }
             return CTRModeWorker(iv: iv.slice, cipherOperation: cipherOperation)
@@ -75,4 +75,3 @@ public enum BlockMode {
         }
     }
 }
-

+ 6 - 6
Sources/CryptoSwift/Blowfish.swift

@@ -326,13 +326,13 @@ public final class Blowfish {
     }
 
     private func setupBlockModeWorkers() throws {
-        encryptWorker = try self.blockMode.worker(blockSize: Blowfish.blockSize, cipherOperation: self.encrypt)
+        encryptWorker = try blockMode.worker(blockSize: Blowfish.blockSize, cipherOperation: encrypt)
 
-        switch self.blockMode {
-            case .CFB, .OFB, .CTR:
-                decryptWorker = try self.blockMode.worker(blockSize: Blowfish.blockSize, cipherOperation: self.encrypt)
-            default:
-                decryptWorker = try self.blockMode.worker(blockSize: Blowfish.blockSize, cipherOperation: self.decrypt)
+        switch blockMode {
+        case .CFB, .OFB, .CTR:
+            decryptWorker = try blockMode.worker(blockSize: Blowfish.blockSize, cipherOperation: encrypt)
+        default:
+            decryptWorker = try blockMode.worker(blockSize: Blowfish.blockSize, cipherOperation: decrypt)
         }
     }
 

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

@@ -17,7 +17,7 @@ extension Collection where Self.Element == UInt8, Self.Index == Int {
 
     // Big endian order
     func toUInt32Array() -> Array<UInt32> {
-        if (self.isEmpty) {
+        if isEmpty {
             return []
         }
 
@@ -32,7 +32,7 @@ extension Collection where Self.Element == UInt8, Self.Index == Int {
 
     // Big endian order
     func toUInt64Array() -> Array<UInt64> {
-        if (self.isEmpty) {
+        if isEmpty {
             return []
         }
 

+ 5 - 6
Sources/CryptoSwift/SHA3.swift

@@ -37,14 +37,14 @@ public final class SHA3: DigestType {
     public let blockSize: Int
     public let digestLength: Int
     public let markByte: UInt8
-    
+
     fileprivate var accumulated = Array<UInt8>()
     fileprivate var processedBytesTotalCount: Int = 0
     fileprivate var accumulatedHash: Array<UInt64>
-    
+
     public enum Variant {
         case sha224, sha256, sha384, sha512, keccak224, keccak256, keccak384, keccak512
-        
+
         var digestLength: Int {
             return 100 - (blockSize / 2)
         }
@@ -52,7 +52,7 @@ public final class SHA3: DigestType {
         var blockSize: Int {
             return (1600 - outputLength * 2) / 8
         }
-        
+
         var markByte: UInt8 {
             switch self {
             case .sha224, .sha256, .sha384, .sha512:
@@ -61,7 +61,7 @@ public final class SHA3: DigestType {
                 return 0x01
             }
         }
-        
+
         public var outputLength: Int {
             switch self {
             case .sha224, .keccak224:
@@ -290,4 +290,3 @@ extension SHA3: Updatable {
         return Array(result[0..<self.digestLength])
     }
 }
-

+ 1 - 1
Sources/CryptoSwift/String+Extension.swift

@@ -18,7 +18,7 @@
 extension String {
 
     public var bytes: Array<UInt8> {
-        return self.data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(self.utf8)
+        return data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8)
     }
 
     public func md5() -> String {

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

@@ -28,11 +28,11 @@ extension UInt16 {
             self = 0
             return
         }
-        
+
         let count = bytes.count
 
         let val0 = count > 0 ? UInt16(bytes[index.advanced(by: 0)]) << 8 : 0
-        let val1 = count > 1 ? UInt16(bytes[index.advanced(by: 1)])      : 0
+        let val1 = count > 1 ? UInt16(bytes[index.advanced(by: 1)]) : 0
 
         self = val0 | val1
     }

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

@@ -30,15 +30,15 @@ extension UInt64 {
         }
 
         let count = bytes.count
-        
+
         let val0 = count > 0 ? UInt64(bytes[index.advanced(by: 0)]) << 56 : 0
         let val1 = count > 0 ? UInt64(bytes[index.advanced(by: 1)]) << 48 : 0
         let val2 = count > 0 ? UInt64(bytes[index.advanced(by: 2)]) << 40 : 0
         let val3 = count > 0 ? UInt64(bytes[index.advanced(by: 3)]) << 32 : 0
         let val4 = count > 0 ? UInt64(bytes[index.advanced(by: 4)]) << 24 : 0
         let val5 = count > 0 ? UInt64(bytes[index.advanced(by: 5)]) << 16 : 0
-        let val6 = count > 0 ? UInt64(bytes[index.advanced(by: 6)]) << 8  : 0
-        let val7 = count > 0 ? UInt64(bytes[index.advanced(by: 7)])       : 0
+        let val6 = count > 0 ? UInt64(bytes[index.advanced(by: 6)]) << 8 : 0
+        let val7 = count > 0 ? UInt64(bytes[index.advanced(by: 7)]) : 0
 
         self = val0 | val1 | val2 | val3 | val4 | val5 | val6 | val7
     }

+ 2 - 3
Sources/CryptoSwift/Utils.swift

@@ -60,11 +60,11 @@ func reversed(_ uint32: UInt32) -> UInt32 {
     return v
 }
 
-func xor<T,V>(_ left: T, _ right: V) -> ArraySlice<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
+func xor<T, V>(_ left: T, _ right: V) -> ArraySlice<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
     return xor(left, right).slice
 }
 
-func xor<T,V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
+func xor<T, V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
     let length = Swift.min(left.count, right.count)
 
     let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
@@ -82,7 +82,6 @@ func xor<T,V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollec
     return Array(UnsafeBufferPointer(start: buf, count: length))
 }
 
-
 /**
  ISO/IEC 9797-1 Padding method 2.
  Add a single bit with value 1 to the end of the data.

+ 1 - 2
Tests/CryptoSwiftTests/ExtensionsTest.swift

@@ -90,7 +90,6 @@ final class ExtensionsTest: XCTestCase {
                 _ = Array<UInt8>(hex: str)
             }
         }
-
     }
 #endif
 
@@ -109,7 +108,7 @@ extension ExtensionsTest {
 
         #if !CI
             tests += [
-                ("testArrayInitHexPerformance", testArrayInitHexPerformance)
+                ("testArrayInitHexPerformance", testArrayInitHexPerformance),
             ]
         #endif
         return tests