openssl3.2 - 官方demo学习 - mac - hmac-sha512.c

文章目录

    • openssl3.2 - 官方demo学习 - mac - hmac-sha512.c
    • 概述
    • 笔记
    • END

openssl3.2 - 官方demo学习 - mac - hmac-sha512.c

概述

MAC算法为HMAC, 设置参数(摘要算法为SHA3-512), 用key初始化, 对明文做MAC数据.

笔记

/*!
\file hmac-sha512.c
\note
openssl3.2 - 官方demo学习 - mac - hmac-sha512.c
MAC算法为HMAC, 设置参数(摘要算法为SHA3-512), 用key初始化, 对明文做MAC数据.
*//*-* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.** Licensed under the Apache License 2.0 (the "License").  You may not use* this file except in compliance with the License.  You can obtain a copy* in the file LICENSE in the source distribution or at* https://www.openssl.org/source/license.html*//** Example of using EVP_MAC_ methods to calculate* a HMAC of static buffers*/#include <string.h>
#include <stdio.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/params.h>#include "my_openSSL_lib.h"/** Hard coding the key into an application is very bad.* It is done here solely for educational purposes.*/
static unsigned char key[] = {0x25, 0xfd, 0x12, 0x99, 0xdf, 0xad, 0x1a, 0x03,0x0a, 0x81, 0x3c, 0x2d, 0xcc, 0x05, 0xd1, 0x5c,0x17, 0x7a, 0x36, 0x73, 0x17, 0xef, 0x41, 0x75,0x71, 0x18, 0xe0, 0x1a, 0xda, 0x99, 0xc3, 0x61,0x38, 0xb5, 0xb1, 0xe0, 0x82, 0x2c, 0x70, 0xa4,0xc0, 0x8e, 0x5e, 0xf9, 0x93, 0x9f, 0xcf, 0xf7,0x32, 0x4d, 0x0c, 0xbd, 0x31, 0x12, 0x0f, 0x9a,0x15, 0xee, 0x82, 0xdb, 0x8d, 0x29, 0x54, 0x14,
};static const unsigned char data[] =
"To be, or not to be, that is the question,\n"
"Whether tis nobler in the minde to suffer\n"
"The ſlings and arrowes of outragious fortune,\n"
"Or to take Armes again in a sea of troubles,\n"
"And by opposing, end them, to die to sleep;\n"
"No more, and by a sleep, to say we end\n"
"The heart-ache, and the thousand natural shocks\n"
"That flesh is heir to? tis a consumation\n"
"Devoutly to be wished. To die to sleep,\n"
"To sleepe, perchance to dreame, Aye, there's the rub,\n"
"For in that sleep of death what dreams may come\n"
"When we haue shuffled off this mortal coil\n"
"Must give us pause. There's the respect\n"
"That makes calamity of so long life:\n"
"For who would bear the Ships and Scorns of time,\n"
"The oppressor's wrong, the proud man's Contumely,\n"
"The pangs of dispised love, the Law's delay,\n"
;/* The known value of the HMAC/SHA3-512 MAC of the above soliloqy */
static const unsigned char expected_output[] = {0x3b, 0x77, 0x5f, 0xf1, 0x4f, 0x9e, 0xb9, 0x23,0x8f, 0xdc, 0xa0, 0x68, 0x15, 0x7b, 0x8a, 0xf1,0x96, 0x23, 0xaa, 0x3c, 0x1f, 0xe9, 0xdc, 0x89,0x11, 0x7d, 0x58, 0x07, 0xe7, 0x96, 0x17, 0xe3,0x44, 0x8b, 0x03, 0x37, 0x91, 0xc0, 0x6e, 0x06,0x7c, 0x54, 0xe4, 0xa4, 0xcc, 0xd5, 0x16, 0xbb,0x5e, 0x4d, 0x64, 0x7d, 0x88, 0x23, 0xc9, 0xb7,0x25, 0xda, 0xbe, 0x4b, 0xe4, 0xd5, 0x34, 0x30,
};/** A property query used for selecting the MAC implementation.*/
static const char* propq = NULL;int main(void)
{int ret = EXIT_FAILURE;OSSL_LIB_CTX* _ossl_lib_ctx = NULL;EVP_MAC* _evp_mac = NULL;EVP_MAC_CTX* _evp_mac_ctx = NULL;EVP_MD_CTX* _evp_md_ctx = NULL;unsigned char* out = NULL;size_t out_len = 0;OSSL_PARAM params[4], * p = params;char digest_name[] = "SHA3-512";_ossl_lib_ctx = OSSL_LIB_CTX_new();if (_ossl_lib_ctx == NULL) {fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");goto end;}/* Fetch the HMAC implementation */_evp_mac = EVP_MAC_fetch(_ossl_lib_ctx, "HMAC", propq);if (_evp_mac == NULL) {fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");goto end;}/* Create a context for the HMAC operation */_evp_mac_ctx = EVP_MAC_CTX_new(_evp_mac);if (_evp_mac_ctx == NULL) {fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");goto end;}/* The underlying digest to be used */*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name,sizeof(digest_name));*p = OSSL_PARAM_construct_end();/* Initialise the HMAC operation */if (!EVP_MAC_init(_evp_mac_ctx, key, sizeof(key), params)) {fprintf(stderr, "EVP_MAC_init() failed\n");goto end;}/* Make one or more calls to process the data to be authenticated */if (!EVP_MAC_update(_evp_mac_ctx, data, sizeof(data))) {fprintf(stderr, "EVP_MAC_update() failed\n");goto end;}/* Make a call to the final with a NULL buffer to get the length of the MAC */if (!EVP_MAC_final(_evp_mac_ctx, NULL, &out_len, 0)) {fprintf(stderr, "EVP_MAC_final() failed\n");goto end;}out = OPENSSL_malloc(out_len);if (out == NULL) {fprintf(stderr, "malloc failed\n");goto end;}/* Make one call to the final to get the MAC */if (!EVP_MAC_final(_evp_mac_ctx, out, &out_len, out_len)) {fprintf(stderr, "EVP_MAC_final() failed\n");goto end;}printf("Generated MAC:\n");BIO_dump_indent_fp(stdout, out, (int)out_len, 2);putchar('\n');if (out_len != sizeof(expected_output)) {fprintf(stderr, "Generated MAC has an unexpected length\n");goto end;}if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {fprintf(stderr, "Generated MAC does not match expected value\n");goto end;}ret = EXIT_SUCCESS;
end:if (ret != EXIT_SUCCESS)ERR_print_errors_fp(stderr);/* OpenSSL free functions will ignore NULL arguments */OPENSSL_free(out);EVP_MD_CTX_free(_evp_md_ctx);EVP_MAC_CTX_free(_evp_mac_ctx);EVP_MAC_free(_evp_mac);OSSL_LIB_CTX_free(_ossl_lib_ctx);return ret;
}

END

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/624478.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用numpy处理图片——滤镜

大纲 3维数组切分打平重组法深度切分法 3维数组堆叠 我们在用手机拍照片时&#xff0c;往往会对照片进行滤镜处理&#xff0c;从而让照片更加美观。本文我们将实现几种滤镜效果——去除所有像素中的某一种原色&#xff0c;形成只有红绿、红蓝和绿蓝原色的照片。 为了突出色彩丰…

Leetcode面试经典150题刷题记录 —— 数学篇

Leetcode面试经典150题刷题记录-系列Leetcod面试经典150题刷题记录——数组 / 字符串篇Leetcod面试经典150题刷题记录 —— 双指针篇Leetcod面试经典150题刷题记录 —— 矩阵篇Leetcod面试经典150题刷题记录 —— 滑动窗口篇Leetcod面试经典150题刷题记录 —— 哈希表篇Leetcod…

WebGL简介以及使用

WebGL简介 WebGL&#xff08;Web图形库&#xff09; 是一种在没有使用插件的情况下在网页浏览器中渲染2D图形和3D图形的技术。它基于OpenGL ES&#xff0c;一个在嵌入式系统中广泛使用的图形API。WebGL通过HTML5的 <canvas> 元素直接在网页上实现图形渲染&#xff0c;使…

定制服务器有什么优势优点?

定制服务器是指在根据用户的需求和业务特点&#xff0c;专门设计和制造的服务器。与标准服务器相比&#xff0c;定制服务器具有以下优势和优点&#xff1a; 更好的性能&#xff1a;定制服务器可以针对特定应用进行优化&#xff0c;从而提高服务器的性能。由于定制服务器不需要…

Win和Mac系统重置系统方法

注意&#xff1a;重置系统前&#xff0c;请备份好系统盘资料到其他盘符&#xff01;重置系统将会删除应用和系统设置&#xff0c;甚至用户文件&#xff0c;还原为出厂设置模式。 Windows重置系统操作方法。&#xff08;目前支持WIN8&#xff0c;WIN10&#xff0c;WIN11&#x…

Linux系统使用docker部署Geoserver(简单粗暴,复制即用)

1、拉取镜像 docker pull kartoza/geoserver:2.20.32、创建数据挂载目录 # 统一管理Docker容器的数据文件,geoserver mkdir -p /mydata/geoserver# 创建geoserver的挂载数据目录 mkdir -p /mydata/geoserver/data_dir# 创建geoserver的挂载数据目录&#xff0c;存放shp数据 m…

【数据库原理】(24)数据库安全性策略

数据库安全性是数据库管理系统&#xff08;DBMS&#xff09;中一个至关重要的方面。它指的是保护数据库免受非授权访问和恶意操作&#xff0c;包括数据泄露、修改、破坏等。 多层安全模型 在典型的计算机系统安全模型中&#xff0c;安全措施被设置在不同层级&#xff1a; 应用…

Unity ComputeShader 使用GPU快速计算复杂问题

Unity ComputeShader 使用GPU快速计算复杂问题 前言项目创建ComputeShader编写CompturShader创建Unity代码场景布置运行场景 参考 前言 遇到一个问题&#xff0c;需要大量的计算&#xff0c;在Unity中直接写会长时间的阻塞主线程&#xff0c;正好使用ComputeShader让GPU来帮我…

What is `@Controller` does?

Controller 是SpringMVC注解&#xff0c;标记一个类作为Web控制器&#xff08;Controller&#xff09;&#xff0c;负责处理HTTP请求并返回响应结果 在SpringMVC中&#xff0c;控制器类的主要职责是&#xff1a; 1、接收来自客户端的HTTP请求 2、调用服务层或其他业务逻辑组件…

海格里斯HEGERLS仓储货架生产厂家|载荷1.5T运行速度1.7~2m/s的智能四向穿梭车系统

四向穿梭车立体库是近年来出现的一种智能型密集系统&#xff0c;通过使用四向穿梭车在货架的水平和纵向轨道上运行来搬运货物&#xff0c;一台四向穿梭车就能完成货物的搬运工作&#xff0c;大大提高了工作效率。同时配合提升机、自动化仓库管理系统(WMS)和仓库调度系统(WCS)&a…

使用WAF防御网络上的隐蔽威胁之SSRF攻击

服务器端请求伪造&#xff08;SSRF&#xff09;攻击是一种常见的网络安全威胁&#xff0c;它允许攻击者诱使服务器执行恶意请求。与跨站请求伪造&#xff08;CSRF&#xff09;相比&#xff0c;SSRF攻击针对的是服务器而不是用户。了解SSRF攻击的工作原理、如何防御它&#xff0…

手写OpenFeign(简易版)

Remoting组件实现 1. 前言2. 原理说明3. 远程调用组件实现---自定义注解3.1 添加Spring依赖3.2 编写EnableRemoting注解3.3 编写RemoteClient注解3.4 编写GetMapping注解 4. 远程调用组件实现---生成代理类4.1 编写自定义BeanDefinition注册器4.2 编写自定义包扫描器4.3 编写Fa…

JVM初识

什么是JVM&#xff1f; JVM全称是Java Virtual Machine&#xff0c;中文译名Java虚拟机。 JVM本质上是一个运行在计算机上的程序&#xff0c;他的职责是运行Java字节码文件。 JVM的功能 jvm的功能主要分为三部分&#xff1a; 解释和运行 对字节码文件中的指令&#xff0c;实…

vue实现导出+ 样式修改

1.安装插件 npm xlsx-style ^0.18.5 npm install xlsx -S ^0.8.13 2. 修改代码 node_modules里面找到 以下位置xlsx.js 搜索 write_ws_xml_data 替换成以下代码 function write_ws_xml_data(ws, opts, idx, wb) {var o [], r [], range safe_decode_range(ws[!ref]…

搭建储能监控云平台:实现能源管理的智能化

搭建储能监控云平台&#xff1a;实现能源管理的智能化 在全球能源变革的大背景下&#xff0c;储能技术的重要性日益凸显。储能监控云平台作为能源管理的智能解决方案&#xff0c;可以为企业提供全方位的储能系统监控与数据分析&#xff0c;提高能源利用率&#xff0c;降低能源成…

QFN封装对国产双轴半自动划片机的性能有哪些要求?

1. 高精度切割&#xff1a;QFN封装要求芯片的尺寸和形状误差要尽可能小&#xff0c;因此对国产双轴半自动划片机的切割精度提出了高要求。高精度的切割能够提高封装的良品率和稳定性。 2. 快速和稳定&#xff1a;QFN封装生产需要快速、稳定的生产过程&#xff0c;因此对国产双轴…

Jenkins 插件下载速度慢、安装失败了!我教你怎么解决!

Jenkins部署完毕&#xff0c;如果不安装插件的话&#xff0c;那它就是一个光杆司令&#xff0c;啥事也做不了&#xff01; 所以首先要登陆管理员账号然后点击系统管理再点击右边的插件管理安装CI/CD必要插件。 但是问题来了&#xff0c;jenkins下载插件速度非常慢&#xff0c…

前端密钥怎么存储,以及临时存储一些数据,如何存储才最安全?

前端密钥存储安全的方案&#xff1a; 1、使用浏览器提供的本地存储&#xff1a;现代浏览器提供了本地存储机制&#xff0c;例如 Web Storage&#xff08;localStorage 和 sessionStorage&#xff09;或 IndexedDB。可以将密钥存储在这些本地存储中&#xff0c;并使用浏览器提供…

XTuner 大模型单卡低成本微调实战

XTuner 大模型单卡低成本微调实战 Finetune简介增量预训练微调指令跟随微调LoRA XTuner介绍功能亮点 8GB显存玩转LLMFlash AttentionDeepSpeed ZeRO 上手操作平台激活环境微调 参考教程&#xff1a;XTuner Finetune简介 LLM的下游应用任务中&#xff0c;增量预训练和指令跟随…