openssl3.2 - 官方demo学习 - kdf - pbkdf2.c

2024-01-15 15:20

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

文章目录

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

openssl3.2 - 官方demo学习 - kdf - pbkdf2.c

概述

设置PBKDF2算法的参数, 取key

笔记

/*!
\file pbkdf2.c
\note 
openssl3.2 - 官方demo学习 - kdf - pbkdf2.c
设置PBKDF2算法的参数, 取key
*//** Copyright 2021-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 <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/kdf.h>
#include <openssl/obj_mac.h>
#include <openssl/params.h>#include "my_openSSL_lib.h"/** test vector from* https://datatracker.ietf.org/doc/html/rfc7914*//** Hard coding a password into an application is very bad.* It is done here solely for educational purposes.*/
static unsigned char password[] = {'P', 'a', 's', 's', 'w', 'o', 'r', 'd'
};/** The salt is better not being hard coded too.  Each password should have a* different salt if possible.  The salt is not considered secret information* and is safe to store with an encrypted password.*/
static unsigned char pbkdf2_salt[] = {'N', 'a', 'C', 'l'
};/** The iteration parameter can be variable or hard coded.  The disadvantage with* hard coding them is that they cannot easily be adjusted for future* technological improvements appear.*/
static unsigned int pbkdf2_iterations = 80000;static const unsigned char expected_output[] = {0x4d, 0xdc, 0xd8, 0xf6, 0x0b, 0x98, 0xbe, 0x21,0x83, 0x0c, 0xee, 0x5e, 0xf2, 0x27, 0x01, 0xf9,0x64, 0x1a, 0x44, 0x18, 0xd0, 0x4c, 0x04, 0x14,0xae, 0xff, 0x08, 0x87, 0x6b, 0x34, 0xab, 0x56,0xa1, 0xd4, 0x25, 0xa1, 0x22, 0x58, 0x33, 0x54,0x9a, 0xdb, 0x84, 0x1b, 0x51, 0xc9, 0xb3, 0x17,0x6a, 0x27, 0x2b, 0xde, 0xbb, 0xa1, 0xd0, 0x78,0x47, 0x8f, 0x62, 0xb3, 0x97, 0xf3, 0x3c, 0x8d
};int main(int argc, char **argv)
{int ret = EXIT_FAILURE;EVP_KDF *kdf = NULL;EVP_KDF_CTX *kctx = NULL;unsigned char out[64];OSSL_PARAM params[5], *p = params;OSSL_LIB_CTX *library_context = NULL;library_context = OSSL_LIB_CTX_new();if (library_context == NULL) {fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");goto end;}/* Fetch the key derivation function implementation */kdf = EVP_KDF_fetch(library_context, "PBKDF2", NULL);if (kdf == NULL) {fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");goto end;}/* Create a context for the key derivation operation */kctx = EVP_KDF_CTX_new(kdf);if (kctx == NULL) {fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");goto end;}/* Set password */*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,sizeof(password));/* Set salt */*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, pbkdf2_salt,sizeof(pbkdf2_salt));/* Set iteration count (default 2048) */*p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &pbkdf2_iterations);/* Set the underlying hash function used to derive the key */*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,"SHA256", 0);*p = OSSL_PARAM_construct_end();/* Derive the key */if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {fprintf(stderr, "EVP_KDF_derive() failed\n");goto end;}if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {fprintf(stderr, "Generated key does not match expected value\n");goto end;}ret = EXIT_SUCCESS;
end:EVP_KDF_CTX_free(kctx);EVP_KDF_free(kdf);OSSL_LIB_CTX_free(library_context);return ret;
}

END

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



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

相关文章

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

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert