Browse Source

Cleanup array extensions

Marcin Krzyżanowski 7 years ago
parent
commit
4d04659099

+ 0 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -46,7 +46,6 @@
 		75EC528F1EE8B81A0048EB3B /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC524C1EE8B6CA0048EB3B /* Cipher.swift */; };
 		75EC52901EE8B81A0048EB3B /* Collection+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC524D1EE8B6CA0048EB3B /* Collection+Extension.swift */; };
 		75EC52911EE8B81A0048EB3B /* Cryptors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC524E1EE8B6CA0048EB3B /* Cryptors.swift */; };
-		75EC52921EE8B81A0048EB3B /* Array+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC524F1EE8B6CA0048EB3B /* Array+Extensions.swift */; };
 		75EC52931EE8B81A0048EB3B /* Digest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52501EE8B6CA0048EB3B /* Digest.swift */; };
 		75EC52941EE8B81A0048EB3B /* DigestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52511EE8B6CA0048EB3B /* DigestType.swift */; };
 		75EC52951EE8B8200048EB3B /* AES+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52531EE8B6CA0048EB3B /* AES+Foundation.swift */; };
@@ -196,7 +195,6 @@
 		75EC524C1EE8B6CA0048EB3B /* Cipher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cipher.swift; sourceTree = "<group>"; };
 		75EC524D1EE8B6CA0048EB3B /* Collection+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Collection+Extension.swift"; sourceTree = "<group>"; };
 		75EC524E1EE8B6CA0048EB3B /* Cryptors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cryptors.swift; sourceTree = "<group>"; };
-		75EC524F1EE8B6CA0048EB3B /* Array+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+Extensions.swift"; sourceTree = "<group>"; };
 		75EC52501EE8B6CA0048EB3B /* Digest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Digest.swift; sourceTree = "<group>"; };
 		75EC52511EE8B6CA0048EB3B /* DigestType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DigestType.swift; sourceTree = "<group>"; };
 		75EC52531EE8B6CA0048EB3B /* AES+Foundation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AES+Foundation.swift"; sourceTree = "<group>"; };
@@ -347,7 +345,6 @@
 				75EC524C1EE8B6CA0048EB3B /* Cipher.swift */,
 				75EC524D1EE8B6CA0048EB3B /* Collection+Extension.swift */,
 				75EC524E1EE8B6CA0048EB3B /* Cryptors.swift */,
-				75EC524F1EE8B6CA0048EB3B /* Array+Extensions.swift */,
 				75EC52501EE8B6CA0048EB3B /* Digest.swift */,
 				75EC52511EE8B6CA0048EB3B /* DigestType.swift */,
 				75EC52521EE8B6CA0048EB3B /* Foundation */,
@@ -582,7 +579,6 @@
 				75EC52A91EE8B83D0048EB3B /* PKCS7Padding.swift in Sources */,
 				75EC52A51EE8B8290048EB3B /* Padding.swift in Sources */,
 				75EC527F1EE8B8130048EB3B /* BatchedCollection.swift in Sources */,
-				75EC52921EE8B81A0048EB3B /* Array+Extensions.swift in Sources */,
 				75EC52991EE8B8200048EB3B /* Data+Extension.swift in Sources */,
 				75EC52B61EE8B83D0048EB3B /* UInt8+Extension.swift in Sources */,
 				75EC52891EE8B8170048EB3B /* OFB.swift in Sources */,

+ 82 - 18
Sources/CryptoSwift/Array+Extension.swift

@@ -14,7 +14,8 @@
 //
 
 extension Array {
-    init(reserveCapacity: Int) {
+
+    public init(reserveCapacity: Int) {
         self = Array<Element>()
         self.reserveCapacity(reserveCapacity)
     }
@@ -22,24 +23,7 @@ extension Array {
     var slice: ArraySlice<Element> {
         return self[self.startIndex..<self.endIndex]
     }
-}
 
-extension Array {
-
-    /// split in chunks with given chunk size
-    @available(*, deprecated: 0.8.0, message: "")
-    public func chunks(size chunksize: Int) -> Array<Array<Element>> {
-        var words = Array<Array<Element>>()
-        words.reserveCapacity(count / chunksize)
-        for idx in stride(from: chunksize, through: count, by: chunksize) {
-            words.append(Array(self[idx - chunksize..<idx])) // slow for large table
-        }
-        let remainder = suffix(count % chunksize)
-        if !remainder.isEmpty {
-            words.append(Array(remainder))
-        }
-        return words
-    }
 }
 
 extension Array where Element == UInt8 {
@@ -81,4 +65,84 @@ extension Array where Element == UInt8 {
             append(b)
         }
     }
+
+    public func toHexString() -> String {
+        return `lazy`.reduce("") {
+            var s = String($1, radix: 16)
+            if s.count == 1 {
+                s = "0" + s
+            }
+            return $0 + s
+        }
+    }
+}
+
+extension Array where Element == UInt8 {
+
+    /// split in chunks with given chunk size
+    @available(*, deprecated: 0.8.0, message: "")
+    public func chunks(size chunksize: Int) -> Array<Array<Element>> {
+        var words = Array<Array<Element>>()
+        words.reserveCapacity(count / chunksize)
+        for idx in stride(from: chunksize, through: count, by: chunksize) {
+            words.append(Array(self[idx - chunksize..<idx])) // slow for large table
+        }
+        let remainder = suffix(count % chunksize)
+        if !remainder.isEmpty {
+            words.append(Array(remainder))
+        }
+        return words
+    }
+
+    public func md5() -> [Element] {
+        return Digest.md5(self)
+    }
+
+    public func sha1() -> [Element] {
+        return Digest.sha1(self)
+    }
+
+    public func sha224() -> [Element] {
+        return Digest.sha224(self)
+    }
+
+    public func sha256() -> [Element] {
+        return Digest.sha256(self)
+    }
+
+    public func sha384() -> [Element] {
+        return Digest.sha384(self)
+    }
+
+    public func sha512() -> [Element] {
+        return Digest.sha512(self)
+    }
+
+    public func sha2(_ variant: SHA2.Variant) -> [Element] {
+        return Digest.sha2(self, variant: variant)
+    }
+
+    public func sha3(_ variant: SHA3.Variant) -> [Element] {
+        return Digest.sha3(self, variant: variant)
+    }
+
+    public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
+        return Checksum.crc32(self, seed: seed, reflect: reflect)
+    }
+
+    public func crc16(seed: UInt16? = nil) -> UInt16 {
+        return Checksum.crc16(self, seed: seed)
+    }
+
+    public func encrypt(cipher: Cipher) throws -> [Element] {
+        return try cipher.encrypt(slice)
+    }
+
+    public func decrypt(cipher: Cipher) throws -> [Element] {
+        return try cipher.decrypt(slice)
+    }
+
+    public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Element] {
+        return try authenticator.authenticate(self)
+    }
 }

+ 0 - 82
Sources/CryptoSwift/Array+Extensions.swift

@@ -1,82 +0,0 @@
-//
-//  CryptoSwift
-//
-//  Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
-//  This software is provided 'as-is', without any express or implied warranty.
-//
-//  In no event will the authors be held liable for any damages arising from the use of this software.
-//
-//  Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
-//
-//  - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
-//  - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
-//  - This notice may not be removed or altered from any source or binary distribution.
-//
-
-public extension Array where Element == UInt8 {
-
-    public func toHexString() -> String {
-        return `lazy`.reduce("") {
-            var s = String($1, radix: 16)
-            if s.count == 1 {
-                s = "0" + s
-            }
-            return $0 + s
-        }
-    }
-}
-
-public extension Array where Element == UInt8 {
-
-    public func md5() -> [Element] {
-        return Digest.md5(self)
-    }
-
-    public func sha1() -> [Element] {
-        return Digest.sha1(self)
-    }
-
-    public func sha224() -> [Element] {
-        return Digest.sha224(self)
-    }
-
-    public func sha256() -> [Element] {
-        return Digest.sha256(self)
-    }
-
-    public func sha384() -> [Element] {
-        return Digest.sha384(self)
-    }
-
-    public func sha512() -> [Element] {
-        return Digest.sha512(self)
-    }
-
-    public func sha2(_ variant: SHA2.Variant) -> [Element] {
-        return Digest.sha2(self, variant: variant)
-    }
-
-    public func sha3(_ variant: SHA3.Variant) -> [Element] {
-        return Digest.sha3(self, variant: variant)
-    }
-
-    public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
-        return Checksum.crc32(self, seed: seed, reflect: reflect)
-    }
-
-    public func crc16(seed: UInt16? = nil) -> UInt16 {
-        return Checksum.crc16(self, seed: seed)
-    }
-
-    public func encrypt(cipher: Cipher) throws -> [Element] {
-        return try cipher.encrypt(slice)
-    }
-
-    public func decrypt(cipher: Cipher) throws -> [Element] {
-        return try cipher.decrypt(slice)
-    }
-
-    public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Element] {
-        return try authenticator.authenticate(self)
-    }
-}