std::ifstream read file as BYTE (unsigned char) not char

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm using Microsoft's CNG Cryptography API's to compute the hash of a file, so far all works fine except that computed hash is wrong comparing to hash computed with external 3rd party program.

I'm not 100% sure but I think the problem is that I read the file into signed char array instead of into BYTE (unsigned char) array.

Bellow code is how I read the file now: (note: I omitted error checking code)

std::ifstream file;
file.open("sample.txt", std::ifstream::binary | std::ifstream::in | std::ifstream::ate);
const int file_length = file.tellg();

char* data = new char[file_length];
file.seekg(file.beg);
file.read(data, file_length);

Problem with above code is that data is read into signed char array but crypto functions expect unsigned char / BYTE and I'm guessing this is why my computed hash is wrong.

Bellow code is what I would like to do but it doesn't work (added comment) since ifstream::read()method expects char array not unsigned char / BYTE

std::ifstream file;
file.open("sample.txt", std::ifstream::binary | std::ifstream::in | std::ifstream::ate);
const int file_length = file.tellg();

PBYTE data = new BYTE[file_length];
file.seekg(file.beg);
file.read(data, file_length); // error PBYTE is incompatibe with char*

So my question is: how do I read data as BYTE not char do I need to use CreateFile API or is there a way with std::ifstream?

Maybe there is something else causing bad computed hash, I don't know if so tell me.

EDIT: Below is fully working code to compute the SHA256 hash for given file name, but the hash is bad. (ie. not the same as if done with 3rd party hash utility)

#include 
#include 
#include 
#include 
#include 
#include 


#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)

std::string ByteToHex(PBYTE data, size_t len)
{
    std::stringstream ss;
    ss << std::hex << std::setfill('0') << std::uppercase;

    for (size_t i = 0; i < len; 
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

When calling read or write, that's one of the few situations where it's considered okay to use reinterpret_cast. In some cases there's no other viable solution.

So for your case:

file.read(reinterpret_cast<char*>(data), file_length);

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.