Data+Extension.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. import Foundation
  16. extension Data {
  17. /// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 65536
  18. public func checksum() -> UInt16 {
  19. var s: UInt32 = 0
  20. var bytesArray = bytes
  21. for i in 0..<bytesArray.count {
  22. s = s + UInt32(bytesArray[i])
  23. }
  24. s = s % 65536
  25. return UInt16(s)
  26. }
  27. public func md5() -> Data {
  28. return Data(bytes: Digest.md5(bytes))
  29. }
  30. public func sha1() -> Data {
  31. return Data(bytes: Digest.sha1(bytes))
  32. }
  33. public func sha224() -> Data {
  34. return Data(bytes: Digest.sha224(bytes))
  35. }
  36. public func sha256() -> Data {
  37. return Data(bytes: Digest.sha256(bytes))
  38. }
  39. public func sha384() -> Data {
  40. return Data(bytes: Digest.sha384(bytes))
  41. }
  42. public func sha512() -> Data {
  43. return Data(bytes: Digest.sha512(bytes))
  44. }
  45. public func sha3(_ variant: SHA3.Variant) -> Data {
  46. return Data(bytes: Digest.sha3(bytes, variant: variant))
  47. }
  48. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> Data {
  49. return Data(bytes: Checksum.crc32(bytes, seed: seed, reflect: reflect).bytes())
  50. }
  51. public func crc16(seed: UInt16? = nil) -> Data {
  52. return Data(bytes: Checksum.crc16(bytes, seed: seed).bytes())
  53. }
  54. public func encrypt(cipher: Cipher) throws -> Data {
  55. return Data(bytes: try cipher.encrypt(bytes.slice))
  56. }
  57. public func decrypt(cipher: Cipher) throws -> Data {
  58. return Data(bytes: try cipher.decrypt(bytes.slice))
  59. }
  60. public func authenticate(with authenticator: Authenticator) throws -> Data {
  61. return Data(bytes: try authenticator.authenticate(bytes))
  62. }
  63. }
  64. extension Data {
  65. public init(hex: String) {
  66. self.init(bytes: Array<UInt8>(hex: hex))
  67. }
  68. public var bytes: Array<UInt8> {
  69. return Array(self)
  70. }
  71. public func toHexString() -> String {
  72. return bytes.toHexString()
  73. }
  74. }