// // Collection+Extension.swift // CryptoSwift // // Created by Marcin Krzyzanowski on 02/08/16. // Copyright © 2016 Marcin Krzyzanowski. All rights reserved. // extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int { func toUInt32Array() -> Array { var result = Array() result.reserveCapacity(16) for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout.size) { var val: UInt32 = 0 val |= self.count > 3 ? UInt32(self[idx.advanced(by: 3)]) << 24 : 0 val |= self.count > 2 ? UInt32(self[idx.advanced(by: 2)]) << 16 : 0 val |= self.count > 1 ? UInt32(self[idx.advanced(by: 1)]) << 8 : 0 val |= self.count > 0 ? UInt32(self[idx]) : 0 result.append(val) } for _ in result.count...size { result.append(0) } return result } func toUInt64Array() -> Array { var result = Array() result.reserveCapacity(32) for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout.size) { var val:UInt64 = 0 val |= self.count > 7 ? UInt64(self[idx.advanced(by: 7)]) << 56 : 0 val |= self.count > 6 ? UInt64(self[idx.advanced(by: 6)]) << 48 : 0 val |= self.count > 5 ? UInt64(self[idx.advanced(by: 5)]) << 40 : 0 val |= self.count > 4 ? UInt64(self[idx.advanced(by: 4)]) << 32 : 0 val |= self.count > 3 ? UInt64(self[idx.advanced(by: 3)]) << 24 : 0 val |= self.count > 2 ? UInt64(self[idx.advanced(by: 2)]) << 16 : 0 val |= self.count > 1 ? UInt64(self[idx.advanced(by: 1)]) << 8 : 0 val |= self.count > 0 ? UInt64(self[idx.advanced(by: 0)]) << 0 : 0 result.append(val) } for _ in result.count...size { result.append(0) } return result } /// Initialize integer from array of bytes. Caution: may be slow! func toInteger() -> 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 < MemoryLayout.size { let paddingCount = MemoryLayout.size - bytes.count if (paddingCount > 0) { bytes += Array(repeating: 0, count: paddingCount) } } if MemoryLayout.size == 1 { return T(truncatingBitPattern: UInt64(bytes[0])) } var result: T = 0 for byte in bytes.reversed() { result = result << 8 | T(byte) } return result } }