CSArrayType+Extensions.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // _ArrayType+Extensions.swift
  3. // CryptoSwift
  4. //
  5. // Copyright (C) 2014-2017 Krzyżanowski <marcin@krzyzanowskim.com>
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. //
  8. // In no event will the authors be held liable for any damages arising from the use of this software.
  9. //
  10. // 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:
  11. //
  12. // - 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.
  13. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. // - This notice may not be removed or altered from any source or binary distribution.
  15. //
  16. public protocol CSArrayType: RangeReplaceableCollection {
  17. func cs_arrayValue() -> [Iterator.Element]
  18. }
  19. extension Array: CSArrayType {
  20. public func cs_arrayValue() -> [Iterator.Element] {
  21. return self
  22. }
  23. }
  24. public extension CSArrayType where Iterator.Element == UInt8 {
  25. public func toHexString() -> String {
  26. return self.lazy.reduce("") {
  27. var s = String($1, radix: 16)
  28. if s.characters.count == 1 {
  29. s = "0" + s
  30. }
  31. return $0 + s
  32. }
  33. }
  34. }
  35. public extension CSArrayType where Iterator.Element == UInt8 {
  36. public func md5() -> [Iterator.Element] {
  37. return Digest.md5(cs_arrayValue())
  38. }
  39. public func sha1() -> [Iterator.Element] {
  40. return Digest.sha1(cs_arrayValue())
  41. }
  42. public func sha224() -> [Iterator.Element] {
  43. return Digest.sha224(cs_arrayValue())
  44. }
  45. public func sha256() -> [Iterator.Element] {
  46. return Digest.sha256(cs_arrayValue())
  47. }
  48. public func sha384() -> [Iterator.Element] {
  49. return Digest.sha384(cs_arrayValue())
  50. }
  51. public func sha512() -> [Iterator.Element] {
  52. return Digest.sha512(cs_arrayValue())
  53. }
  54. public func sha2(_ variant: SHA2.Variant) -> [Iterator.Element] {
  55. return Digest.sha2(cs_arrayValue(), variant: variant)
  56. }
  57. public func sha3(_ variant: SHA3.Variant) -> [Iterator.Element] {
  58. return Digest.sha3(cs_arrayValue(), variant: variant)
  59. }
  60. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> UInt32 {
  61. return Checksum.crc32(cs_arrayValue(), seed: seed, reflect: reflect)
  62. }
  63. public func crc16(seed: UInt16? = nil) -> UInt16 {
  64. return Checksum.crc16(cs_arrayValue(), seed: seed)
  65. }
  66. public func encrypt(cipher: Cipher) throws -> [Iterator.Element] {
  67. return try cipher.encrypt(cs_arrayValue().slice)
  68. }
  69. public func decrypt(cipher: Cipher) throws -> [Iterator.Element] {
  70. return try cipher.decrypt(cs_arrayValue().slice)
  71. }
  72. public func authenticate<A: Authenticator>(with authenticator: A) throws -> [Iterator.Element] {
  73. return try authenticator.authenticate(cs_arrayValue())
  74. }
  75. }