X-Git-Url: http://git.ithinksw.org/ITFoundation.git/blobdiff_plain/744612b5d258474e367710b870a5caaed7d11ab9..8e10b21b0ba4dc981faebcb359143b474309719b:/ITCategory-NSData.m diff --git a/ITCategory-NSData.m b/ITCategory-NSData.m index 0f45f68..e35fa45 100644 --- a/ITCategory-NSData.m +++ b/ITCategory-NSData.m @@ -1,9 +1,32 @@ #import "ITCategory-NSData.h" +#import +#import #import #import @implementation NSData (ITFoundationCategory) ++ (id)dataWithBase64:(NSString *)base64 { + return [[[self alloc] initWithBase64:base64] autorelease]; +} + +- (id)initWithBase64:(NSString *)base64 { + BIO *mem = BIO_new_mem_buf((void *)[base64 cString], [base64 cStringLength]); + BIO *b64 = BIO_new(BIO_f_base64()); + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + NSMutableData *data = [NSMutableData data]; + char inbuf[512]; + int inlen; + while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0) { + [data appendBytes:inbuf length:inlen]; + } + + BIO_free_all(mem); + return [self initWithData:data]; +} + - (NSString *)hexadecimalRepresentation { int dataLength = [self length]; int stringLength = dataLength * 2; @@ -19,6 +42,23 @@ return [NSString stringWithCString:hexString length:stringLength]; } +- (NSString *)base64 { + BIO *mem = BIO_new(BIO_s_mem()); + BIO *b64 = BIO_new(BIO_f_base64()); + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + BIO_write(mem, [self bytes], [self length]); + BIO_flush(mem); + + char *base64Char; + long base64Length = BIO_get_mem_data(mem, &base64Char); + NSString *base64String = [NSString stringWithCString:base64Char length:base64Length]; + + BIO_free_all(mem); + return base64String; +} + - (NSData *)MD5 { int length = 16; unsigned char digest[length];