Skip to content

Instantly share code, notes, and snippets.

@lihnux
Last active August 29, 2015 14:04
Show Gist options
  • Save lihnux/3970d6808bd994da11b9 to your computer and use it in GitHub Desktop.
Save lihnux/3970d6808bd994da11b9 to your computer and use it in GitHub Desktop.
Cooca Base64 Encode & Decode
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
static NSData *base64helper(NSData *input, SecTransformRef transform)
{
NSData *output = nil;
if (!transform)
return nil;
if (SecTransformSetAttribute(transform, kSecTransformInputAttributeName, input, NULL))
output = (NSData *)SecTransformExecute(transform, NULL);
CFRelease(transform);
return [output autorelease];
}
NSString *base64enc(NSData *input)
{
SecTransformRef transform = SecEncodeTransformCreate(kSecBase64Encoding, NULL);
return [[[NSString alloc] initWithData:base64helper(input, transform) encoding:NSASCIIStringEncoding] autorelease];
}
NSData *base64dec(NSString *input)
{
SecTransformRef transform = SecDecodeTransformCreate(kSecBase64Encoding, NULL);
return base64helper([input dataUsingEncoding:NSASCIIStringEncoding], transform);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment