|
9 | 9 | #import "CocoaSecurity.h"
|
10 | 10 | #import <CommonCrypto/CommonHMAC.h>
|
11 | 11 | #import <CommonCrypto/CommonCryptor.h>
|
| 12 | +#import "Base64.h" |
12 | 13 |
|
13 | 14 | #pragma mark - CocoaSecurity
|
14 | 15 | @implementation CocoaSecurity
|
@@ -419,51 +420,7 @@ @implementation CocoaSecurityEncoder
|
419 | 420 | // convert NSData to Base64
|
420 | 421 | - (NSString *)base64:(NSData *)data
|
421 | 422 | {
|
422 |
| - static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
423 |
| - |
424 |
| - long long inputLength = [data length]; |
425 |
| - const unsigned char *inputBytes = [data bytes]; |
426 |
| - |
427 |
| - long long maxOutputLength = (inputLength / 3 + 1) * 4; |
428 |
| - unsigned char *outputBytes = (unsigned char *)malloc((unsigned long)maxOutputLength); |
429 |
| - |
430 |
| - long long index; |
431 |
| - long long outputLength = 0; |
432 |
| - for (index = 0; index < inputLength - 2; index += 3) |
433 |
| - { |
434 |
| - outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2]; |
435 |
| - outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)]; |
436 |
| - outputBytes[outputLength++] = lookup[((inputBytes[index + 1] & 0x0F) << 2) | ((inputBytes[index + 2] & 0xC0) >> 6)]; |
437 |
| - outputBytes[outputLength++] = lookup[inputBytes[index + 2] & 0x3F]; |
438 |
| - } |
439 |
| - |
440 |
| - //handle left-over data |
441 |
| - if (index == inputLength - 2) |
442 |
| - { |
443 |
| - // = terminator |
444 |
| - outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2]; |
445 |
| - outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)]; |
446 |
| - outputBytes[outputLength++] = lookup[(inputBytes[index + 1] & 0x0F) << 2]; |
447 |
| - outputBytes[outputLength++] = '='; |
448 |
| - } |
449 |
| - else if (index == inputLength - 1) |
450 |
| - { |
451 |
| - // == terminator |
452 |
| - outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2]; |
453 |
| - outputBytes[outputLength++] = lookup[(inputBytes[index] & 0x03) << 4]; |
454 |
| - outputBytes[outputLength++] = '='; |
455 |
| - outputBytes[outputLength++] = '='; |
456 |
| - } |
457 |
| - |
458 |
| - NSString *result; |
459 |
| - if (outputLength >= 4) |
460 |
| - { |
461 |
| - //truncate data to match actual output length |
462 |
| - outputBytes = realloc(outputBytes, (unsigned long)outputLength); |
463 |
| - result = [[NSString alloc] initWithBytes:outputBytes length:(unsigned long)outputLength encoding:NSASCIIStringEncoding]; |
464 |
| - } |
465 |
| - free(outputBytes); |
466 |
| - return result; |
| 423 | + return [data base64EncodedString]; |
467 | 424 | }
|
468 | 425 |
|
469 | 426 | // convert NSData to hex string
|
@@ -510,52 +467,7 @@ - (NSString *)hex:(NSData *)data useLower:(BOOL)isOutputLower
|
510 | 467 | @implementation CocoaSecurityDecoder
|
511 | 468 | - (NSData *)base64:(NSString *)string
|
512 | 469 | {
|
513 |
| - static const char lookup[] = |
514 |
| - { |
515 |
| - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, |
516 |
| - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, |
517 |
| - 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63, |
518 |
| - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 99, 99, 99, |
519 |
| - 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
520 |
| - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 99, 99, 99, 99, 99, |
521 |
| - 99, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
522 |
| - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 99, 99, 99, 99, 99 |
523 |
| - }; |
524 |
| - |
525 |
| - NSData *inputData = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; |
526 |
| - NSUInteger inputLength = [inputData length]; |
527 |
| - const unsigned char *inputBytes = [inputData bytes]; |
528 |
| - |
529 |
| - NSMutableData *outputData = [NSMutableData dataWithLength:(inputLength / 4 + 1) * 3]; |
530 |
| - unsigned char *outputBytes = (unsigned char *)[outputData mutableBytes]; |
531 |
| - |
532 |
| - int accumulator = 0; |
533 |
| - long long outputLength = 0; |
534 |
| - unsigned char accumulated[] = {0, 0, 0, 0}; |
535 |
| - for (NSUInteger index = 0; index < inputLength; index++) |
536 |
| - { |
537 |
| - unsigned char decoded = lookup[inputBytes[index] & 0x7F]; |
538 |
| - if (decoded != 99) |
539 |
| - { |
540 |
| - accumulated[accumulator] = decoded; |
541 |
| - if (accumulator == 3) |
542 |
| - { |
543 |
| - outputBytes[outputLength++] = (accumulated[0] << 2) | (accumulated[1] >> 4); |
544 |
| - outputBytes[outputLength++] = (accumulated[1] << 4) | (accumulated[2] >> 2); |
545 |
| - outputBytes[outputLength++] = (accumulated[2] << 6) | accumulated[3]; |
546 |
| - } |
547 |
| - accumulator = (accumulator + 1) % 4; |
548 |
| - } |
549 |
| - } |
550 |
| - |
551 |
| - //handle left-over data |
552 |
| - if (accumulator > 0) outputBytes[outputLength] = (accumulated[0] << 2) | (accumulated[1] >> 4); |
553 |
| - if (accumulator > 1) outputBytes[++outputLength] = (accumulated[1] << 4) | (accumulated[2] >> 2); |
554 |
| - if (accumulator > 2) outputLength++; |
555 |
| - |
556 |
| - //truncate data to match actual output length |
557 |
| - outputData.length = outputLength; |
558 |
| - return outputLength? outputData: nil; |
| 470 | + return [NSData dataWithBase64EncodedString:string]; |
559 | 471 | }
|
560 | 472 | - (NSData *)hex:(NSString *)data
|
561 | 473 | {
|
|
0 commit comments