7

关于HMAC签名相关

 3 years ago
source link: https://xnxy.github.io/xnxy.github.io/2020/11/04/%E5%85%B3%E4%BA%8EHMAC%E7%AD%BE%E5%90%8D%E7%9B%B8%E5%85%B3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

关于HMAC签名相关

2020-11-04

|

2020-11-04

| 整理

Overview

节后有个项目使用到腾讯云的活体检测,其实功能也不是很复杂,但在对接腾讯云的时候被他的签名方式恶心了一下,但所在它的文档写的和详细,仔细地阅读了官方文档,最后也在很快的时间内对接成功。

这里主要记录HMAC-SHA1在iOS中的代码,便于后期查阅。

引入头文件

1
2
#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>

HMAC-SHA1算法签名,Base64进行编码

1
2
3
4
5
6
7
8
9
10
11
12
13
- (NSString *)hmacsha1:(NSString *)obj key:(NSString *)key {
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [obj cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];

CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
length:sizeof(cHMAC)];

NSString *hash = [HMAC base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

return hash;
}

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK