本文主要是介绍iOS HMAC SHA1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需要导入cocoaSecurity框架
https://github.com/kelp404/CocoaSecurity
Base64:https://github.com/nicklockwood/Base64
#import "Base64.h"
#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>
+ (NSString *)hmacSha1:(NSString*)key text:(NSString*)text
{
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [text cStringUsingEncoding:NSUTF8StringEncoding];
uint8_t cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
//NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:CC_SHA1_DIGEST_LENGTH];
NSString *hash;
NSMutableString * output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", cHMAC[i]];
hash = output;
return hash;
}
这篇关于iOS HMAC SHA1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!