消息认证码 hmac

本文介绍了HMAC(Hash Message Authentication Code),一种用于验证消息完整性和发送者身份的技术。通过使用对称密钥,发送方和接收方可以确保消息未被篡改并确认发送者。HMAC的计算公式为:hmac = hash(msg, key)。在macOS上,还提供了Hmac.h和Hmac.cpp文件作为代码封装示例。" 84195818,7512809,Vue面试必备知识点详解,"['Vue', '前端开发', 'MVVM', '数据绑定', '组件', '路由']
部署运行你感兴趣的模型镜像

概念介绍

hmac (hash message authentication code) 是用来确认消息的完整性及认证消息的发送者的技术

  1. 完整性,是通过判断hash值来确定的
  2. 认证,是通过对称密码的密钥来完成的

因为是对称密码,所以发送发和接收方事先需要共享密钥

公式:

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);
    // ...
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值