Files
faceclaw/App_Resources/iOS/src/FaceclawCrypto.m
T

20 lines
908 B
Objective-C

#import "FaceclawCrypto.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonDigest.h>
@implementation FaceclawCrypto
+ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message {
NSData *key = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, key.bytes, key.length, data.bytes, data.length, digest);
return [[NSData dataWithBytes:digest length:sizeof(digest)] base64EncodedStringWithOptions:0];
}
+ (NSString *)sha256:(NSData *)data {
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(data.bytes, (CC_LONG)data.length, digest);
NSMutableString *hex = [NSMutableString stringWithCapacity:64];
for (NSUInteger i = 0; i < sizeof(digest); i++) [hex appendFormat:@"%02x", digest[i]];
return hex;
}
@end