Array+Extension.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. extension Array {
  16. @inlinable
  17. init(reserveCapacity: Int) {
  18. self = Array<Element>()
  19. self.reserveCapacity(reserveCapacity)
  20. }
  21. @inlinable
  22. var slice: ArraySlice<Element> {
  23. self[self.startIndex ..< self.endIndex]
  24. }
  25. }
  26. extension Array where Element == UInt8 {
  27. public init(hex: String) {
  28. self.init(reserveCapacity: hex.unicodeScalars.lazy.underestimatedCount)
  29. var buffer: UInt8?
  30. var skip = hex.hasPrefix("0x") ? 2 : 0
  31. for char in hex.unicodeScalars.lazy {
  32. guard skip == 0 else {
  33. skip -= 1
  34. continue
  35. }
  36. guard char.value >= 48 && char.value <= 102 else {
  37. removeAll()
  38. return
  39. }
  40. let v: UInt8
  41. let c: UInt8 = UInt8(char.value)
  42. switch c {
  43. case let c where c <= 57:
  44. v = c - 48
  45. case let c where c >= 65 && c <= 70:
  46. v = c - 55
  47. case let c where c >= 97:
  48. v = c - 87
  49. default:
  50. removeAll()
  51. return
  52. }
  53. if let b = buffer {
  54. append(b << 4 | v)
  55. buffer = nil
  56. } else {
  57. buffer = v
  58. }
  59. }
  60. if let b = buffer {
  61. append(b)
  62. }
  63. }
  64. public func toHexString() -> String {
  65. `lazy`.reduce(into: "") {
  66. var s = String($1, radix: 16)
  67. if s.count == 1 {
  68. s = "0" + s
  69. }
  70. $0 += s
  71. }
  72. }
  73. }
  74. extension Array where Element == UInt8 {
  75. /// split in chunks with given chunk size
  76. @available(*, deprecated)
  77. public func chunks(size chunksize: Int) -> Array<Array<Element>> {
  78. var words = Array<Array<Element>>()
  79. words.reserveCapacity(count / chunksize)
  80. for idx in stride(from: chunksize, through: count, by: chunksize) {
  81. words.append(Array(self[idx - chunksize ..< idx])) // slow for large table
  82. }
  83. let remainder = suffix(count % chunksize)
  84. if !remainder.isEmpty {
  85. words.append(Array(remainder))
  86. }
  87. return words
  88. }
  89. public func md5() -> [Element] {
  90. Digest.md5(self)
  91. }
  92. public func sha1() -> [Element] {
  93. Digest.sha1(self)
  94. }
  95. public func sha224() -> [Element] {
  96. Digest.sha224(self)
  97. }
  98. public func sha256() -> [Element] {
  99. Digest.sha256(self)
  100. }
  101. public func sha384() -> [Element] {
  102. Digest.sha384(self)
  103. }
  104. public func sha512() -> [Element] {
  105. Digest.sha512(self)
  106. }
  107. public func sha2(_ variant: SHA2.Variant) -> [Element] {
  108. Digest.sha2(self, variant: variant)
  109. }
  110. public func sha3(_ variant: SHA3.Variant) -> [Element] {
  111. Digest.sha3(self, variant: variant)
  112. }
  113. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
  114. Checksum.crc32(self, seed: seed, reflect: reflect)
  115. }
  116. public func crc32c(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
  117. Checksum.crc32c(self, seed: seed, reflect: reflect)
  118. }
  119. public func crc16(seed: UInt16? = nil) -> UInt16 {
  120. Checksum.crc16(self, seed: seed)
  121. }
  122. public func encrypt(cipher: Cipher) throws -> [Element] {
  123. try cipher.encrypt(self.slice)
  124. }
  125. public func decrypt(cipher: Cipher) throws -> [Element] {
  126. try cipher.decrypt(self.slice)
  127. }
  128. public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Element] {
  129. try authenticator.authenticate(self)
  130. }
  131. }