Last active
August 29, 2015 14:04
-
-
Save lihnux/3970d6808bd994da11b9 to your computer and use it in GitHub Desktop.
Cooca Base64 Encode & Decode
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
#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