当前位置:网站首页>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 :

 Insert picture description here

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

原网站

版权声明
本文为[newlw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140233115978.html