Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
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.
Turn Your Knowledge into Earnings.
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.
signed char
BYTE
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.
unsigned char / BYTE
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
ifstream::read()
char
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?
CreateFile
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; REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
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.
read
write
reinterpret_cast
So for your case:
file.read(reinterpret_cast<char*>(data), file_length);
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.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.