Array+Extension.swift 4.5 KB

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