openssl3.2 - exp - class warp for sha3-512

2024-04-11 15:12
文章标签 class 512 exp sha3 warp openssl3.2

本文主要是介绍openssl3.2 - exp - class warp for sha3-512,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • openssl3.2 - exp - class warp for sha3-512
    • 概述
    • 笔记
    • 调用方代码
    • 子类 - cipher_sha3_512.h
    • 子类 - cipher_sha3_512.cpp
    • 基类 - cipher_md_base.h
    • 基类 - cipher_md_base.cpp
    • 备注
    • END

openssl3.2 - exp - class warp for sha3-512

概述

前面实验整了一个对buffer进行sha3-512算hash的函数。
实际用的时候,要对几个buffer连续做hash. 前面封装的统一做hash的函数不适用。
重新封装了一个类,只要在类的生命周期中,就可以随时对连续的buffer(可能不在一个函数中)做hash的update.

笔记

调用方代码

bool CPeFileCheck::calc_hash_SHA3_512()
{bool b_rc = false;CCipherSha3_512 hash;do {if (!hash.begin()){break;}// 如果buffer不在一个函数中,将hash的指针传给那些函数做hash.updateif (!hash.update(this->m_pu8_map_file, this->m_u32_file_size)){break;}if (!hash.end()){break;}m_digest_length = hash.get_md_len();if (m_digest_length > sizeof(m_ary_digest_value)){break;}memcpy(m_ary_digest_value, hash.get_md(), hash.get_md_len());b_rc = true;} while (false);return b_rc;
}

子类 - cipher_sha3_512.h

//! \file cipher_sha3_512.h#ifndef __CIPHER_SHA3_512_H__
#define __CIPHER_SHA3_512_H__#include "cipher_md_base.h"class CCipherSha3_512 : public CCipherMdBase
{
public:CCipherSha3_512();virtual ~CCipherSha3_512();
};#endif // #ifndef __CIPHER_SHA3_512_H__

子类 - cipher_sha3_512.cpp

//! \file cipher_sha3_512.cpp#include "pch.h"
#include "cipher_sha3_512.h"CCipherSha3_512::CCipherSha3_512()
{setCiphherName("SHA3-512");
}CCipherSha3_512::~CCipherSha3_512()
{
}

基类 - cipher_md_base.h

//! \file cipher_md_base.h#ifndef __CIPHER_MD_BASE_H__
#define __CIPHER_MD_BASE_H__#include <openssl/evp.h>
#include <string>//! \ref https://blog.csdn.net/LostSpeed/article/details/135581192
class CCipherMdBase
{
public:CCipherMdBase();virtual ~CCipherMdBase();bool begin();bool update(uint8_t* pBuf, int lenBuf);bool end();unsigned int get_md_len() { return m_digest_length; }uint8_t* get_md() { return m_p_digest_value; }void setCiphherName(const char* pszNmae) { m_strCipherName = ((NULL != pszNmae) ? pszNmae : ""); }private:void clear();private:std::string m_strCipherName;OSSL_LIB_CTX* m_ossl_lib_ctx;EVP_MD* m_evp_md;EVP_MD_CTX* m_evp_md_ctx;unsigned int m_digest_length;uint8_t* m_p_digest_value;
};#endif // #ifndef __CIPHER_MD_BASE_H__

基类 - cipher_md_base.cpp

//! \file cipher_md_base.cpp#include "pch.h"
#include "cipher_md_base.h"CCipherMdBase::CCipherMdBase(): m_ossl_lib_ctx(NULL),m_evp_md(NULL),m_evp_md_ctx(NULL),m_digest_length(0),m_p_digest_value(NULL)
{}CCipherMdBase::~CCipherMdBase()
{clear();
}void CCipherMdBase::clear()
{if (NULL != m_evp_md_ctx){EVP_MD_CTX_free(m_evp_md_ctx);m_evp_md_ctx = NULL;}m_digest_length = 0;if (NULL != m_p_digest_value){OPENSSL_free(m_p_digest_value);m_p_digest_value = NULL;}if (NULL != m_evp_md){EVP_MD_free(m_evp_md);m_evp_md = NULL;}if (NULL != m_ossl_lib_ctx){OSSL_LIB_CTX_free(m_ossl_lib_ctx);m_ossl_lib_ctx = NULL;}
}bool CCipherMdBase::begin()
{bool b_rc = false;const char* _psz_option_properties = NULL;do {clear();m_ossl_lib_ctx = OSSL_LIB_CTX_new();if (NULL == m_ossl_lib_ctx) {// fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");break;}/** Fetch a message digest by name* The algorithm name is case insensitive.* See providers(7) for details about algorithm fetching*/if (m_strCipherName.empty()){break;}m_evp_md = EVP_MD_fetch(m_ossl_lib_ctx, m_strCipherName.data(), _psz_option_properties);if (NULL == m_evp_md) {// fprintf(stderr, "EVP_MD_fetch could not find SHA3-512.");break;}/** Make a message digest context to hold temporary state* during digest creation*/m_evp_md_ctx = EVP_MD_CTX_new();if (NULL == m_evp_md_ctx) {// fprintf(stderr, "EVP_MD_CTX_new failed.\n");break;}/** Initialize the message digest context to use the fetched* digest provider*/if (EVP_DigestInit(m_evp_md_ctx, m_evp_md) != 1) {// fprintf(stderr, "EVP_DigestInit failed.\n");break;}b_rc = true;} while (false);return b_rc;
}bool CCipherMdBase::update(uint8_t* pBuf, int lenBuf)
{bool b_rc = false;do {if (EVP_DigestUpdate(m_evp_md_ctx, pBuf, lenBuf) != 1) {// fprintf(stderr, "EVP_DigestUpdate(hamlet_1) failed.\n");break;}b_rc = true;} while (false);return b_rc;}bool CCipherMdBase::end()
{bool b_rc = false;int digest_length = 0;do {/* Determine the length of the fetched digest type */m_digest_length = EVP_MD_get_size(m_evp_md);if (m_digest_length <= 0) {// fprintf(stderr, "EVP_MD_get_size returned invalid size.\n");break;}m_p_digest_value = (uint8_t*)OPENSSL_malloc(m_digest_length);if (NULL == m_p_digest_value) {// fprintf(stderr, "No memory.\n");break;}if (EVP_DigestFinal(m_evp_md_ctx, m_p_digest_value, &m_digest_length) != 1) {// fprintf(stderr, "EVP_DigestFinal() failed.\n");break;}b_rc = true;} while (false);return b_rc;
}

备注

这个封装类用的很舒服。
如果要做其他种类的hash, 只需要继承一个子类,给定新的hash算法名称就行。

END

这篇关于openssl3.2 - exp - class warp for sha3-512的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

Ubuntu系统怎么安装Warp? 新一代AI 终端神器安装使用方法

《Ubuntu系统怎么安装Warp?新一代AI终端神器安装使用方法》Warp是一款使用Rust开发的现代化AI终端工具,该怎么再Ubuntu系统中安装使用呢?下面我们就来看看详细教程... Warp Terminal 是一款使用 Rust 开发的现代化「AI 终端」工具。最初它只支持 MACOS,但在 20

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

提示:Decompiled.class file,bytecode version如何解决

《提示:Decompiled.classfile,bytecodeversion如何解决》在处理Decompiled.classfile和bytecodeversion问题时,通过修改Maven配... 目录问题原因总结问题1、提示:Decompiled .class file,China编程 bytecode

Thread如何划分为Warp?

1 .Thread如何划分为Warp? https://jielahou.com/code/cuda/thread-to-warp.html  Thread Index和Thread ID之间有什么关系呢?(线程架构参考这里:CUDA C++ Programming Guide (nvidia.com)open in new window) 1维的Thread Index,其Thread

类型信息:反射-Class

在说反射前提一个概念:RTTI(在运行时,识别一个对象的类型) public class Shapes {public static void main(String[] args) {List<Shape> shapes = Arrays.asList(new Circle(), new Square(), new Triangle());for (Shape shape : shapes

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

linux 内核提权总结(demo+exp分析) -- 任意读写(四)

hijack_modprobe_path篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm     原理同hijack_prctl, 当用户执行错误格式的elf文件时内核调用call_usermod

linux 内核提权总结(demo+exp分析) -- 任意读写(三)

hijack_prctl篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm   prctl函数: 用户态函数,可用于定制进程参数,非常适合和内核进行交互 用户态执行prctl函数后触发prctl系统

linux 内核提权总结(demo+exp分析) -- 任意读写(二)

hijack_vdso篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm     vdso: 内核实现的一个动态库,存在于内核,然后映射到用户态空间,可由用户态直接调用 内核中的vdso如果被修改