forked from SeriousMonster/moriarty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSString+HMAC.m
38 lines (29 loc) · 1 KB
/
NSString+HMAC.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
//
// NSString+HMAC.m
//
// Created by Tyler Neylon on 5/19/11.
//
#import "NSString+HMAC.h"
#import <CommonCrypto/CommonHMAC.h>
@implementation NSData (Hexit)
- (NSString *)hexString {
NSMutableString *str = [NSMutableString stringWithCapacity:[self length]];
const unsigned char *byte = [self bytes];
const unsigned char *endByte = byte + [self length];
for (; byte != endByte; ++byte) [str appendFormat:@"%02x", *byte];
return str;
}
@end
@implementation NSString (HMAC)
- (NSString *)hexString {
const char *cStr = [self UTF8String];
return [[NSData dataWithBytes:cStr length:strlen(cStr)] hexString];
}
- (NSString *)hmacWithKey:(NSString *)key {
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [self cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
return [[NSData dataWithBytes:cHMAC length:sizeof(cHMAC)] hexString];
}
@end