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