Przeglądaj źródła

Refactor Int.with() -> Int(bytes:)

Marcin Krzyżanowski 9 lat temu
rodzic
commit
3a0374acd1

+ 2 - 2
CryptoSwiftTests/ExtensionsTest.swift

@@ -31,13 +31,13 @@ final class ExtensionsTest: XCTestCase {
     func testIntExtension() {
         let i1:Int = 1024
         let i1Array = i1.bytes(totalBytes: 32 / 8) // 32 bit
-        let i1recovered = Int.with(i1Array)
+        let i1recovered = Int(bytes: i1Array)
         
         XCTAssertEqual(i1, i1recovered, "Bytes conversion failed")
         
         let i2:Int = 1024
         let i2Array = i2.bytes(totalBytes: 160 / 8) // 160 bit
-        let i2recovered = Int.with(i2Array)
+        let i2recovered = Int(bytes: i2Array)
         
         XCTAssertEqual(i2, i2recovered, "Bytes conversion failed")
     }

+ 2 - 2
Sources/CryptoSwift/AES.swift

@@ -286,7 +286,7 @@ fileprivate extension AES {
             var arr = Array<UInt32>()
             for idx in stride(from: expanded.startIndex, to: expanded.endIndex, by: 4) {
                 let four = Array(expanded[idx..<idx.advanced(by: 4)].reversed())
-                let num = UInt32.with(four)
+                let num = UInt32(bytes: four)
                 arr.append(num)
             }
 
@@ -326,7 +326,7 @@ fileprivate extension AES {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
             }
             if ((i % variant.Nk) == 0) {
-                tmp = subWord(rotateLeft(UInt32.with(tmp), by: 8).bytes(totalBytes: MemoryLayout<UInt32>.size))
+                tmp = subWord(rotateLeft(UInt32(bytes: tmp), by: 8).bytes(totalBytes: MemoryLayout<UInt32>.size))
                 tmp[0] = tmp.first! ^ Rcon[i/variant.Nk]
             } else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
                 tmp = subWord(tmp)

+ 1 - 1
Sources/CryptoSwift/BlockMode/CTR.swift

@@ -40,6 +40,6 @@ private func buildNonce(_ iv: Array<UInt8>, counter: UInt64) -> Array<UInt8> {
     let noncePartLen = AES.blockSize / 2
     let noncePrefix = Array(iv[0..<noncePartLen])
     let nonceSuffix = Array(iv[noncePartLen..<iv.count])
-    let c = UInt64.with(nonceSuffix) + counter
+    let c = UInt64(bytes: nonceSuffix) + counter
     return noncePrefix + arrayOfBytes(value: c)
 }

+ 6 - 6
Sources/CryptoSwift/Int+Extension.swift

@@ -30,14 +30,14 @@ extension Int {
 
 /* array of bytes */
 extension Int {
-    /** Array of bytes with optional padding (little-endian) */
-    public func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
+    /** Int with collection of bytes (little-endian) */
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
     }
 
-    /** Int with collection of bytes (little-endian) */
-    public static func with<T: Collection>(_ bytes: T) -> Int where T.Iterator.Element == UInt8, T.Index == Int {
-        return bytes.toInteger()
+    /** Array of bytes with optional padding (little-endian) */
+    func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
     }
 }
 

+ 4 - 4
Sources/CryptoSwift/Rabbit.swift

@@ -100,10 +100,10 @@ final public class Rabbit: BlockCipher {
     private func setupIV(_ iv: Array<UInt8>) {
         // 63...56 55...48 47...40 39...32 31...24 23...16 15...8 7...0 IV bits
         //    0       1       2       3       4       5       6     7   IV bytes in array
-        let iv0 = UInt32.with([iv[4], iv[5], iv[6], iv[7]])
-        let iv1 = UInt32.with([iv[0], iv[1], iv[4], iv[5]])
-        let iv2 = UInt32.with([iv[0], iv[1], iv[2], iv[3]])
-        let iv3 = UInt32.with([iv[2], iv[3], iv[6], iv[7]])
+        let iv0 = UInt32(bytes: [iv[4], iv[5], iv[6], iv[7]])
+        let iv1 = UInt32(bytes: [iv[0], iv[1], iv[4], iv[5]])
+        let iv2 = UInt32(bytes: [iv[0], iv[1], iv[2], iv[3]])
+        let iv3 = UInt32(bytes: [iv[2], iv[3], iv[6], iv[7]])
         
         // Modify the counter state as function of the IV
         c[0] = c[0] ^ iv0

+ 4 - 5
Sources/CryptoSwift/UInt16+Extension.swift

@@ -8,12 +8,11 @@
 
 /** array of bytes */
 extension UInt16 {
-    public func bytes(totalBytes: Int = MemoryLayout<UInt16>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
     }
 
-    /** Int with array bytes (little-endian) */
-    public static func with<T: Collection>(_ bytes: T) -> UInt32 where T.Iterator.Element == UInt8, T.Index == Int {
-        return bytes.toInteger()
+    func bytes(totalBytes: Int = MemoryLayout<UInt16>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
     }
 }

+ 4 - 5
Sources/CryptoSwift/UInt32+Extension.swift

@@ -18,13 +18,12 @@ extension UInt32: _UInt32Type {}
 
 /** array of bytes */
 extension UInt32 {
-    public func bytes(totalBytes: Int = MemoryLayout<UInt32>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
     }
 
-    /** Int with array bytes (little-endian) */
-    public static func with<T: Collection>(_ bytes: T) -> UInt32 where T.Iterator.Element == UInt8, T.Index == Int {
-        return bytes.toInteger()
+    func bytes(totalBytes: Int = MemoryLayout<UInt32>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
     }
 }
 

+ 4 - 5
Sources/CryptoSwift/UInt64+Extension.swift

@@ -8,12 +8,11 @@
 
 /** array of bytes */
 extension UInt64 {
-    public func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
+    init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
+        self = bytes.toInteger()
     }
 
-    /** Int with array bytes (little-endian) */
-    public static func with<T: Collection>(_ bytes: T) -> UInt64 where T.Iterator.Element == UInt8, T.Index == Int {
-        return bytes.toInteger()
+    func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
+        return arrayOfBytes(value: self, length: totalBytes)
     }
 }