-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITCategory-NSData.m
76 lines (60 loc) · 1.87 KB
/
ITCategory-NSData.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#import "ITCategory-NSData.h"
#import <openssl/bio.h>
#import <openssl/evp.h>
#import <openssl/md5.h>
#import <openssl/sha.h>
@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;
char *dataBytes = [self bytes];
char hexString[stringLength];
int i;
for (i=0; i < dataLength; i++) {
sprintf(hexString + (i * 2), "%02x", dataBytes[i]);
}
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];
MD5([self bytes], [self length], digest);
return [NSData dataWithBytes:&digest length:length];
}
- (NSData *)SHA1 {
int length = 20;
unsigned char digest[length];
SHA1([self bytes], [self length], digest);
return [NSData dataWithBytes:&digest length:length];
}
@end