Browse Source

Refactor freestanding generic functions to Collection

Marcin Krzyżanowski 9 years ago
parent
commit
2025d83b7a

+ 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(bytes: four)
+                let num = UInt32.with(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(bytes: tmp), by: 8).bytes(totalBytes: sizeof(UInt32.self)))
+                tmp = subWord(rotateLeft(UInt32.with(tmp), by: 8).bytes(totalBytes: sizeof(UInt32.self)))
                 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(bytes: nonceSuffix) + counter
+    let c = UInt64.with(nonceSuffix) + counter
     return noncePrefix + arrayOfBytes(value: c)
 }

+ 25 - 0
Sources/CryptoSwift/Collection+Extension.swift

@@ -38,4 +38,29 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
         }
         return result
     }
+
+    /// Initialize integer from array of bytes. Caution: may be slow!
+    func toInteger<T:Integer>() -> T where T: ByteConvertible, T: BitshiftOperationsType {
+        if self.count == 0 {
+            return 0;
+        }
+
+        var bytes = self.reversed() //FIXME: check it this is equivalent of Array(...)
+        if bytes.count < sizeof(T.self) {
+            let paddingCount = sizeof(T.self) - bytes.count
+            if (paddingCount > 0) {
+                bytes += Array<UInt8>(repeating: 0, count: paddingCount)
+            }
+        }
+
+        if sizeof(T.self) == 1 {
+            return T(truncatingBitPattern: UInt64(bytes[0]))
+        }
+
+        var result: T = 0
+        for byte in bytes.reversed() {
+            result = result << 8 | T(byte)
+        }
+        return result
+    }
 }

+ 1 - 24
Sources/CryptoSwift/Generics.swift

@@ -33,29 +33,6 @@ func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T
     return bitPattern
 }
 
-/// Initialize integer from array of bytes.
-/// This method may be slow
-@_specialize(UInt32)
-func integerWith<T:Integer>(_ bytes: Array<UInt8>) -> T where T:ByteConvertible, T: BitshiftOperationsType {
-    var bytes = bytes.reversed() as Array<UInt8> //FIXME: check it this is equivalent of Array(...)
-    if bytes.count < sizeof(T.self) {
-        let paddingCount = sizeof(T.self) - bytes.count
-        if (paddingCount > 0) {
-            bytes += Array<UInt8>(repeating: 0, count: paddingCount)
-        }
-    }
-    
-    if sizeof(T.self) == 1 {
-        return T(truncatingBitPattern: UInt64(bytes.first!))
-    }
-    
-    var result: T = 0
-    for byte in bytes.reversed() {
-        result = result << 8 | T(byte)
-    }
-    return result
-}
-
 /// Array of bytes, little-endian representation. Don't use if not necessary.
 /// I found this method slow
 func arrayOfBytes<T>(value:T, length:Int? = nil) -> Array<UInt8> {
@@ -78,7 +55,7 @@ func arrayOfBytes<T>(value:T, length:Int? = nil) -> Array<UInt8> {
 
 // MARK: - shiftLeft
 
-// helper to be able tomake shift operation on T
+// helper to be able to make shift operation on T
 @_specialize(Int)
 func << <T:SignedInteger>(lhs: T, rhs: Int) -> Int {
     let a = lhs as! Int

+ 3 - 7
Sources/CryptoSwift/IntExtension.swift

@@ -35,13 +35,9 @@ extension Int {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
-    public static func with(_ bytes: ArraySlice<UInt8>) -> Int {
-        return integerWith(Array(bytes))
-    }
-
-    /** Int with array bytes (little-endian) */
-    public static func with(_ bytes: Array<UInt8>) -> Int {
-        return integerWith(bytes)
+    /** 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()
     }
 }
 

+ 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 = integerWith([iv[4], iv[5], iv[6], iv[7]])
-        let iv1: UInt32 = integerWith([iv[0], iv[1], iv[4], iv[5]])
-        let iv2: UInt32 = integerWith([iv[0], iv[1], iv[2], iv[3]])
-        let iv3: UInt32 = integerWith([iv[2], iv[3], iv[6], iv[7]])
+        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]])
         
         // Modify the counter state as function of the IV
         c[0] = c[0] ^ iv0

+ 2 - 6
Sources/CryptoSwift/UInt32Extension.swift

@@ -22,13 +22,9 @@ extension UInt32 {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
-    public static func with(bytes: ArraySlice<UInt8>) -> UInt32 {
-        return integerWith(Array(bytes))
-    }
-
     /** Int with array bytes (little-endian) */
-    public static func with(bytes: Array<UInt8>) -> UInt32 {
-        return integerWith(bytes)
+    public static func with<T: Collection>(_ bytes: T) -> UInt32 where T.Iterator.Element == UInt8, T.Index == Int {
+        return bytes.toInteger()
     }
 }
 

+ 2 - 6
Sources/CryptoSwift/UInt64Extension.swift

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