openssl3.2 - 官方demo学习 - digest - EVP_MD_xof.c

2024-01-14 12:28

本文主要是介绍openssl3.2 - 官方demo学习 - digest - EVP_MD_xof.c,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • openssl3.2 - 官方demo学习 - digest - EVP_MD_xof.c
    • 概述
    • 笔记
    • END

openssl3.2 - 官方demo学习 - digest - EVP_MD_xof.c

概述

使用支持XOF方式的摘要算法(e.g. SHAKE256), 对buffer进行摘要, 并和预留的摘要值进行比对

笔记

/*!
\file EVP_MD_xof.c
\note openssl3.2 - 官方demo学习 - digest - EVP_MD_xof.c
使用支持XOF方式的摘要算法(e.g. SHAKE256), 对buffer进行摘要, 并和预留的摘要值进行比对
*//*-* 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*/#include <stdio.h>
#include <string.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/core_names.h>#include "my_openSSL_lib.h"/** Example of using an extendable-output hash function (XOF). A XOF is a hash* function with configurable output length and which can generate an* arbitrarily large output.** This example uses SHAKE256, an extendable output variant of SHA3 (Keccak).** To generate different output lengths, you can pass a single integer argument* on the command line, which is the output size in bytes. By default, a 20-byte* output is generated and (for this length only) a known answer test is* performed.*//* Our input to the XOF hash function. */
const char message[] = "This is a test message.";/* Expected output when an output length of 20 bytes is used. */
static const char known_answer[] = {0x52, 0x97, 0x93, 0x78, 0x27, 0x58, 0x7d, 0x62,0x8b, 0x00, 0x25, 0xb5, 0xec, 0x39, 0x5e, 0x2d,0x7f, 0x3e, 0xd4, 0x19
};/** A property query used for selecting the SHAKE256 implementation.*/
static const char *propq = NULL;int main(int argc, char **argv)
{int ret = EXIT_FAILURE;OSSL_LIB_CTX *_ossl_lib_ctx = NULL;EVP_MD *_evp_md = NULL;EVP_MD_CTX *_evp_md_ctx = NULL;unsigned int digest_len = 20;int digest_len_i;unsigned char *_psz_digest = NULL;/* Allow digest length to be changed for demonstration purposes. */if (argc > 1) {digest_len_i = atoi(argv[1]);if (digest_len_i <= 0) {fprintf(stderr, "Specify a non-negative digest length\n");goto end;}digest_len = (unsigned int)digest_len_i;}/** Retrieve desired algorithm. This must be a hash algorithm which supports* XOF.*/_evp_md = EVP_MD_fetch(_ossl_lib_ctx, "SHAKE256", propq);if (_evp_md == NULL) {fprintf(stderr, "Failed to retrieve SHAKE256 algorithm\n");goto end;}/* Create context. */_evp_md_ctx = EVP_MD_CTX_new();if (_evp_md_ctx == NULL) {fprintf(stderr, "Failed to create digest context\n");goto end;}/* Initialize digest context. */if (EVP_DigestInit(_evp_md_ctx, _evp_md) == 0) {fprintf(stderr, "Failed to initialize digest\n");goto end;}/** Feed our message into the digest function.* This may be called multiple times.*/if (EVP_DigestUpdate(_evp_md_ctx, message, sizeof(message)) == 0) {fprintf(stderr, "Failed to hash input message\n");goto end;}/* Allocate enough memory for our digest length. */_psz_digest = OPENSSL_malloc(digest_len);if (_psz_digest == NULL) {fprintf(stderr, "Failed to allocate memory for digest\n");goto end;}/* Get computed digest. The digest will be of whatever length we specify. */if (EVP_DigestFinalXOF(_evp_md_ctx, _psz_digest, digest_len) == 0) {fprintf(stderr, "Failed to finalize hash\n");goto end;}printf("Output digest:\n");BIO_dump_indent_fp(stdout, _psz_digest, digest_len, 2); /*!< 参数4是dump的宽度, 默认是16, 不用填写, 或者给0都行 *//* If digest length is 20 bytes, check it matches our known answer. */if (digest_len == 20) {/** Always use a constant-time function such as CRYPTO_memcmp* when comparing cryptographic values. Do not use memcmp(3).*/if (CRYPTO_memcmp(_psz_digest, known_answer, sizeof(known_answer)) != 0) {fprintf(stderr, "Output does not match expected result\n");goto end;}}ret = EXIT_SUCCESS;
end:OPENSSL_free(_psz_digest);EVP_MD_CTX_free(_evp_md_ctx);EVP_MD_free(_evp_md);OSSL_LIB_CTX_free(_ossl_lib_ctx);return ret;
}

END

这篇关于openssl3.2 - 官方demo学习 - digest - EVP_MD_xof.c的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/605142

相关文章

springboot依靠security实现digest认证的实践

《springboot依靠security实现digest认证的实践》HTTP摘要认证通过加密参数(如nonce、response)验证身份,避免明文传输,但存在密码存储风险,相比基本认证更安全,却因... 目录概述参数Demopom.XML依赖Digest1Application.JavaMyPasswo

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

ubuntu系统使用官方操作命令升级Dify指南

《ubuntu系统使用官方操作命令升级Dify指南》Dify支持自动化执行、日志记录和结果管理,适用于数据处理、模型训练和部署等场景,今天我们就来看看ubuntu系统中使用官方操作命令升级Dify的方... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx