Browse Source

Don't use arrayOfBytes directly, only via extension where it is specialised, hence optimised to performance.

Marcin Krzyżanowski 9 years ago
parent
commit
3de4268b98

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

@@ -41,5 +41,5 @@ private func buildNonce(_ iv: Array<UInt8>, counter: UInt64) -> Array<UInt8> {
     let noncePrefix = Array(iv[0..<noncePartLen])
     let nonceSuffix = Array(iv[noncePartLen..<iv.count])
     let c = UInt64(bytes: nonceSuffix) + counter
-    return noncePrefix + arrayOfBytes(value: c)
+    return noncePrefix + c.bytes()
 }

+ 7 - 5
Sources/CryptoSwift/Generics.swift

@@ -33,11 +33,13 @@ func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T
     return bitPattern
 }
 
-/// 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> {
-    let totalBytes = length ?? MemoryLayout<T>.size
-
+/// Array of bytes. Caution: don't use directly because generic is slow.
+///
+/// - parameter value: integer value
+/// - parameter length: length of output array. By default size of value type
+///
+/// - returns: Array of bytes
+func arrayOfBytes<T: Integer>(value:T, length totalBytes: Int = MemoryLayout<T>.size) -> Array<UInt8> {
     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
     valuePointer.pointee = value
 

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

@@ -35,8 +35,8 @@ extension Int {
     //    self = bytes.toInteger()
     //}
 
-    /** Array of bytes with optional padding (little-endian) */
+    /** Array of bytes with optional padding */
     func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
+         return arrayOfBytes(value: self, length: totalBytes)
     }
 }

+ 1 - 1
Sources/CryptoSwift/SHA3.swift

@@ -263,7 +263,7 @@ extension SHA3: Updatable {
 
         //TODO: verify performance, reduce vs for..in
         let result = self.accumulatedHash.reduce(Array<UInt8>()) { (result, value) -> Array<UInt8> in
-            return result + arrayOfBytes(value: value.bigEndian)
+            return result + value.bigEndian.bytes()
         }
 
         // reset hash value for instance