11 #import "TSXAdditions.h"
12 #include <openssl/bio.h>
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
18 - (void)setValuesForKeyPathsWithDictionary:(NSDictionary *)keyedValues
20 for (
id keyPath in keyedValues)
21 [
self setValue:[keyedValues objectForKey:keyPath] forKeyPath:keyPath];
26 if ([
self respondsToSelector:
@selector(mutableCopyWithZone:)])
27 return [
self mutableCopy];
28 else if ([
self respondsToSelector:
@selector(copyWithZone:)])
40 #pragma mark Creation routines
41 + (NSString *)stringWithUUID
43 CFUUIDRef uuidObj = CFUUIDCreate(nil);
44 NSString *uuidString = (NSString *)CFUUIDCreateString(nil, uuidObj);
46 return [uuidString autorelease];
52 - (NSData *)dataFromHexString
54 NSData *hexData = [
self dataUsingEncoding:NSASCIIStringEncoding];
55 const char *hexBuf = [hexData bytes];
56 NSUInteger hexLen = [hexData length];
62 if ((hexLen % 2) != 0)
67 NSMutableData *binaryData = [NSMutableData dataWithLength:(hexLen / 2)];
68 unsigned char *binaryPtr = [binaryData mutableBytes];
69 unsigned char value = 0;
70 for (NSUInteger i = 0; i < hexLen; i++)
85 value += 10 + c - 'a';
89 value += 10 + c - 'A';
103 return [NSData dataWithData:binaryData];
106 + (NSString *)hexStringFromData:(const
unsigned char *)data
107 ofSize:(
unsigned int)size
108 withSeparator:(NSString *)sep
109 afterNthChar:(
int)sepnth
111 NSMutableString *result;
112 NSString *immutableResult;
114 result = [[NSMutableString alloc] init];
115 for (
int i = 0; i < size; i++)
117 if (i && sep && sepnth && i % sepnth == 0)
118 [result appendString:sep];
119 [result appendFormat:@"%02X", data[i]];
122 immutableResult = [NSString stringWithString:result];
124 return immutableResult;
129 #pragma mark Mutable deep copy for dicionary, array and set
135 NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] init];
136 NSEnumerator *enumerator = [
self keyEnumerator];
138 while ((key = [enumerator nextObject]))
140 id obj = [[
self objectForKey:key] mutableDeepCopy];
141 [newDictionary setObject:obj forKey:key];
144 return newDictionary;
149 @implementation NSArray (TSXAdditions)
153 NSMutableArray *newArray = [[NSMutableArray alloc] init];
154 NSEnumerator *enumerator = [
self objectEnumerator];
156 while ((obj = [enumerator nextObject]))
158 obj = [obj mutableDeepCopy];
159 [newArray addObject:obj];
167 @implementation NSSet (TSXAdditions)
171 NSMutableSet *newSet = [[NSMutableSet alloc] init];
172 NSEnumerator *enumerator = [
self objectEnumerator];
174 while ((obj = [enumerator nextObject]))
176 obj = [obj mutableDeepCopy];
177 [newSet addObject:obj];
192 #pragma mark - String Conversion
193 - (NSString *)hexadecimalString
197 const unsigned char *dataBuffer = (
const unsigned char *)[
self bytes];
200 return [NSString
string];
202 NSUInteger dataLength = [
self length];
203 NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
205 for (
int i = 0; i < dataLength; ++i)
206 [hexString appendString:[NSString stringWithFormat:
@"%02lx", (
unsigned long)dataBuffer[i]]];
208 return [NSString stringWithString:hexString];
212 - (NSString *)base64EncodedString
215 BIO *context = BIO_new(BIO_s_mem());
218 BIO *command = BIO_new(BIO_f_base64());
219 context = BIO_push(command, context);
220 BIO_set_flags(context, BIO_FLAGS_BASE64_NO_NL);
224 BIO_write(context, [
self bytes], [
self length]);
225 (void)BIO_flush(context);
229 long outputLength = BIO_get_mem_data(context, &outputBuffer);
230 NSString *encodedString = [[NSString alloc] initWithBytes:outputBuffer
232 encoding:NSASCIIStringEncoding];
234 BIO_free_all(context);
236 return encodedString;