当前位置:网站首页>Calculate sha256 value of data or file based on crypto++
Calculate sha256 value of data or file based on crypto++
2022-07-06 00:18:00 【newlw】
background
Wrote a base Crypto++ Encryption library to achieve the calculation of files and data SHA256 Value of a small program ,Crypto++ The encryption library will not be introduced in detail , This library provides many well-known encryption and decryption algorithms , Just call it directly , It's easy to use .
Write this article , Is to share their learning experience . My knowledge of cryptography is not very good , Also good have Crypto++ Open source libraries can be used , It makes up for the lack of encryption and decryption . Now? , Share with you Crypto++ Medium SHA256 The module realizes file and data SHA256 Value calculation method .
Precautions for program compilation settings
First , First download Crypto++ Open source code for Libraries , then , Compile by yourself Crypto++ Library file .
In the import Crypto++ Library files to their own engineering projects , You should compile and set your own project . The main point is : In the properties of the project “ Runtime ” Set up , To compile with Crypto++ Library file of “ Runtime ” The options should be consistent , Otherwise, the program will fail to compile . That is to check LIB Library project and this test project : attribute --> C/C++ --> Code generation --> Runtime Is it unified .
If there is a compilation error , The report XX Repeated definition and other errors , Again , To check LIB Library project and this test project : attribute --> C/C++ --> Code generation --> Runtime Is it unified .
Realization principle
Calculate file and data SHA256 value , Their implementation core code is :
FileSource(pszFileName, true, new HashFilter(sha256, new HexEncoder(new StringSink(value))));
StringSource(pData, dwDataSize, true, new HashFilter(sha256, new HexEncoder(new StringSink(value))));
These two lines of code take a total of 4 Classes StringSink、HexEncoder、HashFilter、FileSource and StringSource. and , The main difference between these two lines of code is FileSource and StringSource The difference between .FileSource The first parameter of only needs to pass in the file path name , and StringSource The first and second parameters respectively represent the first address pointer and data size .
The meaning of other parameters is the same , First use class StringSink Add to a string Object buffer , Then use class HexEncoder Convert this buffer to 16 Base number . among , Calculation Hash Values are mainly used in classes HashFilter.FileSource Class is to calculate Hash Value file filename Make a certain conversion and put it into the temporary buffer , Then call the instantiated HashFilter Calculate it accordingly Hash Functional Hash value , And put Hash The value is returned to the buffer . and StringSource Class is to calculate Hash Value data is passed directly to HashFilter, Then call the instantiated HashFilter Calculate it accordingly Hash Functional Hash value , And put Hash The value is returned to the buffer .
coded
Calculate the SHA256
// Calculate the SHA256 value
string CalSHA256_ByFile(char *pszFileName)
{
string value;
SHA256 sha256;
FileSource(pszFileName, true, new HashFilter(sha256, new HexEncoder(new StringSink(value))));
return value;
}
Calculating data SHA256
// Calculating data SHA256 value
string CalSHA256_ByMem(PBYTE pData, DWORD dwDataSize)
{
string value;
SHA256 sha256;
StringSource(pData, dwDataSize, true, new HashFilter(sha256, new HexEncoder(new StringSink(value))));
return value;
}
Program testing
We run the program , Use file and data to calculate 520.exe Of SHA256 value , The result is that both values are the same :
summary
This program is not very complicated , It's easy to use . In the process of using , Pay attention to the difference between file calculation and data calculation .
Reference resources
Reference from 《Windows Detailed explanation of hacker programming technology 》 A Book
http://www.biyezuopin.vip
边栏推荐
- N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
- 软件测试工程师必会的银行存款业务,你了解多少?
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
- MySql——CRUD
- MySQL存储引擎
- [Luogu p3295] mengmengda (parallel search) (double)
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- Huawei equipment configuration ospf-bgp linkage
- Problems encountered in the database
- 数据库遇到的问题
猜你喜欢
Learn PWN from CTF wiki - ret2libc1
电机的简介
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
软件测试工程师必会的银行存款业务,你了解多少?
Ffmpeg learning - core module
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
wx.getLocation(Object object)申请方法,最新版
Doppler effect (Doppler shift)
[binary search tree] add, delete, modify and query function code implementation
Configuring OSPF GR features for Huawei devices
随机推荐
传输层协议------UDP协议
Gd32f4xx UIP protocol stack migration record
mysql-全局锁和表锁
Solve the problem of reading Chinese garbled code in sqlserver connection database
硬件及接口学习总结
Yunna | what are the main operating processes of the fixed assets management system
QT--线程
SQLServer连接数据库读取中文乱码问题解决
shardingsphere源码解析
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
LeetCode 6006. Take out the least number of magic beans
Classical concurrency problem: the dining problem of philosophers
FFT 学习笔记(自认为详细)
第16章 OAuth2AuthorizationRequestRedirectWebFilter源码解析
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
Doppler effect (Doppler shift)
【QT】Qt使用QJson生成json文件并保存
Browser local storage
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)