Array+Extension.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2022 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. @inlinable
  26. subscript (safe index: Index) -> Element? {
  27. return indices.contains(index) ? self[index] : nil
  28. }
  29. }
  30. extension Array where Element == UInt8 {
  31. public init(hex: String) {
  32. self.init(reserveCapacity: hex.unicodeScalars.lazy.underestimatedCount)
  33. var buffer: UInt8?
  34. var skip = hex.hasPrefix("0x") ? 2 : 0
  35. for char in hex.unicodeScalars.lazy {
  36. guard skip == 0 else {
  37. skip -= 1
  38. continue
  39. }
  40. guard char.value >= 48 && char.value <= 102 else {
  41. removeAll()
  42. return
  43. }
  44. let v: UInt8
  45. let c: UInt8 = UInt8(char.value)
  46. switch c {
  47. case let c where c <= 57:
  48. v = c - 48
  49. case let c where c >= 65 && c <= 70:
  50. v = c - 55
  51. case let c where c >= 97:
  52. v = c - 87
  53. default:
  54. removeAll()
  55. return
  56. }
  57. if let b = buffer {
  58. append(b << 4 | v)
  59. buffer = nil
  60. } else {
  61. buffer = v
  62. }
  63. }
  64. if let b = buffer {
  65. append(b)
  66. }
  67. }
  68. public func toHexString() -> String {
  69. `lazy`.reduce(into: "") {
  70. var s = String($1, radix: 16)
  71. if s.count == 1 {
  72. s = "0" + s
  73. }
  74. $0 += s
  75. }
  76. }
  77. }
  78. extension Array where Element == UInt8 {
  79. /// split in chunks with given chunk size
  80. @available(*, deprecated)
  81. public func chunks(size chunksize: Int) -> Array<Array<Element>> {
  82. var words = Array<Array<Element>>()
  83. words.reserveCapacity(count / chunksize)
  84. for idx in stride(from: chunksize, through: count, by: chunksize) {
  85. words.append(Array(self[idx - chunksize ..< idx])) // slow for large table
  86. }
  87. let remainder = suffix(count % chunksize)
  88. if !remainder.isEmpty {
  89. words.append(Array(remainder))
  90. }
  91. return words
  92. }
  93. public func md5() -> [Element] {
  94. Digest.md5(self)
  95. }
  96. public func sha1() -> [Element] {
  97. Digest.sha1(self)
  98. }
  99. public func sha224() -> [Element] {
  100. Digest.sha224(self)
  101. }
  102. public func sha256() -> [Element] {
  103. Digest.sha256(self)
  104. }
  105. public func sha384() -> [Element] {
  106. Digest.sha384(self)
  107. }
  108. public func sha512() -> [Element] {
  109. Digest.sha512(self)
  110. }
  111. public func sha2(_ variant: SHA2.Variant) -> [Element] {
  112. Digest.sha2(self, variant: variant)
  113. }
  114. public func sha3(_ variant: SHA3.Variant) -> [Element] {
  115. Digest.sha3(self, variant: variant)
  116. }
  117. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
  118. Checksum.crc32(self, seed: seed, reflect: reflect)
  119. }
  120. public func crc32c(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
  121. Checksum.crc32c(self, seed: seed, reflect: reflect)
  122. }
  123. public func crc16(seed: UInt16? = nil) -> UInt16 {
  124. Checksum.crc16(self, seed: seed)
  125. }
  126. public func encrypt(cipher: Cipher) throws -> [Element] {
  127. try cipher.encrypt(self.slice)
  128. }
  129. public func decrypt(cipher: Cipher) throws -> [Element] {
  130. try cipher.decrypt(self.slice)
  131. }
  132. public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Element] {
  133. try authenticator.authenticate(self)
  134. }
  135. }