Data+Extension.swift 2.9 KB

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