概念介绍
hmac (hash message authentication code) 是用来确认消息的完整性及认证消息的发送者的技术
- 完整性,是通过判断hash值来确定的
- 认证,是通过对称密码的密钥来完成的
因为是对称密码,所以发送发和接收方事先需要共享密钥
公式:
hmac = hash(msg, key)
发送方发送 msg + hmac
接收方利用公式 hash(msg, key)计算出hmac,然后与接收到的hmac进行对比,如果相同则可确定消息没有被篡改,且是发送者发送的
macOS 上代码封装
头文件 Hmac.h
#ifndef Hmac_h
#define Hmac_h
#include <stdint.h>
#include <vector>
class Hmac final {
public:
enum struct Algorithm {
Md5,
Sha1,
};
public:
typedef std::vector<uint8_t> Data;
public:
static Data md5(const Data &msg, const