NSDataExtension.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. var b = 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) -> NSData? {
  48. if let encrypted = cipher.encrypt(self.arrayOfBytes()) {
  49. return NSData.withBytes(encrypted)
  50. }
  51. return nil
  52. }
  53. public func decrypt(cipher: Cipher) -> NSData? {
  54. if let decrypted = cipher.decrypt(self.arrayOfBytes()) {
  55. return NSData.withBytes(decrypted)
  56. }
  57. return nil;
  58. }
  59. public func authenticate(authenticator: Authenticator) -> NSData? {
  60. if let result = authenticator.authenticate(self.arrayOfBytes()) {
  61. return NSData.withBytes(result)
  62. }
  63. return nil
  64. }
  65. }
  66. extension NSData {
  67. public var hexString: String {
  68. return self.toHexString()
  69. }
  70. func toHexString() -> String {
  71. let count = self.length / sizeof(UInt8)
  72. var bytesArray = [UInt8](count: count, repeatedValue: 0)
  73. self.getBytes(&bytesArray, length:count * sizeof(UInt8))
  74. var s:String = "";
  75. for byte in bytesArray {
  76. s = s + String(format:"%02X", byte)
  77. }
  78. return s.lowercaseString;
  79. }
  80. public func arrayOfBytes() -> [UInt8] {
  81. let count = self.length / sizeof(UInt8)
  82. var bytesArray = [UInt8](count: count, repeatedValue: 0)
  83. self.getBytes(&bytesArray, length:count * sizeof(UInt8))
  84. return bytesArray
  85. }
  86. class public func withBytes(bytes: [UInt8]) -> NSData {
  87. return NSData(bytes: bytes, length: bytes.count)
  88. }
  89. }