消息认证码 hmac
来源:互联网 发布:淘宝拍摄睡袋如何布光 编辑:程序博客网 时间:2024/06/06 05:59
概念介绍
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 Data &key); static Data sha1(const Data &msg, const Data &key);public: ~Hmac(); Hmac(Algorithm alg, const Data &key); Hmac(Hmac &&hmac) noexcept; Hmac &operator=(Hmac &&hmac) noexcept; void update(const Data &data); Data final();private: struct HmacImpl *_impl = nullptr;};#endif /* Hmac_h */
实现文件 Hmac.cpp
#include "Hmac.h"#include <CommonCrypto/CommonHMAC.h>#include <assert.h>struct HmacImpl final {public: HmacImpl(Hmac::Algorithm alg, const void *key, size_t keyLength) { CCHmacAlgorithm hmacAlg; switch (alg) { case Hmac::Algorithm::Md5: hmacAlg = kCCHmacAlgMD5; _hmac.resize(CC_MD5_DIGEST_LENGTH); break; case Hmac::Algorithm::Sha1: hmacAlg = kCCHmacAlgSHA1; _hmac.resize(CC_SHA1_DIGEST_LENGTH); break; } CCHmacInit(&_ctx, hmacAlg, key, keyLength); } void update(const Hmac::Data &data) { CCHmacUpdate(&_ctx, data.data(), data.size()); } Hmac::Data final() { CCHmacFinal(&_ctx, _hmac.data()); return _hmac; }private: CCHmacContext _ctx; Hmac::Data _hmac;};#pragma mark -Hmac::~Hmac(){ delete _impl;}Hmac::Hmac(Algorithm alg, const Data &key) : _impl(new HmacImpl(alg, key.data(), key.size())){}Hmac::Hmac(Hmac &&hmac) noexcept : _impl(hmac._impl){ hmac._impl = nullptr;}Hmac &Hmac::operator=(Hmac &&hmac) noexcept{ assert(&hmac != this); delete _impl; _impl = hmac._impl; hmac._impl = nullptr; return *this;}void Hmac::update(const Data &data){ _impl->update(data);}Hmac::Data Hmac::final(){ return _impl->final();}#pragma mark -Hmac::Data Hmac::md5(const Data &msg, const Data &key){ Hmac md5(Algorithm::Md5, key); md5.update(msg); return md5.final();}Hmac::Data Hmac::sha1(const Data &msg, const Data &key){ Hmac sha1(Algorithm::Sha1, key); sha1.update(msg); return sha1.final();}
示例:
#include <string>#include "Hmac.h"int main(){ Hmac::Data key = { 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 }; // 一般是对称密码的密钥 std::string msg = "hello, world"; Hmac::Data data(msg.cbegin(), msg.cend()); auto sha1_mac = Hmac::sha1(data, key); auto md5_mac = Hmac::md5(data, key); // ...}
阅读全文
0 0
- 消息认证码 hmac
- android 加密:Hmac消息认证作用
- NET(C#):中的基于哈希算法的消息认证码(HMAC)
- java安全(四)HMAC 哈希消息认证
- Bouncy castle HMAC 消息验证码
- REST和认证 HMAC
- HMAC-SHA1签名认证算法
- MAC消息认证码
- MAC --消息认证码
- 【kong系列八】之HMAC认证hmac-auth插件
- 消息摘要算法-HMAC算法
- 消息摘要算法-HMAC算法
- 消息摘要算法-HMAC算法
- OAuth认证的 HMAC-SHA1签署 实现
- HMAC
- HMAC
- hmac
- 消息摘要MD/SHA/HMAC的使用方法
- 如何在Intellij IDEA中修改快捷键
- android Spinner控件详解
- 代码练习存储 选择排序
- 新手制作,大小比较奥
- 基于创建的mogodb数据库,用python分析股票的跳空缺口
- 消息认证码 hmac
- Entity Framework Core 2.0 使用入门
- Visual Studio 15.4发布,新增多平台支持
- Docker简介与入门
- 通过Swashbukle给DotNet Core Web API 增加自动文档功能
- Stack with max and min 查找堆中最大最小数
- JS Jquery去除数组重复元素
- 1024,千家公司程序员幸福指数大比拼!最“幸福”的程序员是你吗?
- Spring--13.事务