C++ JAVA源码 HMAC计算 openssl 消息认证码计算 https消息防篡改 通信安全

签名和验签

把所有消息按顺序合并成一条信息,对这个信息用密钥进行签名。

签名信息通过 HTTP 头 Sign 传递,没有携带签名或者签名验证不通过的请求,将会被认为异常请求,并返回相应 code 码。

校验方法:根据 http请求的原始 Body 及请求头内参数等所有消息按顺序合并成一条信息 进行签名生成 sign。对自生成的 sign 与 传过来的Sign 做对比,相同则校验通过,反之则校验失败。

在线工具 https://www.lddgo.net/encrypt/hmac

C++代码验证

要在 C++ 中计算 HMAC-SHA256 的结果并将其转换为 Base64 输出,你可以结合 OpenSSL 库进行 HMAC 计算,并使用 Base64 编码函数来转换结果。下面是一个示例代码:

cpp 复制代码
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <iostream>
#include <string>

// Convert HMAC result to Base64
std::string base64_encode(const unsigned char* buffer, size_t length) {
    BIO* bio = BIO_new(BIO_f_base64());
    BIO* mem = BIO_new(BIO_s_mem());
    bio = BIO_push(bio, mem);

    // Do not use newlines to flush buffer
    BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
    BIO_write(bio, buffer, length);
    BIO_flush(bio);

    BUF_MEM* mem_ptr;
    BIO_get_mem_ptr(bio, &mem_ptr);

    std::string base64_result(mem_ptr->data, mem_ptr->length);
    BIO_free_all(bio);

    return base64_result;
}

// Compute HMAC-SHA256 and encode result in Base64
std::string hmac_sha256_base64(const std::string& key, const std::string& message) {
    unsigned char* digest;
    unsigned int digest_len;

    digest = HMAC(EVP_sha256(),
                  key.c_str(), key.length(),
                  reinterpret_cast<const unsigned char*>(message.c_str()), message.length(),
                  nullptr, &digest_len);

    // Convert the HMAC digest to Base64
    return base64_encode(digest, digest_len);
}

int main() {
    std::string key = "your-key";        // Replace with your key
    std::string message = "your-message";  // Replace with your message

    std::string hmac_base64 = hmac_sha256_base64(key, message);

    std::cout << "HMAC-SHA256 (Base64): " << hmac_base64 << std::endl;

    return 0;
}

编译与运行

  1. 编译

    bash 复制代码
    g++ -o hmac_tool hmac_tool.cpp -lssl -lcrypto
  2. 运行

    bash 复制代码
    ./hmac_tool

代码解释:

  • HMAC Calculation : 使用 HMAC 函数来计算 HMAC-SHA256,结果保存在 digest 中。
  • Base64 编码 : 使用 OpenSSL 的 BIO 来进行 Base64 编码。BIO 是 OpenSSL 中用于处理数据流的结构。
  • BIO_f_base64BIO_s_mem : 分别用于创建 Base64 过滤器和内存 BIO。BIO_push 将它们连接在一起,使数据能够经过 Base64 编码后存储在内存中。
  • BIO_flush : 确保所有数据都已写入 BIO
  • base64_encode 函数: 将 digest 转换为 Base64 字符串并返回。

运行程序后,它将输出 HMAC-SHA256 结果的 Base64 编码版本。

java

java 复制代码
public static String HMACSHA256(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));

        return Base64.getUrlEncoder().encodeToString(array); //这里不要用urlEncoder,以免c++没有对应的库 对Hmac转base64就行。
    }
相关推荐
技术无疆15 分钟前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
迷迭所归处3 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ3 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
CV工程师小林3 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
拾光师4 小时前
spring获取当前request
java·后端·spring
white__ice4 小时前
2024.9.19
c++
aPurpleBerry4 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j
天玑y4 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
我是苏苏4 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
姜太公钓鲸2334 小时前
c++ static(详解)
开发语言·c++