NSData+YYAdd.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // NSData+YYAdd.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 13/4/4.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Provide hash, encrypt, encode and some common method for `NSData`.
  15. */
  16. @interface NSData (YYAdd)
  17. #pragma mark - Hash
  18. ///=============================================================================
  19. /// @name Hash
  20. ///=============================================================================
  21. /**
  22. Returns a lowercase NSString for md2 hash.
  23. */
  24. - (NSString *)md2String;
  25. /**
  26. Returns an NSData for md2 hash.
  27. */
  28. - (NSData *)md2Data;
  29. /**
  30. Returns a lowercase NSString for md4 hash.
  31. */
  32. - (NSString *)md4String;
  33. /**
  34. Returns an NSData for md4 hash.
  35. */
  36. - (NSData *)md4Data;
  37. /**
  38. Returns a lowercase NSString for md5 hash.
  39. */
  40. - (NSString *)md5String;
  41. /**
  42. Returns an NSData for md5 hash.
  43. */
  44. - (NSData *)md5Data;
  45. /**
  46. Returns a lowercase NSString for sha1 hash.
  47. */
  48. - (NSString *)sha1String;
  49. /**
  50. Returns an NSData for sha1 hash.
  51. */
  52. - (NSData *)sha1Data;
  53. /**
  54. Returns a lowercase NSString for sha224 hash.
  55. */
  56. - (NSString *)sha224String;
  57. /**
  58. Returns an NSData for sha224 hash.
  59. */
  60. - (NSData *)sha224Data;
  61. /**
  62. Returns a lowercase NSString for sha256 hash.
  63. */
  64. - (NSString *)sha256String;
  65. /**
  66. Returns an NSData for sha256 hash.
  67. */
  68. - (NSData *)sha256Data;
  69. /**
  70. Returns a lowercase NSString for sha384 hash.
  71. */
  72. - (NSString *)sha384String;
  73. /**
  74. Returns an NSData for sha384 hash.
  75. */
  76. - (NSData *)sha384Data;
  77. /**
  78. Returns a lowercase NSString for sha512 hash.
  79. */
  80. - (NSString *)sha512String;
  81. /**
  82. Returns an NSData for sha512 hash.
  83. */
  84. - (NSData *)sha512Data;
  85. /**
  86. Returns a lowercase NSString for hmac using algorithm md5 with key.
  87. @param key The hmac key.
  88. */
  89. - (NSString *)hmacMD5StringWithKey:(NSString *)key;
  90. /**
  91. Returns an NSData for hmac using algorithm md5 with key.
  92. @param key The hmac key.
  93. */
  94. - (NSData *)hmacMD5DataWithKey:(NSData *)key;
  95. /**
  96. Returns a lowercase NSString for hmac using algorithm sha1 with key.
  97. @param key The hmac key.
  98. */
  99. - (NSString *)hmacSHA1StringWithKey:(NSString *)key;
  100. /**
  101. Returns an NSData for hmac using algorithm sha1 with key.
  102. @param key The hmac key.
  103. */
  104. - (NSData *)hmacSHA1DataWithKey:(NSData *)key;
  105. /**
  106. Returns a lowercase NSString for hmac using algorithm sha224 with key.
  107. @param key The hmac key.
  108. */
  109. - (NSString *)hmacSHA224StringWithKey:(NSString *)key;
  110. /**
  111. Returns an NSData for hmac using algorithm sha224 with key.
  112. @param key The hmac key.
  113. */
  114. - (NSData *)hmacSHA224DataWithKey:(NSData *)key;
  115. /**
  116. Returns a lowercase NSString for hmac using algorithm sha256 with key.
  117. @param key The hmac key.
  118. */
  119. - (NSString *)hmacSHA256StringWithKey:(NSString *)key;
  120. /**
  121. Returns an NSData for hmac using algorithm sha256 with key.
  122. @param key The hmac key.
  123. */
  124. - (NSData *)hmacSHA256DataWithKey:(NSData *)key;
  125. /**
  126. Returns a lowercase NSString for hmac using algorithm sha384 with key.
  127. @param key The hmac key.
  128. */
  129. - (NSString *)hmacSHA384StringWithKey:(NSString *)key;
  130. /**
  131. Returns an NSData for hmac using algorithm sha384 with key.
  132. @param key The hmac key.
  133. */
  134. - (NSData *)hmacSHA384DataWithKey:(NSData *)key;
  135. /**
  136. Returns a lowercase NSString for hmac using algorithm sha512 with key.
  137. @param key The hmac key.
  138. */
  139. - (NSString *)hmacSHA512StringWithKey:(NSString *)key;
  140. /**
  141. Returns an NSData for hmac using algorithm sha512 with key.
  142. @param key The hmac key.
  143. */
  144. - (NSData *)hmacSHA512DataWithKey:(NSData *)key;
  145. /**
  146. Returns a lowercase NSString for crc32 hash.
  147. */
  148. - (NSString *)crc32String;
  149. /**
  150. Returns crc32 hash.
  151. */
  152. - (uint32_t)crc32;
  153. #pragma mark - Encrypt and Decrypt
  154. ///=============================================================================
  155. /// @name Encrypt and Decrypt
  156. ///=============================================================================
  157. /**
  158. Returns an encrypted NSData using AES.
  159. @param key A key length of 16, 24 or 32 (128, 192 or 256bits).
  160. @param iv An initialization vector length of 16(128bits).
  161. Pass nil when you don't want to use iv.
  162. @return An NSData encrypted, or nil if an error occurs.
  163. */
  164. - (nullable NSData *)aes256EncryptWithKey:(NSData *)key iv:(nullable NSData *)iv;
  165. /**
  166. Returns an decrypted NSData using AES.
  167. @param key A key length of 16, 24 or 32 (128, 192 or 256bits).
  168. @param iv An initialization vector length of 16(128bits).
  169. Pass nil when you don't want to use iv.
  170. @return An NSData decrypted, or nil if an error occurs.
  171. */
  172. - (nullable NSData *)aes256DecryptWithkey:(NSData *)key iv:(nullable NSData *)iv;
  173. #pragma mark - Encode and decode
  174. ///=============================================================================
  175. /// @name Encode and decode
  176. ///=============================================================================
  177. /**
  178. Returns string decoded in UTF8.
  179. */
  180. - (nullable NSString *)utf8String;
  181. /**
  182. Returns a uppercase NSString in HEX.
  183. */
  184. - (nullable NSString *)hexString;
  185. /**
  186. Returns an NSData from hex string.
  187. @param hexString The hex string which is case insensitive.
  188. @return a new NSData, or nil if an error occurs.
  189. */
  190. + (nullable NSData *)dataWithHexString:(NSString *)hexString;
  191. /**
  192. Returns an NSString for base64 encoded.
  193. */
  194. - (nullable NSString *)base64EncodedString;
  195. /**
  196. Returns an NSData from base64 encoded string.
  197. @warning This method has been implemented in iOS7.
  198. @param base64EncodedString The encoded string.
  199. */
  200. + (nullable NSData *)dataWithBase64EncodedString:(NSString *)base64EncodedString;
  201. /**
  202. Returns an NSDictionary or NSArray for decoded self.
  203. Returns nil if an error occurs.
  204. */
  205. - (nullable id)jsonValueDecoded;
  206. #pragma mark - Inflate and deflate
  207. ///=============================================================================
  208. /// @name Inflate and deflate
  209. ///=============================================================================
  210. /**
  211. Decompress data from gzip data.
  212. @return Inflated data.
  213. */
  214. - (nullable NSData *)gzipInflate;
  215. /**
  216. Comperss data to gzip in default compresssion level.
  217. @return Deflated data.
  218. */
  219. - (nullable NSData *)gzipDeflate;
  220. /**
  221. Decompress data from zlib-compressed data.
  222. @return Inflated data.
  223. */
  224. - (nullable NSData *)zlibInflate;
  225. /**
  226. Comperss data to zlib-compressed in default compresssion level.
  227. @return Deflated data.
  228. */
  229. - (nullable NSData *)zlibDeflate;
  230. #pragma mark - Others
  231. ///=============================================================================
  232. /// @name Others
  233. ///=============================================================================
  234. /**
  235. Create data from the file in main bundle (similar to [UIImage imageNamed:]).
  236. @param name The file name (in main bundle).
  237. @return A new data create from the file.
  238. */
  239. + (nullable NSData *)dataNamed:(NSString *)name;
  240. @end
  241. NS_ASSUME_NONNULL_END