NSData+Extension.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // PGPDataExtension.swift
  3. // SwiftPGP
  4. //
  5. // Created by Marcin Krzyzanowski on 05/07/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import Foundation
  9. extension NSMutableData {
  10. /** Convenient way to append bytes */
  11. internal func appendBytes(arrayOfBytes: [UInt8]) {
  12. self.appendBytes(arrayOfBytes, length: arrayOfBytes.count)
  13. }
  14. }
  15. extension NSData {
  16. /// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 65536
  17. public func checksum() -> UInt16 {
  18. var s:UInt32 = 0
  19. var bytesArray = self.arrayOfBytes()
  20. for i in 0..<bytesArray.count {
  21. s = s + UInt32(bytesArray[i])
  22. }
  23. s = s % 65536
  24. return UInt16(s)
  25. }
  26. @nonobjc public func md5() -> NSData {
  27. let result = Hash.md5(self.arrayOfBytes()).calculate()
  28. return NSData.withBytes(result)
  29. }
  30. public func sha1() -> NSData? {
  31. let result = Hash.sha1(self.arrayOfBytes()).calculate()
  32. return NSData.withBytes(result)
  33. }
  34. public func sha224() -> NSData? {
  35. let result = Hash.sha224(self.arrayOfBytes()).calculate()
  36. return NSData.withBytes(result)
  37. }
  38. public func sha256() -> NSData? {
  39. let result = Hash.sha256(self.arrayOfBytes()).calculate()
  40. return NSData.withBytes(result)
  41. }
  42. public func sha384() -> NSData? {
  43. let result = Hash.sha384(self.arrayOfBytes()).calculate()
  44. return NSData.withBytes(result)
  45. }
  46. public func sha512() -> NSData? {
  47. let result = Hash.sha512(self.arrayOfBytes()).calculate()
  48. return NSData.withBytes(result)
  49. }
  50. public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> NSData? {
  51. let result = Hash.crc32(self.arrayOfBytes(), seed: seed, reflect: reflect).calculate()
  52. return NSData.withBytes(result)
  53. }
  54. public func crc16(seed: UInt16? = nil) -> NSData? {
  55. let result = Hash.crc16(self.arrayOfBytes(), seed: seed).calculate()
  56. return NSData.withBytes(result)
  57. }
  58. public func encrypt(cipher: Cipher) throws -> NSData {
  59. let encrypted = try cipher.encrypt(self.arrayOfBytes())
  60. return NSData.withBytes(encrypted)
  61. }
  62. public func decrypt(cipher: Cipher) throws -> NSData {
  63. let decrypted = try cipher.decrypt(self.arrayOfBytes())
  64. return NSData.withBytes(decrypted)
  65. }
  66. public func authenticate(authenticator: Authenticator) throws -> NSData {
  67. let result = try authenticator.authenticate(self.arrayOfBytes())
  68. return NSData.withBytes(result)
  69. }
  70. }
  71. extension NSData {
  72. public func toHexString() -> String {
  73. return self.arrayOfBytes().toHexString()
  74. }
  75. public func arrayOfBytes() -> [UInt8] {
  76. let count = self.length / sizeof(UInt8)
  77. var bytesArray = [UInt8](count: count, repeatedValue: 0)
  78. self.getBytes(&bytesArray, length:count * sizeof(UInt8))
  79. return bytesArray
  80. }
  81. public convenience init(bytes: [UInt8]) {
  82. self.init(data: NSData.withBytes(bytes))
  83. }
  84. class public func withBytes(bytes: [UInt8]) -> NSData {
  85. return NSData(bytes: bytes, length: bytes.count)
  86. }
  87. }