appjam 0.1.8.9 → 0.1.8.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/lib/appjam/generators/templates/blank/EiffelApplication.xcodeproj/project.pbxproj +114 -0
  2. data/lib/appjam/generators/templates/blank/EiffelApplication.xcodeproj/project.xcworkspace/xcuserdata/eiffel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  3. data/lib/appjam/generators/templates/blank/EiffelApplication/AppDelegate.m.tt +13 -1
  4. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/CustomBadge.h +52 -0
  5. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/CustomBadge.m +241 -0
  6. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherItem.h +58 -0
  7. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherItem.m +227 -0
  8. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherPageControl.h +37 -0
  9. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherPageControl.m +117 -0
  10. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherScrollView.h +23 -0
  11. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherScrollView.m +42 -0
  12. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherView.h +70 -0
  13. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherView.m +830 -0
  14. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherViewController.h +38 -0
  15. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/components/MyLauncher/MyLauncherViewController.m +302 -0
  16. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/AESCrypt.h +37 -0
  17. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/AESCrypt.m +50 -0
  18. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/NSData+Base64.h +17 -0
  19. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/NSData+Base64.m +110 -0
  20. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/NSData+CommonCrypto.h +112 -0
  21. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/NSData+CommonCrypto.m +546 -0
  22. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/NSString+Base64.h +15 -0
  23. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/AESCrypt-ObjC/NSString+Base64.m +82 -0
  24. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCCrossHairView.h +16 -0
  25. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCCrossHairView.m +43 -0
  26. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCFrameView.h +55 -0
  27. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCFrameView.m +234 -0
  28. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCIntrospect.h +155 -0
  29. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCIntrospect.m +1654 -0
  30. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCIntrospectSettings.h +56 -0
  31. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCStatusBarOverlay.h +34 -0
  32. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/DCIntrospect/DCStatusBarOverlay.m +107 -0
  33. data/lib/appjam/version.rb +1 -1
  34. metadata +32 -3
@@ -0,0 +1,112 @@
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
+
39
+ #import <Foundation/NSData.h>
40
+ #import <Foundation/NSError.h>
41
+ #import <CommonCrypto/CommonCryptor.h>
42
+ #import <CommonCrypto/CommonHMAC.h>
43
+
44
+ extern NSString * const kCommonCryptoErrorDomain;
45
+
46
+ @interface NSError (CommonCryptoErrorDomain)
47
+ + (NSError *) errorWithCCCryptorStatus: (CCCryptorStatus) status;
48
+ @end
49
+
50
+ @interface NSData (CommonDigest)
51
+
52
+ - (NSData *) MD2Sum;
53
+ - (NSData *) MD4Sum;
54
+ - (NSData *) MD5Sum;
55
+
56
+ - (NSData *) SHA1Hash;
57
+ - (NSData *) SHA224Hash;
58
+ - (NSData *) SHA256Hash;
59
+ - (NSData *) SHA384Hash;
60
+ - (NSData *) SHA512Hash;
61
+
62
+ @end
63
+
64
+ @interface NSData (CommonCryptor)
65
+
66
+ - (NSData *) AES256EncryptedDataUsingKey: (id) key error: (NSError **) error;
67
+ - (NSData *) decryptedAES256DataUsingKey: (id) key error: (NSError **) error;
68
+
69
+ - (NSData *) DESEncryptedDataUsingKey: (id) key error: (NSError **) error;
70
+ - (NSData *) decryptedDESDataUsingKey: (id) key error: (NSError **) error;
71
+
72
+ - (NSData *) CASTEncryptedDataUsingKey: (id) key error: (NSError **) error;
73
+ - (NSData *) decryptedCASTDataUsingKey: (id) key error: (NSError **) error;
74
+
75
+ @end
76
+
77
+ @interface NSData (LowLevelCommonCryptor)
78
+
79
+ - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
80
+ key: (id) key // data or string
81
+ error: (CCCryptorStatus *) error;
82
+ - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
83
+ key: (id) key // data or string
84
+ options: (CCOptions) options
85
+ error: (CCCryptorStatus *) error;
86
+ - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
87
+ key: (id) key // data or string
88
+ initializationVector: (id) iv // data or string
89
+ options: (CCOptions) options
90
+ error: (CCCryptorStatus *) error;
91
+
92
+ - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
93
+ key: (id) key // data or string
94
+ error: (CCCryptorStatus *) error;
95
+ - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
96
+ key: (id) key // data or string
97
+ options: (CCOptions) options
98
+ error: (CCCryptorStatus *) error;
99
+ - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
100
+ key: (id) key // data or string
101
+ initializationVector: (id) iv // data or string
102
+ options: (CCOptions) options
103
+ error: (CCCryptorStatus *) error;
104
+
105
+ @end
106
+
107
+ @interface NSData (CommonHMAC)
108
+
109
+ - (NSData *) HMACWithAlgorithm: (CCHmacAlgorithm) algorithm;
110
+ - (NSData *) HMACWithAlgorithm: (CCHmacAlgorithm) algorithm key: (id) key;
111
+
112
+ @end
@@ -0,0 +1,546 @@
1
+ /*
2
+ * NSData+CommonCrypto.m
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
+
39
+ #import <Foundation/Foundation.h>
40
+ #import "NSData+CommonCrypto.h"
41
+ #import <CommonCrypto/CommonDigest.h>
42
+ #import <CommonCrypto/CommonCryptor.h>
43
+ #import <CommonCrypto/CommonHMAC.h>
44
+
45
+ NSString * const kCommonCryptoErrorDomain = @"CommonCryptoErrorDomain";
46
+
47
+ @implementation NSError (CommonCryptoErrorDomain)
48
+
49
+ + (NSError *) errorWithCCCryptorStatus: (CCCryptorStatus) status
50
+ {
51
+ NSString * description = nil, * reason = nil;
52
+
53
+ switch ( status )
54
+ {
55
+ case kCCSuccess:
56
+ description = NSLocalizedString(@"Success", @"Error description");
57
+ break;
58
+
59
+ case kCCParamError:
60
+ description = NSLocalizedString(@"Parameter Error", @"Error description");
61
+ reason = NSLocalizedString(@"Illegal parameter supplied to encryption/decryption algorithm", @"Error reason");
62
+ break;
63
+
64
+ case kCCBufferTooSmall:
65
+ description = NSLocalizedString(@"Buffer Too Small", @"Error description");
66
+ reason = NSLocalizedString(@"Insufficient buffer provided for specified operation", @"Error reason");
67
+ break;
68
+
69
+ case kCCMemoryFailure:
70
+ description = NSLocalizedString(@"Memory Failure", @"Error description");
71
+ reason = NSLocalizedString(@"Failed to allocate memory", @"Error reason");
72
+ break;
73
+
74
+ case kCCAlignmentError:
75
+ description = NSLocalizedString(@"Alignment Error", @"Error description");
76
+ reason = NSLocalizedString(@"Input size to encryption algorithm was not aligned correctly", @"Error reason");
77
+ break;
78
+
79
+ case kCCDecodeError:
80
+ description = NSLocalizedString(@"Decode Error", @"Error description");
81
+ reason = NSLocalizedString(@"Input data did not decode or decrypt correctly", @"Error reason");
82
+ break;
83
+
84
+ case kCCUnimplemented:
85
+ description = NSLocalizedString(@"Unimplemented Function", @"Error description");
86
+ reason = NSLocalizedString(@"Function not implemented for the current algorithm", @"Error reason");
87
+ break;
88
+
89
+ default:
90
+ description = NSLocalizedString(@"Unknown Error", @"Error description");
91
+ break;
92
+ }
93
+
94
+ NSMutableDictionary * userInfo = [[NSMutableDictionary alloc] init];
95
+ [userInfo setObject: description forKey: NSLocalizedDescriptionKey];
96
+
97
+ if ( reason != nil )
98
+ [userInfo setObject: reason forKey: NSLocalizedFailureReasonErrorKey];
99
+
100
+ NSError * result = [NSError errorWithDomain: kCommonCryptoErrorDomain code: status userInfo: userInfo];
101
+ #if !__has_feature(objc_arc)
102
+ [userInfo release];
103
+ #endif
104
+
105
+ return ( result );
106
+ }
107
+
108
+ @end
109
+
110
+ #pragma mark -
111
+
112
+ @implementation NSData (CommonDigest)
113
+
114
+ - (NSData *) MD2Sum
115
+ {
116
+ unsigned char hash[CC_MD2_DIGEST_LENGTH];
117
+ (void) CC_MD2( [self bytes], (CC_LONG)[self length], hash );
118
+ return ( [NSData dataWithBytes: hash length: CC_MD2_DIGEST_LENGTH] );
119
+ }
120
+
121
+ - (NSData *) MD4Sum
122
+ {
123
+ unsigned char hash[CC_MD4_DIGEST_LENGTH];
124
+ (void) CC_MD4( [self bytes], (CC_LONG)[self length], hash );
125
+ return ( [NSData dataWithBytes: hash length: CC_MD4_DIGEST_LENGTH] );
126
+ }
127
+
128
+ - (NSData *) MD5Sum
129
+ {
130
+ unsigned char hash[CC_MD5_DIGEST_LENGTH];
131
+ (void) CC_MD5( [self bytes], (CC_LONG)[self length], hash );
132
+ return ( [NSData dataWithBytes: hash length: CC_MD5_DIGEST_LENGTH] );
133
+ }
134
+
135
+ - (NSData *) SHA1Hash
136
+ {
137
+ unsigned char hash[CC_SHA1_DIGEST_LENGTH];
138
+ (void) CC_SHA1( [self bytes], (CC_LONG)[self length], hash );
139
+ return ( [NSData dataWithBytes: hash length: CC_SHA1_DIGEST_LENGTH] );
140
+ }
141
+
142
+ - (NSData *) SHA224Hash
143
+ {
144
+ unsigned char hash[CC_SHA224_DIGEST_LENGTH];
145
+ (void) CC_SHA224( [self bytes], (CC_LONG)[self length], hash );
146
+ return ( [NSData dataWithBytes: hash length: CC_SHA224_DIGEST_LENGTH] );
147
+ }
148
+
149
+ - (NSData *) SHA256Hash
150
+ {
151
+ unsigned char hash[CC_SHA256_DIGEST_LENGTH];
152
+ (void) CC_SHA256( [self bytes], (CC_LONG)[self length], hash );
153
+ return ( [NSData dataWithBytes: hash length: CC_SHA256_DIGEST_LENGTH] );
154
+ }
155
+
156
+ - (NSData *) SHA384Hash
157
+ {
158
+ unsigned char hash[CC_SHA384_DIGEST_LENGTH];
159
+ (void) CC_SHA384( [self bytes], (CC_LONG)[self length], hash );
160
+ return ( [NSData dataWithBytes: hash length: CC_SHA384_DIGEST_LENGTH] );
161
+ }
162
+
163
+ - (NSData *) SHA512Hash
164
+ {
165
+ unsigned char hash[CC_SHA512_DIGEST_LENGTH];
166
+ (void) CC_SHA512( [self bytes], (CC_LONG)[self length], hash );
167
+ return ( [NSData dataWithBytes: hash length: CC_SHA512_DIGEST_LENGTH] );
168
+ }
169
+
170
+ @end
171
+
172
+ @implementation NSData (CommonCryptor)
173
+
174
+ - (NSData *) AES256EncryptedDataUsingKey: (id) key error: (NSError **) error
175
+ {
176
+ CCCryptorStatus status = kCCSuccess;
177
+ NSData * result = [self dataEncryptedUsingAlgorithm: kCCAlgorithmAES128
178
+ key: key
179
+ options: kCCOptionPKCS7Padding
180
+ error: &status];
181
+
182
+ if ( result != nil )
183
+ return ( result );
184
+
185
+ if ( error != NULL )
186
+ *error = [NSError errorWithCCCryptorStatus: status];
187
+
188
+ return ( nil );
189
+ }
190
+
191
+ - (NSData *) decryptedAES256DataUsingKey: (id) key error: (NSError **) error
192
+ {
193
+ CCCryptorStatus status = kCCSuccess;
194
+ NSData * result = [self decryptedDataUsingAlgorithm: kCCAlgorithmAES128
195
+ key: key
196
+ options: kCCOptionPKCS7Padding
197
+ error: &status];
198
+
199
+ if ( result != nil )
200
+ return ( result );
201
+
202
+ if ( error != NULL )
203
+ *error = [NSError errorWithCCCryptorStatus: status];
204
+
205
+ return ( nil );
206
+ }
207
+
208
+ - (NSData *) DESEncryptedDataUsingKey: (id) key error: (NSError **) error
209
+ {
210
+ CCCryptorStatus status = kCCSuccess;
211
+ NSData * result = [self dataEncryptedUsingAlgorithm: kCCAlgorithmDES
212
+ key: key
213
+ options: kCCOptionPKCS7Padding
214
+ error: &status];
215
+
216
+ if ( result != nil )
217
+ return ( result );
218
+
219
+ if ( error != NULL )
220
+ *error = [NSError errorWithCCCryptorStatus: status];
221
+
222
+ return ( nil );
223
+ }
224
+
225
+ - (NSData *) decryptedDESDataUsingKey: (id) key error: (NSError **) error
226
+ {
227
+ CCCryptorStatus status = kCCSuccess;
228
+ NSData * result = [self decryptedDataUsingAlgorithm: kCCAlgorithmDES
229
+ key: key
230
+ options: kCCOptionPKCS7Padding
231
+ error: &status];
232
+
233
+ if ( result != nil )
234
+ return ( result );
235
+
236
+ if ( error != NULL )
237
+ *error = [NSError errorWithCCCryptorStatus: status];
238
+
239
+ return ( nil );
240
+ }
241
+
242
+ - (NSData *) CASTEncryptedDataUsingKey: (id) key error: (NSError **) error
243
+ {
244
+ CCCryptorStatus status = kCCSuccess;
245
+ NSData * result = [self dataEncryptedUsingAlgorithm: kCCAlgorithmCAST
246
+ key: key
247
+ options: kCCOptionPKCS7Padding
248
+ error: &status];
249
+
250
+ if ( result != nil )
251
+ return ( result );
252
+
253
+ if ( error != NULL )
254
+ *error = [NSError errorWithCCCryptorStatus: status];
255
+
256
+ return ( nil );
257
+ }
258
+
259
+ - (NSData *) decryptedCASTDataUsingKey: (id) key error: (NSError **) error
260
+ {
261
+ CCCryptorStatus status = kCCSuccess;
262
+ NSData * result = [self decryptedDataUsingAlgorithm: kCCAlgorithmCAST
263
+ key: key
264
+ options: kCCOptionPKCS7Padding
265
+ error: &status];
266
+
267
+ if ( result != nil )
268
+ return ( result );
269
+
270
+ if ( error != NULL )
271
+ *error = [NSError errorWithCCCryptorStatus: status];
272
+
273
+ return ( nil );
274
+ }
275
+
276
+ @end
277
+
278
+ static void FixKeyLengths( CCAlgorithm algorithm, NSMutableData * keyData, NSMutableData * ivData )
279
+ {
280
+ NSUInteger keyLength = [keyData length];
281
+ switch ( algorithm )
282
+ {
283
+ case kCCAlgorithmAES128:
284
+ {
285
+ if ( keyLength < 16 )
286
+ {
287
+ [keyData setLength: 16];
288
+ }
289
+ else if ( keyLength < 24 )
290
+ {
291
+ [keyData setLength: 24];
292
+ }
293
+ else
294
+ {
295
+ [keyData setLength: 32];
296
+ }
297
+
298
+ break;
299
+ }
300
+
301
+ case kCCAlgorithmDES:
302
+ {
303
+ [keyData setLength: 8];
304
+ break;
305
+ }
306
+
307
+ case kCCAlgorithm3DES:
308
+ {
309
+ [keyData setLength: 24];
310
+ break;
311
+ }
312
+
313
+ case kCCAlgorithmCAST:
314
+ {
315
+ if ( keyLength < 5 )
316
+ {
317
+ [keyData setLength: 5];
318
+ }
319
+ else if ( keyLength > 16 )
320
+ {
321
+ [keyData setLength: 16];
322
+ }
323
+
324
+ break;
325
+ }
326
+
327
+ case kCCAlgorithmRC4:
328
+ {
329
+ if ( keyLength > 512 )
330
+ [keyData setLength: 512];
331
+ break;
332
+ }
333
+
334
+ default:
335
+ break;
336
+ }
337
+
338
+ [ivData setLength: [keyData length]];
339
+ }
340
+
341
+ @implementation NSData (LowLevelCommonCryptor)
342
+
343
+ - (NSData *) _runCryptor: (CCCryptorRef) cryptor result: (CCCryptorStatus *) status
344
+ {
345
+ size_t bufsize = CCCryptorGetOutputLength( cryptor, (size_t)[self length], true );
346
+ void * buf = malloc( bufsize );
347
+ size_t bufused = 0;
348
+ size_t bytesTotal = 0;
349
+ *status = CCCryptorUpdate( cryptor, [self bytes], (size_t)[self length],
350
+ buf, bufsize, &bufused );
351
+ if ( *status != kCCSuccess )
352
+ {
353
+ free( buf );
354
+ return ( nil );
355
+ }
356
+
357
+ bytesTotal += bufused;
358
+
359
+ // From Brent Royal-Gordon (Twitter: architechies):
360
+ // Need to update buf ptr past used bytes when calling CCCryptorFinal()
361
+ *status = CCCryptorFinal( cryptor, buf + bufused, bufsize - bufused, &bufused );
362
+ if ( *status != kCCSuccess )
363
+ {
364
+ free( buf );
365
+ return ( nil );
366
+ }
367
+
368
+ bytesTotal += bufused;
369
+
370
+ return ( [NSData dataWithBytesNoCopy: buf length: bytesTotal] );
371
+ }
372
+
373
+ - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
374
+ key: (id) key
375
+ error: (CCCryptorStatus *) error
376
+ {
377
+ return ( [self dataEncryptedUsingAlgorithm: algorithm
378
+ key: key
379
+ initializationVector: nil
380
+ options: 0
381
+ error: error] );
382
+ }
383
+
384
+ - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
385
+ key: (id) key
386
+ options: (CCOptions) options
387
+ error: (CCCryptorStatus *) error
388
+ {
389
+ return ( [self dataEncryptedUsingAlgorithm: algorithm
390
+ key: key
391
+ initializationVector: nil
392
+ options: options
393
+ error: error] );
394
+ }
395
+
396
+ - (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm
397
+ key: (id) key
398
+ initializationVector: (id) iv
399
+ options: (CCOptions) options
400
+ error: (CCCryptorStatus *) error
401
+ {
402
+ CCCryptorRef cryptor = NULL;
403
+ CCCryptorStatus status = kCCSuccess;
404
+
405
+ NSParameterAssert([key isKindOfClass: [NSData class]] || [key isKindOfClass: [NSString class]]);
406
+ NSParameterAssert(iv == nil || [iv isKindOfClass: [NSData class]] || [iv isKindOfClass: [NSString class]]);
407
+
408
+ NSMutableData * keyData, * ivData;
409
+ if ( [key isKindOfClass: [NSData class]] )
410
+ keyData = (NSMutableData *) [key mutableCopy];
411
+ else
412
+ keyData = [[key dataUsingEncoding: NSUTF8StringEncoding] mutableCopy];
413
+
414
+ if ( [iv isKindOfClass: [NSString class]] )
415
+ ivData = [[iv dataUsingEncoding: NSUTF8StringEncoding] mutableCopy];
416
+ else
417
+ ivData = (NSMutableData *) [iv mutableCopy]; // data or nil
418
+
419
+ #if !__has_feature(objc_arc)
420
+ [keyData autorelease];
421
+ [ivData autorelease];
422
+ #endif
423
+ // ensure correct lengths for key and iv data, based on algorithms
424
+ FixKeyLengths( algorithm, keyData, ivData );
425
+
426
+ status = CCCryptorCreate( kCCEncrypt, algorithm, options,
427
+ [keyData bytes], [keyData length], [ivData bytes],
428
+ &cryptor );
429
+
430
+ if ( status != kCCSuccess )
431
+ {
432
+ if ( error != NULL )
433
+ *error = status;
434
+ return ( nil );
435
+ }
436
+
437
+ NSData * result = [self _runCryptor: cryptor result: &status];
438
+ if ( (result == nil) && (error != NULL) )
439
+ *error = status;
440
+
441
+ CCCryptorRelease( cryptor );
442
+
443
+ return ( result );
444
+ }
445
+
446
+ - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
447
+ key: (id) key // data or string
448
+ error: (CCCryptorStatus *) error
449
+ {
450
+ return ( [self decryptedDataUsingAlgorithm: algorithm
451
+ key: key
452
+ initializationVector: nil
453
+ options: 0
454
+ error: error] );
455
+ }
456
+
457
+ - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
458
+ key: (id) key // data or string
459
+ options: (CCOptions) options
460
+ error: (CCCryptorStatus *) error
461
+ {
462
+ return ( [self decryptedDataUsingAlgorithm: algorithm
463
+ key: key
464
+ initializationVector: nil
465
+ options: options
466
+ error: error] );
467
+ }
468
+
469
+ - (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
470
+ key: (id) key // data or string
471
+ initializationVector: (id) iv // data or string
472
+ options: (CCOptions) options
473
+ error: (CCCryptorStatus *) error
474
+ {
475
+ CCCryptorRef cryptor = NULL;
476
+ CCCryptorStatus status = kCCSuccess;
477
+
478
+ NSParameterAssert([key isKindOfClass: [NSData class]] || [key isKindOfClass: [NSString class]]);
479
+ NSParameterAssert(iv == nil || [iv isKindOfClass: [NSData class]] || [iv isKindOfClass: [NSString class]]);
480
+
481
+ NSMutableData * keyData, * ivData;
482
+ if ( [key isKindOfClass: [NSData class]] )
483
+ keyData = (NSMutableData *) [key mutableCopy];
484
+ else
485
+ keyData = [[key dataUsingEncoding: NSUTF8StringEncoding] mutableCopy];
486
+
487
+ if ( [iv isKindOfClass: [NSString class]] )
488
+ ivData = [[iv dataUsingEncoding: NSUTF8StringEncoding] mutableCopy];
489
+ else
490
+ ivData = (NSMutableData *) [iv mutableCopy]; // data or nil
491
+
492
+ #if !__has_feature(objc_arc)
493
+ [keyData autorelease];
494
+ [ivData autorelease];
495
+ #endif
496
+
497
+ // ensure correct lengths for key and iv data, based on algorithms
498
+ FixKeyLengths( algorithm, keyData, ivData );
499
+
500
+ status = CCCryptorCreate( kCCDecrypt, algorithm, options,
501
+ [keyData bytes], [keyData length], [ivData bytes],
502
+ &cryptor );
503
+
504
+ if ( status != kCCSuccess )
505
+ {
506
+ if ( error != NULL )
507
+ *error = status;
508
+ return ( nil );
509
+ }
510
+
511
+ NSData * result = [self _runCryptor: cryptor result: &status];
512
+ if ( (result == nil) && (error != NULL) )
513
+ *error = status;
514
+
515
+ CCCryptorRelease( cryptor );
516
+
517
+ return ( result );
518
+ }
519
+
520
+ @end
521
+
522
+ @implementation NSData (CommonHMAC)
523
+
524
+ - (NSData *) HMACWithAlgorithm: (CCHmacAlgorithm) algorithm
525
+ {
526
+ return ( [self HMACWithAlgorithm: algorithm key: nil] );
527
+ }
528
+
529
+ - (NSData *) HMACWithAlgorithm: (CCHmacAlgorithm) algorithm key: (id) key
530
+ {
531
+ NSParameterAssert(key == nil || [key isKindOfClass: [NSData class]] || [key isKindOfClass: [NSString class]]);
532
+
533
+ NSData * keyData = nil;
534
+ if ( [key isKindOfClass: [NSString class]] )
535
+ keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
536
+ else
537
+ keyData = (NSData *) key;
538
+
539
+ // this could be either CC_SHA1_DIGEST_LENGTH or CC_MD5_DIGEST_LENGTH. SHA1 is larger.
540
+ unsigned char buf[CC_SHA1_DIGEST_LENGTH];
541
+ CCHmac( algorithm, [keyData bytes], [keyData length], [self bytes], [self length], buf );
542
+
543
+ return ( [NSData dataWithBytes: buf length: (algorithm == kCCHmacAlgMD5 ? CC_MD5_DIGEST_LENGTH : CC_SHA1_DIGEST_LENGTH)] );
544
+ }
545
+
546
+ @end