資源簡介
objective-c實現authCode 解決php與ios通信加密的問題

代碼片段和文件信息
#import?“base64.h“
#import?
#import?
#if?!__has_feature(objc_arc)
#error?This?library?requires?automatic?reference?counting
#endif
@implementation?NSData?(base64)
+?(NSData?*)dataWithbase64EncodedString:(NSString?*)string
{
????const?char?lookup[]?=
????{
????????99?99?99?99?99?99?99?99?99?99?99?99?99?99?99?99
????????99?99?99?99?99?99?99?99?99?99?99?99?99?99?99?99
????????99?99?99?99?99?99?99?99?99?99?99?62?99?99?99?63
????????52?53?54?55?56?57?58?59?60?61?99?99?99?99?99?99
????????99??0??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?99?99?99?99?99
????????99?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?99?99?99?99?99
????};
????
????NSData?*inputData?=?[string?dataUsingEncoding:NSASCIIStringEncoding?allowLossyConversion:YES];
????long?long?inputLength?=?[inputData?length];
????const?unsigned?char?*inputBytes?=?[inputData?bytes];
????
????long?long?maxOutputLength?=?(inputLength?/?4?+?1)?*?3;
????NSMutableData?*outputData?=?[NSMutableData?dataWithLength:maxOutputLength];
????unsigned?char?*outputBytes?=?(unsigned?char?*)[outputData?mutableBytes];
????
????int?accumulator?=?0;
????long?long?outputLength?=?0;
????unsigned?char?accumulated[]?=?{0?0?0?0};
????for?(long?long?i?=?0;?i?????{
????????unsigned?char?decoded?=?lookup[inputBytes[i]?&?0x7F];
????????if?(decoded?!=?99)
????????{
????????????accumulated[accumulator]?=?decoded;
????????????if?(accumulator?==?3)
????????????{
????????????????outputBytes[outputLength++]?=?(accumulated[0]?<2)?|?(accumulated[1]?>>?4);
????????????????outputBytes[outputLength++]?=?(accumulated[1]?<4)?|?(accumulated[2]?>>?2);
????????????????outputBytes[outputLength++]?=?(accumulated[2]?<6)?|?accumulated[3];
????????????}
????????????accumulator?=?(accumulator?+?1)?%?4;
????????}
????}
????
????//handle?left-over?data
????if?(accumulator?>?0)?outputBytes[outputLength]?=?(accumulated[0]?<2)?|?(accumulated[1]?>>?4);
????if?(accumulator?>?1)?outputBytes[++outputLength]?=?(accumulated[1]?<4)?|?(accumulated[2]?>>?2);
????if?(accumulator?>?2)?outputLength++;
????
????//truncate?data?to?match?actual?output?length
????outputData.length?=?outputLength;
????return?outputLength??outputData:?nil;
}
-?(NSString?*)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth
{
????//ensure?wrapWidth?is?a?multiple?of?4
????wrapWidth?=?(wrapWidth?/?4)?*?4;
????
????const?char?lookup[]?=?“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/“;
????
????long?long?inputLength?=?[self?length];
????const?unsigned?char?*inputBytes?=?[self?bytes];
????
????long?long?maxOutputLength?=?(inputLength?/?3?+?1)?*?4;
????maxOutputLength?+=?wrapWidth??(maxOutputLength?/?wrapWidth)?*?2:?0;
????unsigned?char?*outputBytes?=?(unsigned?char?*)malloc(maxOutputLength);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-03-05?11:44??authCode\
?????文件????????6148??2015-03-05?11:44??authCode\.DS_Store
?????目錄???????????0??2015-03-05?11:49??__MACOSX\
?????目錄???????????0??2015-03-05?11:49??__MACOSX\authCode\
?????文件?????????120??2015-03-05?11:44??__MACOSX\authCode\._.DS_Store
?????目錄???????????0??2015-03-05?11:45??authCode\ios\
?????文件????????6148??2015-03-05?11:48??authCode\ios\.DS_Store
?????目錄???????????0??2015-03-05?11:49??__MACOSX\authCode\ios\
?????文件?????????120??2015-03-05?11:48??__MACOSX\authCode\ios\._.DS_Store
?????文件?????????549??2015-03-05?11:45??authCode\ios\ba
?????文件?????????171??2015-03-05?11:45??__MACOSX\authCode\ios\._ba
?????文件????????9673??2015-03-05?11:45??authCode\ios\ba
?????文件?????????171??2015-03-05?11:45??__MACOSX\authCode\ios\._ba
?????文件??????????75??2015-03-05?11:48??authCode\ios\ViewController.h
?????文件?????????171??2015-03-05?11:48??__MACOSX\authCode\ios\._ViewController.h
?????文件?????????857??2015-03-05?11:48??authCode\ios\ViewController.m
?????文件?????????171??2015-03-05?11:48??__MACOSX\authCode\ios\._ViewController.m
?????目錄???????????0??2015-03-05?11:44??authCode\php\
?????文件????????6148??2015-03-05?11:44??authCode\php\.DS_Store
?????目錄???????????0??2015-03-05?11:49??__MACOSX\authCode\php\
?????文件?????????120??2015-03-05?11:44??__MACOSX\authCode\php\._.DS_Store
?????文件????????1713??2015-03-05?11:43??authCode\php\test.php
?????文件?????????171??2015-03-05?11:43??__MACOSX\authCode\php\._test.php
評論
共有 條評論