here is what I want to do: I want to read 104-character strings from a text file, one per line. I want to chop those strings in two, the first 32 characters being a string called schecksum and the last 72 characters being a string called sencdata. Then I want to convert these strings to binary using the getHash function, and store them in vectors (ts_checksum and enc_data, respectively).
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned char uint8_t;
size_t getHash(char szHash[], unsigned char hash[])
{
unsigned int number;
size_t nHashLen;
if(((nHashLen = strlen(szHash)) % 2) != 0)
return(0);
for(size_t i = 0;i < nHashLen;i++) {
if(isxdigit(szHash[i]) == 0)
return(0);
}
nHashLen /= 2;
for(size_t i = 0;i < nHashLen;i++) {
sscanf(&szHash[i*2],"%2x",&number);
hash[i] = (unsigned char)number;
}
return(nHashLen);
}
void dump(uint8_t *digest)
{
for(int i = 0;i < 16;i++)
printf("%02X",digest[i]);
}
int main (int argc, char *argv[])
{
vector<uint8_t*> vec_ts_checksum;
vector<uint8_t*> vec_enc_data;
//open the hash list...
ifstream hash_list(argv[4]);
//checksum of timestamp
uint8_t ts_checksum[16];
//encrypted timestamp
uint8_t enc_data[36];
char schecksum[]="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
char sencdata[]="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
char shash[105];
while(!hash_list.eof())
{
hash_list.getline(shash,105);//get each line of hash file
cout << shash << " loaded\n\n";
if(strlen(shash)!=104)
{
cout << "encrypted timestamp is a 104-character hex string\n";
exit(1);
}
//chop string into two different strings...
for(int i=0;i<32;i++)
schecksum[i]=shash[i];
for(int i=0;i<72;i++)
sencdata[i]=shash[i+32];
//convert strings to binary and store in vector...
getHash(schecksum,ts_checksum);
cout << "ts_checksum= ";
dump(ts_checksum);
cout << "\n";
vec_ts_checksum.push_back(ts_checksum);
getHash(sencdata,enc_data);
cout << "enc_data= ";
dump(enc_data);
cout << "\n";
vec_enc_data.push_back(enc_data);
cout << "\nvec_ts_checksum[0] = \n";
dump(vec_ts_checksum[0]);
cout << "\n\n";
}
Everything seems to work alright except for storing the binary in the vectors. When I look at the vector, all the elements are the same! And they are all equal to the last element that was inserted into the vector!
for example, the text file contains 3 104-character strings a, b, and c. The vector ends up being filled with c, c, c rather than a, b, c. I want vector[0] to be a, vector[1] to be b, and vector[2] to be c. But vector[0] is c, vector[1] is c, and vector[2] is c! I find this to be very frustrating.
What am I doing wrong? I have tried everything! Please help. Thanks!