当前位置:网站首页>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
边栏推荐
- Tools to improve work efficiency: the idea of SQL batch generation tools
- Zhuan: in the future, such an organization can withstand the risks
- Upgrade openssl-1.1.1p for openssl-1.0.2k
- Ffmpeg learning - core module
- Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
- 【luogu P3295】萌萌哒(并查集)(倍增)
- What are Yunna's fixed asset management systems?
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- Notepad++ regular expression replacement string
- Detailed explanation of APP functions of door-to-door appointment service
猜你喜欢
Huawei equipment is configured with OSPF and BFD linkage
Hudi of data Lake (2): Hudi compilation
云呐|固定资产管理系统主要操作流程有哪些
FFmpeg学习——核心模块
FFT learning notes (I think it is detailed)
Huawei equipment configuration ospf-bgp linkage
Effet Doppler (déplacement de fréquence Doppler)
STM32 configuration after chip replacement and possible errors
AtCoder Beginner Contest 254【VP记录】
[designmode] Decorator Pattern
随机推荐
LeetCode 1598. Folder operation log collector
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Upgrade openssl-1.1.1p for openssl-1.0.2k
MySql——CRUD
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
How much do you know about the bank deposit business that software test engineers must know?
Wechat applet -- wxml template syntax (with notes)
Effet Doppler (déplacement de fréquence Doppler)
Detailed explanation of APP functions of door-to-door appointment service
FFMPEG关键结构体——AVFrame
Go learning - dependency injection
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
MySQL存储引擎
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
云呐|固定资产管理系统功能包括哪些?
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
Cloudcompare & PCL point cloud randomly adds noise
Key structure of ffmpeg - avframe
Mathematical model Lotka Volterra