当前位置:网站首页>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
边栏推荐
- JS can really prohibit constant modification this time!
- Configuring OSPF GR features for Huawei devices
- [Luogu p3295] mengmengda (parallel search) (double)
- Transport layer protocol ----- UDP protocol
- Go learning --- structure to map[string]interface{}
- The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
- AtCoder Beginner Contest 254【VP记录】
- Zhuan: in the future, such an organization can withstand the risks
- Huawei equipment is configured with OSPF and BFD linkage
- OS i/o devices and device controllers
猜你喜欢

Transport layer protocol ----- UDP protocol

Senparc. Weixin. Sample. MP source code analysis

After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!

Effet Doppler (déplacement de fréquence Doppler)

Hudi of data Lake (2): Hudi compilation

MySQL之函数

Knowledge about the memory size occupied by the structure

Room cannot create an SQLite connection to verify the queries

Extracting profile data from profile measurement

NSSA area where OSPF is configured for Huawei equipment
随机推荐
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
mysql-全局锁和表锁
[designmode] composite mode
电机的简介
Cloudcompare & PCL point cloud randomly adds noise
MySql——CRUD
About the slmgr command
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
微信小程序---WXML 模板语法(附带笔记文档)
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
The difference of time zone and the time library of go language
QT -- thread
Solve the problem of reading Chinese garbled code in sqlserver connection database
LeetCode 1189. Maximum number of "balloons"
What are Yunna's fixed asset management systems?
多普勒效應(多普勒頻移)
Search (DFS and BFS)
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
【QT】Qt使用QJson生成json文件并保存
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share