NSData+CommonCrypto.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * NSData+CommonCrypto.h
  3. * AQToolkit
  4. *
  5. * Created by Jim Dovey on 31/8/2008.
  6. *
  7. * Copyright (c) 2008-2009, Jim Dovey
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. *
  17. * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * Neither the name of this project's author nor the names of its
  22. * contributors may be used to endorse or promote products derived from
  23. * this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  31. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #import <Foundation/NSData.h>
  39. #import <Foundation/NSError.h>
  40. #import <CommonCrypto/CommonCryptor.h>
  41. #import <CommonCrypto/CommonHMAC.h>
  42. extern NSString * const kCommonCryptoErrorDomain;
  43. @interface NSError (CommonCryptoErrorDomain)
  44. + (NSError *) errorWithCCCryptorStatus: (CCCryptorStatus) status;
  45. @end
  46. @interface NSData (CommonDigest)
  47. - (NSData *) MD2Sum;
  48. - (NSData *) MD4Sum;
  49. - (NSData *) MD5Sum;
  50. - (NSData *) SHA1Hash;
  51. - (NSData *) SHA224Hash;
  52. - (NSData *) SHA256Hash;
  53. - (NSData *) SHA384Hash;
  54. - (NSData *) SHA512Hash;
  55. @end
  56. @interface NSData (CommonCryptor)
  57. - (NSData *) AES256EncryptedDataUsingKey: (id) key error: (NSError **) error;
  58. - (NSData *) decryptedAES256DataUsingKey: (id) key error: (NSError **) error;
  59. - (NSData *) DESEncryptedDataUsingKey: (id) key error: (NSError **) error;
  60. - (NSData *) decryptedDESDataUsingKey: (id) key error: (NSError **) error;
  61. - (NSData *) CASTEncryptedDataUsingKey: (id) key error: (NSError **) error;
  62. - (NSData *) decryptedCASTDataUsingKey: (id) key error: (NSError **) error;
  63. @end
  64. @interface NSData (LowLevelCommonCryptor)
  65. - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
  66. key: (id) key // data or string
  67. error: (CCCryptorStatus *) error;
  68. - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
  69. key: (id) key // data or string
  70. options: (CCOptions) options
  71. error: (CCCryptorStatus *) error;
  72. - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
  73. key: (id) key // data or string
  74. initializationVector: (id) iv // data or string
  75. options: (CCOptions) options
  76. error: (CCCryptorStatus *) error;
  77. - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
  78. key: (id) key // data or string
  79. error: (CCCryptorStatus *) error;
  80. - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
  81. key: (id) key // data or string
  82. options: (CCOptions) options
  83. error: (CCCryptorStatus *) error;
  84. - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
  85. key: (id) key // data or string
  86. initializationVector: (id) iv // data or string
  87. options: (CCOptions) options
  88. error: (CCCryptorStatus *) error;
  89. @end
  90. @interface NSData (CommonHMAC)
  91. - (NSData *) HMACWithAlgorithm: (CCHmacAlgorithm) algorithm;
  92. - (NSData *) HMACWithAlgorithm: (CCHmacAlgorithm) algorithm key: (id) key;
  93. @end