Skip to content

Instantly share code, notes, and snippets.

@Uncommon
Created October 6, 2011 21:55
Show Gist options
  • Save Uncommon/1268798 to your computer and use it in GitHub Desktop.
Save Uncommon/1268798 to your computer and use it in GitHub Desktop.
Base64
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
@Uncommon
Copy link
Author

Uncommon commented Oct 6, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment