当前位置:网站首页>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
边栏推荐
- STM32 configuration after chip replacement and possible errors
- Shardingsphere source code analysis
- LeetCode 1598. Folder operation log collector
- Senparc. Weixin. Sample. MP source code analysis
- Atcoder beginer contest 254 [VP record]
- 选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
- [Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
- 如何解决ecology9.0执行导入流程流程产生的问题
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
- Add noise randomly to open3d point cloud
猜你喜欢
电机的简介
Atcoder beginer contest 258 [competition record]
Senparc. Weixin. Sample. MP source code analysis
FFMPEG关键结构体——AVCodecContext
GD32F4xx uIP协议栈移植记录
Knowledge about the memory size occupied by the structure
About the slmgr command
提升工作效率工具:SQL批量生成工具思想
Search (DFS and BFS)
Upgrade openssl-1.1.1p for openssl-1.0.2k
随机推荐
About the slmgr command
【DesignMode】组合模式(composite mode)
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
[binary search tree] add, delete, modify and query function code implementation
Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
PV static creation and dynamic creation
LeetCode 斐波那契序列
[designmode] composite mode
wx. Getlocation (object object) application method, latest version
Miaochai Weekly - 8
[online chat] the original wechat applet can also reply to Facebook homepage messages!
FFT 学习笔记(自认为详细)
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Huawei equipment configuration ospf-bgp linkage
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
Notepad++ regular expression replacement string
权限问题:source .bash_profile permission denied
Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
Room cannot create an SQLite connection to verify the queries