-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNSUserDefaults+EncryptionCipher.m
More file actions
951 lines (793 loc) · 31.8 KB
/
Copy pathNSUserDefaults+EncryptionCipher.m
File metadata and controls
951 lines (793 loc) · 31.8 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
//
// NSUserDefaults+EncryptionCipher.m
//
//
// Created by iskf on 2017/3/3.
// https://github.com/iskf
//
#import "NSUserDefaults+EncryptionCipher.h"
#import <objc/runtime.h>
#define kStoredObjectKey @"kf_Encryption"
static NSString *_secret = @"kf123456789";
@implementation NSUserDefaults (EncryptionCipher)
+(void)load
{
/** setValue:forKey: */
Method sys_setValueForKeyM = class_getInstanceMethod([self class], @selector(setValue:forKey:));
Method kf_setValueForKeyM = class_getInstanceMethod([self class], @selector(kf_setValue:forKey:));
method_exchangeImplementations(sys_setValueForKeyM, kf_setValueForKeyM);
/** valueForKey: */
Method sys_valueForKeyM = class_getInstanceMethod([self class], @selector(valueForKey:));
Method kf_valueForKeyM = class_getInstanceMethod([self class], @selector(kf_valueForKey:));
method_exchangeImplementations(sys_valueForKeyM, kf_valueForKeyM);
/** setObject:forKey: */
Method sys_setObjectForKeyM = class_getInstanceMethod([self class], @selector(setObject:forKey:));
Method kf_setObjectForKeyM = class_getInstanceMethod([self class], @selector(kf_setObject:forKey:));
method_exchangeImplementations(sys_setObjectForKeyM, kf_setObjectForKeyM);
/** objectForKey: */
Method sys_objectForKeyM = class_getInstanceMethod([self class], @selector(objectForKey:));
Method kf_objectForKeyM = class_getInstanceMethod([self class], @selector(kf_objectForKey:));
method_exchangeImplementations(sys_objectForKeyM, kf_objectForKeyM);
/** setBool:forKey: */
Method sys_setBoolForKeyM = class_getInstanceMethod([self class], @selector(setBool:forKey:));
Method kf_setBoolForKeyM = class_getInstanceMethod([self class], @selector(kf_setBool:forKey:));
method_exchangeImplementations(sys_setBoolForKeyM, kf_setBoolForKeyM);
/** boolForKey: */
Method sys_boolForKeyM = class_getInstanceMethod([self class], @selector(boolForKey:));
Method kf_boolForKeyM = class_getInstanceMethod([self class], @selector(kf_boolForKey:));
method_exchangeImplementations(sys_boolForKeyM, kf_boolForKeyM);
/** setBool:forKey: */
Method sys_setIntegerForKeyM = class_getInstanceMethod([self class], @selector(setInteger:forKey:));
Method kf_setIntegerForKeyM = class_getInstanceMethod([self class], @selector(kf_setInteger:forKey:));
method_exchangeImplementations(sys_setIntegerForKeyM, kf_setIntegerForKeyM);
/** boolForKey: */
Method sys_integerForKeyM = class_getInstanceMethod([self class], @selector(integerForKey:));
Method kf_integerForKeyM = class_getInstanceMethod([self class], @selector(kf_integerForKey:));
method_exchangeImplementations(sys_integerForKeyM, kf_integerForKeyM);
/** setFloat:forKey: */
Method sys_setFloatForKeyM = class_getInstanceMethod([self class], @selector(setFloat:forKey:));
Method kf_setFloatForKeyM = class_getInstanceMethod([self class], @selector(kf_setFloat:forKey:));
method_exchangeImplementations(sys_setFloatForKeyM, kf_setFloatForKeyM);
/** floatForKey: */
Method sys_floatForKeyM = class_getInstanceMethod([self class], @selector(floatForKey:));
Method kf_floatForKeyM = class_getInstanceMethod([self class], @selector(kf_floatForKey:));
method_exchangeImplementations(sys_floatForKeyM, kf_floatForKeyM);
/** setDouble:forKey: */
Method sys_setDoubleForKeyM = class_getInstanceMethod([self class], @selector(setDouble:forKey:));
Method kf_setDoubleForKeyM = class_getInstanceMethod([self class], @selector(kf_setDouble:forKey:));
method_exchangeImplementations(sys_setDoubleForKeyM, kf_setDoubleForKeyM);
/** doubleForKey: */
Method sys_doubleForKeyM = class_getInstanceMethod([self class], @selector(doubleForKey:));
Method kf_doubleForKeyM = class_getInstanceMethod([self class], @selector(kf_doubleForKey:));
method_exchangeImplementations(sys_doubleForKeyM, kf_doubleForKeyM);
/** setURL:forKey: */
Method sys_setURLForKeyM = class_getInstanceMethod([self class], @selector(setURL:forKey:));
Method kf_setURLForKeyM = class_getInstanceMethod([self class], @selector(kf_setURL:forKey:));
method_exchangeImplementations(sys_setURLForKeyM, kf_setURLForKeyM);
/** URLForKey: */
Method sys_URLForKeyM = class_getInstanceMethod([self class], @selector(URLForKey:));
Method kf_URLForKeyM = class_getInstanceMethod([self class], @selector(kf_URLForKey:));
method_exchangeImplementations(sys_URLForKeyM, kf_URLForKeyM);
/** dataForKey: */
Method sys_dataForKeyM = class_getInstanceMethod([self class], @selector(dataForKey:));
Method kf_dataForKeyM = class_getInstanceMethod([self class], @selector(kf_dataForKey:));
method_exchangeImplementations(sys_dataForKeyM, kf_dataForKeyM);
/** arrayForKey: */
Method sys_arrayForKeyM = class_getInstanceMethod([self class], @selector(arrayForKey:));
Method kf_arrayForKeyM = class_getInstanceMethod([self class], @selector(kf_arrayForKey:));
method_exchangeImplementations(sys_arrayForKeyM, kf_arrayForKeyM);
/** dictionaryForKey: */
Method sys_dictionaryForKeyM = class_getInstanceMethod([self class], @selector(dictionaryForKey:));
Method kf_dictionaryForKeyM = class_getInstanceMethod([self class], @selector(kf_dictionaryForKey:));
method_exchangeImplementations(sys_dictionaryForKeyM, kf_dictionaryForKeyM);
}
-(id)kf_valueForKey:(NSString *)key
{
id obj = [self kf_secretObjectForKey:key];
[self kf_objectForKey:key];
return obj;
}
-(id)kf_objectForKey:(NSString *)defaultName
{
[self kf_objectForKey:defaultName];
id obj = [self kf_secretObjectForKey:defaultName];
return obj;
}
-(BOOL)kf_boolForKey:(NSString *)defaultName
{
id object = [self kf_secretObjectForKey:defaultName];
[self kf_boolForKey:defaultName];
if([object isKindOfClass:[NSNumber class]]) {
return [object boolValue];
} else {
return NO;
}
}
-(NSInteger)kf_integerForKey:(NSString *)defaultName
{
id object = [self kf_secretObjectForKey:defaultName];
[self kf_integerForKey:defaultName];
if([object isKindOfClass:[NSNumber class]]) {
return [object integerValue];
} else {
return 0;
}
}
-(float)kf_floatForKey:(NSString *)defaultName
{
id object = [self kf_secretObjectForKey:defaultName];
[self kf_floatForKey:defaultName];
if([object isKindOfClass:[NSNumber class]]) {
return [object floatValue];
} else {
return 0.f;
}
}
-(double)kf_doubleForKey:(NSString *)defaultName
{
id object = [self kf_secretObjectForKey:defaultName];
[self kf_doubleForKey:defaultName];
if([object isKindOfClass:[NSNumber class]]) {
return [object doubleValue];
} else {
return 0;
}
}
-(NSURL *)kf_URLForKey:(NSString *)defaultName
{
id object = [self kf_secretObjectForKey:defaultName];
[self kf_URLForKey:defaultName];
if([object isKindOfClass:[NSURL class]]) {
return object;
} else {
return nil;
}
}
-(NSData *)kf_dataForKey:(NSString *)defaultName
{
id object = [self kf_secretObjectForKey:defaultName];
[self kf_dataForKey:defaultName];
if([object isKindOfClass:[NSData class]]) {
return object;
} else {
return nil;
}
}
-(NSArray *)kf_arrayForKey:(NSString *)defaultName
{
id objects = [self kf_secretObjectForKey:defaultName];
[self kf_arrayForKey:defaultName];
if([objects isKindOfClass:[NSArray class]]) {
for(id object in objects) {
if(![object isKindOfClass:[NSString class]]) {
return nil;
}
}
return objects;
} else {
return nil;
}
}
-(NSDictionary<NSString *,id> *)kf_dictionaryForKey:(NSString *)defaultName
{
id object = [self kf_secretObjectForKey:defaultName];
[self kf_dictionaryForKey:defaultName];
if([object isKindOfClass:[NSDictionary class]]) {
return object;
} else {
return nil;
}
}
- (id)kf_secretObjectForKey:(NSString *)defaultName
{
NSAssert(_secret, @"Secret may not be nil when storing an object securely");
NSData *data = [self kf_objectForKey:defaultName];
if(data == nil) {
return nil;
}
@try {
CocoaSecurityResult *keyData = [CocoaSecurity sha384:_secret];
NSData *aesKey = [keyData.data subdataWithRange:NSMakeRange(0, 32)];
NSData *aesIv = [keyData.data subdataWithRange:NSMakeRange(32, 16)];
CocoaSecurityResult *result = [CocoaSecurity aesDecryptWithData:data key:aesKey iv:aesIv];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:result.data];
id object = [unarchiver decodeObjectForKey:kStoredObjectKey];
[unarchiver finishDecoding];
return object;
}
@catch (NSException *exception) {
NSLog(@"Cannot receive object from encrypted data storage: %@", exception.reason);
return nil;
}
@finally {}
}
-(void)kf_setValue:(id)value forKey:(NSString *)key
{
//TO DO encryption
[self kf_setSecretObject:value forKey:key];
// [self kf_setValue:value forKey:key];
}
-(void)kf_setObject:(id)value forKey:(NSString *)defaultName
{
[self kf_setSecretObject:value forKey:defaultName];
//[self kf_setObject:value forKey:defaultName];
}
-(void)kf_setBool:(BOOL)value forKey:(NSString *)defaultName
{
[self kf_setSecretObject:[NSNumber numberWithBool:value] forKey:defaultName];
// [self kf_setBool:value forKey:defaultName];
}
-(void)kf_setInteger:(NSInteger)value forKey:(NSString *)defaultName
{
[self kf_setSecretObject:[NSNumber numberWithInteger:value] forKey:defaultName];
// [self kf_setInteger:value forKey:defaultName];
}
-(void)kf_setFloat:(float)value forKey:(NSString *)defaultName
{
[self kf_setSecretObject:[NSNumber numberWithFloat:value] forKey:defaultName];
// [self kf_setFloat:value forKey:defaultName];
}
-(void)kf_setDouble:(double)value forKey:(NSString *)defaultName
{
[self kf_setSecretObject:[NSNumber numberWithDouble:value] forKey:defaultName];
// [self kf_setDouble:value forKey:defaultName];
}
-(void)kf_setURL:(NSURL *)url forKey:(NSString *)defaultName
{
[self kf_setSecretObject:url forKey:defaultName];
// [self kf_setURL:url forKey:defaultName];
}
- (void)kf_setSecretObject:(id)value forKey:(NSString *)defaultName
{
NSAssert(_secret, @"Secret may not be nil when storing an object securely");
@try {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:value forKey:kStoredObjectKey];
[archiver finishEncoding];
CocoaSecurityResult *keyData = [CocoaSecurity sha384:_secret];
NSData *aesKey = [keyData.data subdataWithRange:NSMakeRange(0, 32)];
NSData *aesIv = [keyData.data subdataWithRange:NSMakeRange(32, 16)];
CocoaSecurityResult *result = [CocoaSecurity aesEncryptWithData:data key:aesKey iv:aesIv];
[self kf_setObject:result.data forKey:defaultName];
}
@catch (NSException *exception) {
NSLog(@"Cannot store object securely: %@", exception.reason);
}
@finally {}
}
// https://github.com/iskf
@end
#pragma GCC diagnostic ignored "-Wselector"
#import <Availability.h>
#if !__has_feature(objc_arc)
#error This library requires automatic reference counting
#endif
@implementation NSData (Base64)
+ (NSData *)dataWithBase64EncodedString:(NSString *)string
{
if (![string length]) return nil;
NSData *decoded = nil;
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9 || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
if (![NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)])
{
decoded = [[self alloc] initWithBase64Encoding:[string stringByReplacingOccurrencesOfString:@"[^A-Za-z0-9+/=]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])]];
}
else
#endif
{
decoded = [[self alloc] initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];
}
return [decoded length]? decoded: nil;
}
- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth
{
if (![self length]) return nil;
NSString *encoded = nil;
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9 || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
if (![NSData instancesRespondToSelector:@selector(base64EncodedStringWithOptions:)])
{
encoded = [self base64Encoding];
}
else
#endif
{
switch (wrapWidth)
{
case 64:
{
return [self base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
case 76:
{
return [self base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
}
default:
{
encoded = [self base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
}
}
}
if (!wrapWidth || wrapWidth >= [encoded length])
{
return encoded;
}
wrapWidth = (wrapWidth / 4) * 4;
NSMutableString *result = [NSMutableString string];
for (NSUInteger i = 0; i < [encoded length]; i+= wrapWidth)
{
if (i + wrapWidth >= [encoded length])
{
[result appendString:[encoded substringFromIndex:i]];
break;
}
[result appendString:[encoded substringWithRange:NSMakeRange(i, wrapWidth)]];
[result appendString:@"\r\n"];
}
return result;
}
- (NSString *)base64EncodedString
{
return [self base64EncodedStringWithWrapWidth:0];
}
@end
@implementation NSString (Base64)
+ (NSString *)stringWithBase64EncodedString:(NSString *)string
{
NSData *data = [NSData dataWithBase64EncodedString:string];
if (data)
{
return [[self alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return nil;
}
- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
return [data base64EncodedStringWithWrapWidth:wrapWidth];
}
- (NSString *)base64EncodedString
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
return [data base64EncodedString];
}
- (NSString *)base64DecodedString
{
return [NSString stringWithBase64EncodedString:self];
}
- (NSData *)base64DecodedData
{
return [NSData dataWithBase64EncodedString:self];
}
@end
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>
#pragma mark - CocoaSecurity
@implementation CocoaSecurity
#pragma mark - AES Encrypt
// default AES Encrypt, key -> SHA384(key).sub(0, 32), iv -> SHA384(key).sub(32, 16)
+ (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSString *)key
{
CocoaSecurityResult * sha = [self sha384:key];
NSData *aesKey = [sha.data subdataWithRange:NSMakeRange(0, 32)];
NSData *aesIv = [sha.data subdataWithRange:NSMakeRange(32, 16)];
return [self aesEncrypt:data key:aesKey iv:aesIv];
}
#pragma mark AES Encrypt 128, 192, 256
+ (CocoaSecurityResult *)aesEncrypt:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv
{
CocoaSecurityDecoder *decoder = [CocoaSecurityDecoder new];
NSData *aesKey = [decoder hex:key];
NSData *aesIv = [decoder hex:iv];
return [self aesEncrypt:data key:aesKey iv:aesIv];
}
+ (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSData *)key iv:(NSData *)iv
{
return [self aesEncryptWithData:[data dataUsingEncoding:NSUTF8StringEncoding] key:key iv:iv];
}
+ (CocoaSecurityResult *)aesEncryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv
{
// check length of key and iv
if ([iv length] != 16) {
@throw [NSException exceptionWithName:@"Cocoa Security"
reason:@"Length of iv is wrong. Length of iv should be 16(128bits)"
userInfo:nil];
}
if ([key length] != 16 && [key length] != 24 && [key length] != 32 ) {
@throw [NSException exceptionWithName:@"Cocoa Security"
reason:@"Length of key is wrong. Length of iv should be 16, 24 or 32(128, 192 or 256bits)"
userInfo:nil];
}
// setup output buffer
size_t bufferSize = [data length] + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
// do encrypt
size_t encryptedSize = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[key bytes], // Key
[key length], // kCCKeySizeAES
[iv bytes], // IV
[data bytes],
[data length],
buffer,
bufferSize,
&encryptedSize);
if (cryptStatus == kCCSuccess) {
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:buffer length:encryptedSize];
free(buffer);
return result;
}
else {
free(buffer);
@throw [NSException exceptionWithName:@"Cocoa Security"
reason:@"Encrypt Error!"
userInfo:nil];
return nil;
}
}
#pragma mark - AES Decrypt
// default AES Decrypt, key -> SHA384(key).sub(0, 32), iv -> SHA384(key).sub(32, 16)
+ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSString *)key
{
CocoaSecurityResult * sha = [self sha384:key];
NSData *aesKey = [sha.data subdataWithRange:NSMakeRange(0, 32)];
NSData *aesIv = [sha.data subdataWithRange:NSMakeRange(32, 16)];
return [self aesDecryptWithBase64:data key:aesKey iv:aesIv];
}
#pragma mark AES Decrypt 128, 192, 256
+ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv
{
CocoaSecurityDecoder *decoder = [CocoaSecurityDecoder new];
NSData *aesKey = [decoder hex:key];
NSData *aesIv = [decoder hex:iv];
return [self aesDecryptWithBase64:data key:aesKey iv:aesIv];
}
+ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSData *)key iv:(NSData *)iv
{
CocoaSecurityDecoder *decoder = [CocoaSecurityDecoder new];
return [self aesDecryptWithData:[decoder base64:data] key:key iv:iv];
}
+ (CocoaSecurityResult *)aesDecryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv
{
// check length of key and iv
if ([iv length] != 16) {
@throw [NSException exceptionWithName:@"Cocoa Security"
reason:@"Length of iv is wrong. Length of iv should be 16(128bits)"
userInfo:nil];
}
if ([key length] != 16 && [key length] != 24 && [key length] != 32 ) {
@throw [NSException exceptionWithName:@"Cocoa Security"
reason:@"Length of key is wrong. Length of iv should be 16, 24 or 32(128, 192 or 256bits)"
userInfo:nil];
}
// setup output buffer
size_t bufferSize = [data length] + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
// do encrypt
size_t encryptedSize = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[key bytes], // Key
[key length], // kCCKeySizeAES
[iv bytes], // IV
[data bytes],
[data length],
buffer,
bufferSize,
&encryptedSize);
if (cryptStatus == kCCSuccess) {
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:buffer length:encryptedSize];
free(buffer);
return result;
}
else {
free(buffer);
@throw [NSException exceptionWithName:@"Cocoa Security"
reason:@"Decrypt Error!"
userInfo:nil];
return nil;
}
}
#pragma mark - MD5
+ (CocoaSecurityResult *)md5:(NSString *)hashString
{
return [self md5WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]];
}
+ (CocoaSecurityResult *)md5WithData:(NSData *)hashData
{
unsigned char *digest;
digest = malloc(CC_MD5_DIGEST_LENGTH);
CC_MD5([hashData bytes], (CC_LONG)[hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];
free(digest);
return result;
}
#pragma mark - HMAC-MD5
+ (CocoaSecurityResult *)hmacMd5:(NSString *)hashString hmacKey:(NSString *)key
{
return [self hmacMd5WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key];
}
+ (CocoaSecurityResult *)hmacMd5WithData:(NSData *)hashData hmacKey:(NSString *)key
{
unsigned char *digest;
digest = malloc(CC_MD5_DIGEST_LENGTH);
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
CCHmac(kCCHmacAlgMD5, cKey, strlen(cKey), [hashData bytes], [hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];
free(digest);
cKey = nil;
return result;
}
#pragma mark - SHA1
+ (CocoaSecurityResult *)sha1:(NSString *)hashString
{
return [self sha1WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]];
}
+ (CocoaSecurityResult *)sha1WithData:(NSData *)hashData
{
unsigned char *digest;
digest = malloc(CC_SHA1_DIGEST_LENGTH);
CC_SHA1([hashData bytes], (CC_LONG)[hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
free(digest);
return result;
}
#pragma mark SHA224
+ (CocoaSecurityResult *)sha224:(NSString *)hashString
{
return [self sha224WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]];
}
+ (CocoaSecurityResult *)sha224WithData:(NSData *)hashData
{
unsigned char *digest;
digest = malloc(CC_SHA224_DIGEST_LENGTH);
CC_SHA224([hashData bytes], (CC_LONG)[hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA224_DIGEST_LENGTH];
free(digest);
return result;
}
#pragma mark SHA256
+ (CocoaSecurityResult *)sha256:(NSString *)hashString
{
return [self sha256WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]];
}
+ (CocoaSecurityResult *)sha256WithData:(NSData *)hashData
{
unsigned char *digest;
digest = malloc(CC_SHA256_DIGEST_LENGTH);
CC_SHA256([hashData bytes], (CC_LONG)[hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
free(digest);
return result;
}
#pragma mark SHA384
+ (CocoaSecurityResult *)sha384:(NSString *)hashString
{
return [self sha384WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]];
}
+ (CocoaSecurityResult *)sha384WithData:(NSData *)hashData
{
unsigned char *digest;
digest = malloc(CC_SHA384_DIGEST_LENGTH);
CC_SHA384([hashData bytes], (CC_LONG)[hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA384_DIGEST_LENGTH];
free(digest);
return result;
}
#pragma mark SHA512
+ (CocoaSecurityResult *)sha512:(NSString *)hashString
{
return [self sha512WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]];
}
+ (CocoaSecurityResult *)sha512WithData:(NSData *)hashData
{
unsigned char *digest;
digest = malloc(CC_SHA512_DIGEST_LENGTH);
CC_SHA512([hashData bytes], (CC_LONG)[hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
free(digest);
return result;
}
#pragma mark - HMAC-SHA1
+ (CocoaSecurityResult *)hmacSha1:(NSString *)hashString hmacKey:(NSString *)key
{
return [self hmacSha1WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key];
}
+ (CocoaSecurityResult *)hmacSha1WithData:(NSData *)hashData hmacKey:(NSString *)key
{
unsigned char *digest;
digest = malloc(CC_SHA1_DIGEST_LENGTH);
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), [hashData bytes], [hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
free(digest);
cKey = nil;
return result;
}
#pragma mark HMAC-SHA224
+ (CocoaSecurityResult *)hmacSha224:(NSString *)hashString hmacKey:(NSString *)key
{
return [self hmacSha224WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key];
}
+ (CocoaSecurityResult *)hmacSha224WithData:(NSData *)hashData hmacKey:(NSString *)key
{
unsigned char *digest;
digest = malloc(CC_SHA224_DIGEST_LENGTH);
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
CCHmac(kCCHmacAlgSHA224, cKey, strlen(cKey), [hashData bytes], [hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA224_DIGEST_LENGTH];
free(digest);
cKey = nil;
return result;
}
#pragma mark HMAC-SHA256
+ (CocoaSecurityResult *)hmacSha256:(NSString *)hashString hmacKey:(NSString *)key
{
return [self hmacSha256WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key];
}
+ (CocoaSecurityResult *)hmacSha256WithData:(NSData *)hashData hmacKey:(NSString *)key
{
unsigned char *digest;
digest = malloc(CC_SHA256_DIGEST_LENGTH);
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), [hashData bytes], [hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
free(digest);
cKey = nil;
return result;
}
#pragma mark HMAC-SHA384
+ (CocoaSecurityResult *)hmacSha384:(NSString *)hashString hmacKey:(NSString *)key
{
return [self hmacSha384WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key];
}
+ (CocoaSecurityResult *)hmacSha384WithData:(NSData *)hashData hmacKey:(NSString *)key
{
unsigned char *digest;
digest = malloc(CC_SHA384_DIGEST_LENGTH);
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
CCHmac(kCCHmacAlgSHA384, cKey, strlen(cKey), [hashData bytes], [hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA384_DIGEST_LENGTH];
free(digest);
cKey = nil;
return result;
}
#pragma mark HMAC-SHA512
+ (CocoaSecurityResult *)hmacSha512:(NSString *)hashString hmacKey:(NSString *)key
{
return [self hmacSha512WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key];
}
+ (CocoaSecurityResult *)hmacSha512WithData:(NSData *)hashData hmacKey:(NSString *)key
{
unsigned char *digest;
digest = malloc(CC_SHA512_DIGEST_LENGTH);
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
CCHmac(kCCHmacAlgSHA512, cKey, strlen(cKey), [hashData bytes], [hashData length], digest);
CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
free(digest);
cKey = nil;
return result;
}
@end
#pragma mark - CocoaSecurityResult
@implementation CocoaSecurityResult
@synthesize data = _data;
#pragma mark - Init
- (id)initWithBytes:(unsigned char[])initData length:(NSUInteger)length
{
self = [super init];
if (self) {
_data = [NSData dataWithBytes:initData length:length];
}
return self;
}
#pragma mark UTF8 String
// convert CocoaSecurityResult to UTF8 string
- (NSString *)utf8String
{
NSString *result = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
return result;
}
#pragma mark HEX
// convert CocoaSecurityResult to HEX string
- (NSString *)hex
{
CocoaSecurityEncoder *encoder = [CocoaSecurityEncoder new];
return [encoder hex:_data useLower:false];
}
- (NSString *)hexLower
{
CocoaSecurityEncoder *encoder = [CocoaSecurityEncoder new];
return [encoder hex:_data useLower:true];
}
#pragma mark Base64
// convert CocoaSecurityResult to Base64 string
- (NSString *)base64
{
CocoaSecurityEncoder *encoder = [CocoaSecurityEncoder new];
return [encoder base64:_data];
}
@end
#pragma mark - CocoaSecurityEncoder
@implementation CocoaSecurityEncoder
// convert NSData to Base64
- (NSString *)base64:(NSData *)data
{
return [data base64EncodedString];
}
// convert NSData to hex string
- (NSString *)hex:(NSData *)data useLower:(BOOL)isOutputLower
{
if (data.length == 0) { return nil; }
static const char HexEncodeCharsLower[] = "0123456789abcdef";
static const char HexEncodeChars[] = "0123456789ABCDEF";
char *resultData;
// malloc result data
resultData = malloc([data length] * 2 +1);
// convert imgData(NSData) to char[]
unsigned char *sourceData = ((unsigned char *)[data bytes]);
NSUInteger length = [data length];
if (isOutputLower) {
for (NSUInteger index = 0; index < length; index++) {
// set result data
resultData[index * 2] = HexEncodeCharsLower[(sourceData[index] >> 4)];
resultData[index * 2 + 1] = HexEncodeCharsLower[(sourceData[index] % 0x10)];
}
}
else {
for (NSUInteger index = 0; index < length; index++) {
// set result data
resultData[index * 2] = HexEncodeChars[(sourceData[index] >> 4)];
resultData[index * 2 + 1] = HexEncodeChars[(sourceData[index] % 0x10)];
}
}
resultData[[data length] * 2] = 0;
// convert result(char[]) to NSString
NSString *result = [NSString stringWithCString:resultData encoding:NSASCIIStringEncoding];
sourceData = nil;
free(resultData);
return result;
}
@end
#pragma mark - CocoaSecurityDecoder
@implementation CocoaSecurityDecoder
- (NSData *)base64:(NSString *)string
{
return [NSData dataWithBase64EncodedString:string];
}
- (NSData *)hex:(NSString *)data
{
if (data.length == 0) { return nil; }
static const unsigned char HexDecodeChars[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, //49
2, 3, 4, 5, 6, 7, 8, 9, 0, 0, //59
0, 0, 0, 0, 0, 10, 11, 12, 13, 14,
15, 0, 0, 0, 0, 0, 0, 0, 0, 0, //79
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 10, 11, 12, //99
13, 14, 15
};
// convert data(NSString) to CString
const char *source = [data cStringUsingEncoding:NSUTF8StringEncoding];
// malloc buffer
unsigned char *buffer;
NSUInteger length = strlen(source) / 2;
buffer = malloc(length);
for (NSUInteger index = 0; index < length; index++) {
buffer[index] = (HexDecodeChars[source[index * 2]] << 4) + (HexDecodeChars[source[index * 2 + 1]]);
}
// init result NSData
NSData *result = [NSData dataWithBytes:buffer length:length];
free(buffer);
source = nil;
return result;
}
@end