Skip to content

Instantly share code, notes, and snippets.

@ezodude
Created May 6, 2014 10:08
Show Gist options
  • Save ezodude/1272aea9e0bd3ba23de3 to your computer and use it in GitHub Desktop.
Save ezodude/1272aea9e0bd3ba23de3 to your computer and use it in GitHub Desktop.
//
// NSString+SO.m
//
// Created by Ezo Saleh on 04/12/2011.
//
#import "NSString+SO.h"
@implementation NSString (SO)
//Source http://www.cocoadev.com/index.pl?BaseSixtyFour
+ (NSString *)base64forData:(NSData *)data
{
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if ([data length] == 0)
return @"";
char *characters = malloc((([data length] + 2) / 3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [data length])
{
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < [data length])
buffer[bufferLength++] = ((char *)[data bytes])[i++];
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
}
- (BOOL)isOnlyFromCharacterSet:(NSCharacterSet *)aCharacterSet {
NSString *s = [[self lowercaseString] copy];
unichar c;
for(NSUInteger i = 0; i < [s length]; i++) {
[s getCharacters:&c range:NSMakeRange(i, 1)];
if([aCharacterSet characterIsMember:c] == NO) {
return NO;
}
}
return YES;
}
- (BOOL)isHexadecimal {
NSCharacterSet *hexadecimalCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdef"];
return [self isOnlyFromCharacterSet:hexadecimalCharacterSet];
}
-(NSString *)MD5{
// Create pointer to the string as UTF8
const char *ptr = [self UTF8String];
// Create byte array of unsigned chars
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
// Create 16 byte MD5 hash value, store in buffer
CC_MD5(ptr, strlen(ptr), md5Buffer);
// Convert MD5 value in the buffer to NSString of hex values
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x",md5Buffer[i]];
return output;
}
-(NSString *) SHA1{
const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:self.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment