Created
October 6, 2011 21:55
-
-
Save Uncommon/1268798 to your computer and use it in GitHub Desktop.
Base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
@implementation Base64 | |
+ (NSString*) | |
encode:(NSData*)plainText | |
{ | |
int encodedLength = (((([plainText length] % 3) + [plainText length]) / 3) * 4) + 1; | |
unsigned char *outputBuffer = malloc(encodedLength); | |
unsigned char *inputBuffer = (unsigned char*)[plainText bytes]; | |
NSInteger i; | |
NSInteger j = 0; | |
int remain; | |
for (i = 0; i < [plainText length]; i += 3) { | |
remain = [plainText length] - i; | |
outputBuffer[j++] = alphabet[(inputBuffer[i] & 0xFC) >> 2]; | |
outputBuffer[j++] = alphabet[((inputBuffer[i] & 0x03) << 4) | | |
((remain > 1) ? ((inputBuffer[i + 1] & 0xF0) >> 4): 0)]; | |
if (remain > 1) | |
outputBuffer[j++] = alphabet[((inputBuffer[i + 1] & 0x0F) << 2) | |
| ((remain > 2) ? ((inputBuffer[i + 2] & 0xC0) >> 6) : 0)]; | |
else | |
outputBuffer[j++] = '='; | |
if (remain > 2) | |
outputBuffer[j++] = alphabet[inputBuffer[i + 2] & 0x3F]; | |
else | |
outputBuffer[j++] = '='; | |
} | |
outputBuffer[j] = 0; | |
NSString *result = [NSString stringWithCString:outputBuffer length:strlen(outputBuffer)]; | |
free(outputBuffer); | |
return result; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found at http://www.chrisumbel.com/article/basic_authentication_iphone_cocoa_touch