UInt64+Extension.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // UInt64Extension.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 02/09/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. /** array of bytes */
  9. extension UInt64 {
  10. @_specialize(ArraySlice<UInt8>)
  11. init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
  12. self = UInt64(bytes: bytes, fromIndex: bytes.startIndex)
  13. }
  14. @_specialize(ArraySlice<UInt8>)
  15. init<T: Collection>(bytes: T, fromIndex index: T.Index) where T.Iterator.Element == UInt8, T.Index == Int {
  16. let val0 = UInt64(bytes[index.advanced(by: 0)]) << 56
  17. let val1 = UInt64(bytes[index.advanced(by: 1)]) << 48
  18. let val2 = UInt64(bytes[index.advanced(by: 2)]) << 40
  19. let val3 = UInt64(bytes[index.advanced(by: 3)]) << 32
  20. let val4 = UInt64(bytes[index.advanced(by: 4)]) << 24
  21. let val5 = UInt64(bytes[index.advanced(by: 5)]) << 16
  22. let val6 = UInt64(bytes[index.advanced(by: 6)]) << 8
  23. let val7 = UInt64(bytes[index.advanced(by: 7)])
  24. self = val0 | val1 | val2 | val3 | val4 | val5 | val6 | val7
  25. }
  26. func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
  27. return arrayOfBytes(value: self, length: totalBytes)
  28. }
  29. }