dimanche 13 mai 2018

Manipulating Hex Nibles

I need to be able to remove the first two characters of a string in C++ and then remove the first half (nibble) of the third character.

unsigned char hash[40];

The string is coming from the output of SHA1 function.

SHA1((unsigned char*)ibuf.c_str(), ibuf.size(), hash);

I need to be able to remove the first 2 characters and the first nibble of the third character (let's say on a server end) then send the resulting string (including the 2nd nibble of the third character) to another function (client) which then is going to try and figure out what the first 2 characters was and the first nibble of the third character. I convert the unsigned char hash to a string:

std::string puzzle = hexStr((unsigned char*)hash, 20);
//  hexStr is a function from https://codereview.stackexchange.com/questions/78535/converting-array-of-bytes-to-the-hex-string-representation 
// to convert to hex string representations.

The reason I need the whole string is, puzzle was used to in another hash function, and the challenge is reconstructing this original string.

SHA1((unsigned char*)puzzle.c_str(), puzzle.size(), hash2);

So my questions are:

  1. How do I remove the first nibble of the third character.
  2. How do I send the resulting string (which includes the second nibble of the third character).
  3. How do I then manipulate the resulting string to add/come up with the first nibble of the third character to reconstruct that third character.

Aucun commentaire:

Enregistrer un commentaire