updated Base64 module.

This commit is contained in:
Kelp 2014-01-27 11:03:30 +08:00
parent d49fa1cd57
commit a7161a78d9
4 changed files with 23 additions and 91 deletions

3
.gitmodules vendored
View File

@ -2,3 +2,6 @@
path = xctool
url = https://github.com/facebook/xctool.git
ignore = dirty
[submodule "submodules/Base64"]
path = submodules/Base64
url = https://github.com/nicklockwood/Base64.git

View File

@ -18,6 +18,8 @@
21540A04183DD7F000E4C15B /* CocoaSecurityEncoder_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 21540A00183DD7F000E4C15B /* CocoaSecurityEncoder_Tests.m */; };
21540A05183DD7F000E4C15B /* CocoaSecurityResult_Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 21540A01183DD7F000E4C15B /* CocoaSecurityResult_Test.m */; };
21540A06183DD81A00E4C15B /* CocoaSecurity.m in Sources */ = {isa = PBXBuildFile; fileRef = 2113924116EAD6AF00AFDF87 /* CocoaSecurity.m */; };
21FB346C189601A6002F38DE /* Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 21FB346B189601A6002F38DE /* Base64.m */; };
21FB346D18960347002F38DE /* Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 21FB346B189601A6002F38DE /* Base64.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -59,6 +61,8 @@
21540A00183DD7F000E4C15B /* CocoaSecurityEncoder_Tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CocoaSecurityEncoder_Tests.m; sourceTree = "<group>"; };
21540A01183DD7F000E4C15B /* CocoaSecurityResult_Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CocoaSecurityResult_Test.m; sourceTree = "<group>"; };
21BB8A2E17FEA0EC0032C38C /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
21FB346A189601A6002F38DE /* Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Base64.h; path = submodules/Base64/Base64/Base64.h; sourceTree = SOURCE_ROOT; };
21FB346B189601A6002F38DE /* Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Base64.m; path = submodules/Base64/Base64/Base64.m; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -115,6 +119,7 @@
2113923C16EAD6AF00AFDF87 /* CocoaSecurity */ = {
isa = PBXGroup;
children = (
21FB34691896019A002F38DE /* Submodules */,
2113923F16EAD6AF00AFDF87 /* CocoaSecurity.h */,
2113924116EAD6AF00AFDF87 /* CocoaSecurity.m */,
);
@ -143,6 +148,15 @@
name = "Supporting Files";
sourceTree = "<group>";
};
21FB34691896019A002F38DE /* Submodules */ = {
isa = PBXGroup;
children = (
21FB346A189601A6002F38DE /* Base64.h */,
21FB346B189601A6002F38DE /* Base64.m */,
);
name = Submodules;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -228,6 +242,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
21FB346C189601A6002F38DE /* Base64.m in Sources */,
2113924216EAD6AF00AFDF87 /* CocoaSecurity.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -236,6 +251,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
21FB346D18960347002F38DE /* Base64.m in Sources */,
21540A06183DD81A00E4C15B /* CocoaSecurity.m in Sources */,
21540A05183DD7F000E4C15B /* CocoaSecurityResult_Test.m in Sources */,
21540A03183DD7F000E4C15B /* CocoaSecurityDecoder_Tests.m in Sources */,

View File

@ -9,6 +9,7 @@
#import "CocoaSecurity.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>
#import "Base64.h"
#pragma mark - CocoaSecurity
@implementation CocoaSecurity
@ -419,51 +420,7 @@
// convert NSData to Base64
- (NSString *)base64:(NSData *)data
{
static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
long long inputLength = [data length];
const unsigned char *inputBytes = [data bytes];
long long maxOutputLength = (inputLength / 3 + 1) * 4;
unsigned char *outputBytes = (unsigned char *)malloc((unsigned long)maxOutputLength);
long long index;
long long outputLength = 0;
for (index = 0; index < inputLength - 2; index += 3)
{
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)];
outputBytes[outputLength++] = lookup[((inputBytes[index + 1] & 0x0F) << 2) | ((inputBytes[index + 2] & 0xC0) >> 6)];
outputBytes[outputLength++] = lookup[inputBytes[index + 2] & 0x3F];
}
//handle left-over data
if (index == inputLength - 2)
{
// = terminator
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)];
outputBytes[outputLength++] = lookup[(inputBytes[index + 1] & 0x0F) << 2];
outputBytes[outputLength++] = '=';
}
else if (index == inputLength - 1)
{
// == terminator
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0x03) << 4];
outputBytes[outputLength++] = '=';
outputBytes[outputLength++] = '=';
}
NSString *result;
if (outputLength >= 4)
{
//truncate data to match actual output length
outputBytes = realloc(outputBytes, (unsigned long)outputLength);
result = [[NSString alloc] initWithBytes:outputBytes length:(unsigned long)outputLength encoding:NSASCIIStringEncoding];
}
free(outputBytes);
return result;
return [data base64EncodedString];
}
// convert NSData to hex string
@ -510,52 +467,7 @@
@implementation CocoaSecurityDecoder
- (NSData *)base64:(NSString *)string
{
static 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];
NSUInteger inputLength = [inputData length];
const unsigned char *inputBytes = [inputData bytes];
NSMutableData *outputData = [NSMutableData dataWithLength:(inputLength / 4 + 1) * 3];
unsigned char *outputBytes = (unsigned char *)[outputData mutableBytes];
int accumulator = 0;
long long outputLength = 0;
unsigned char accumulated[] = {0, 0, 0, 0};
for (NSUInteger index = 0; index < inputLength; index++)
{
unsigned char decoded = lookup[inputBytes[index] & 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;
return [NSData dataWithBase64EncodedString:string];
}
- (NSData *)hex:(NSString *)data
{

1
submodules/Base64 Submodule

@ -0,0 +1 @@
Subproject commit 613d17caeb02928df50e77cc438439cccf95c4cd