NSDataExtension.swift 2.7 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. public func checksum() -> UInt16 {
  17. var s:UInt32 = 0;
  18. var bytesArray = self.arrayOfBytes()
  19. for (var i = 0; i < bytesArray.count; i++) {
  20. _ = bytesArray[i]
  21. s = s + UInt32(bytesArray[i])
  22. }
  23. s = s % 65536;
  24. return UInt16(s);
  25. }
  26. public func md5() -> NSData? {
  27. return Hash.md5(self).calculate()
  28. }
  29. public func sha1() -> NSData? {
  30. return Hash.sha1(self).calculate()
  31. }
  32. public func sha224() -> NSData? {
  33. return Hash.sha224(self).calculate()
  34. }
  35. public func sha256() -> NSData? {
  36. return Hash.sha256(self).calculate()
  37. }
  38. public func sha384() -> NSData? {
  39. return Hash.sha384(self).calculate()
  40. }
  41. public func sha512() -> NSData? {
  42. return Hash.sha512(self).calculate()
  43. }
  44. public func crc32() -> NSData? {
  45. return Hash.crc32(self).calculate()
  46. }
  47. public func encrypt(cipher: Cipher) throws -> NSData? {
  48. let encrypted = try cipher.encrypt(self.arrayOfBytes())
  49. return NSData.withBytes(encrypted)
  50. }
  51. public func decrypt(cipher: Cipher) throws -> NSData? {
  52. let decrypted = try cipher.decrypt(self.arrayOfBytes())
  53. return NSData.withBytes(decrypted)
  54. }
  55. public func authenticate(authenticator: Authenticator) -> NSData? {
  56. if let result = authenticator.authenticate(self.arrayOfBytes()) {
  57. return NSData.withBytes(result)
  58. }
  59. return nil
  60. }
  61. }
  62. extension NSData {
  63. public var hexString: String {
  64. return self.toHexString()
  65. }
  66. func toHexString() -> String {
  67. let count = self.length / sizeof(UInt8)
  68. var bytesArray = [UInt8](count: count, repeatedValue: 0)
  69. self.getBytes(&bytesArray, length:count * sizeof(UInt8))
  70. var s:String = "";
  71. for byte in bytesArray {
  72. s = s + String(format:"%02x", byte)
  73. }
  74. return s
  75. }
  76. public func arrayOfBytes() -> [UInt8] {
  77. let count = self.length / sizeof(UInt8)
  78. var bytesArray = [UInt8](count: count, repeatedValue: 0)
  79. self.getBytes(&bytesArray, length:count * sizeof(UInt8))
  80. return bytesArray
  81. }
  82. class public func withBytes(bytes: [UInt8]) -> NSData {
  83. return NSData(bytes: bytes, length: bytes.count)
  84. }
  85. }