Generating new value at every execution (when it shouldn't

Jul 28, 2011 at 3:50pm
Hi again,

So I've ran into another snag with my message digest code. It generates new, very slightly different values every time it's ran with the same, unaltered message. The message is 128 bytes of Lorem Ipsum text in a raw text file. Every time I run it, the last few bytes of the generated checksum is slightly different than the last. Here's the code:

(I work out complex stuff in main() and then slice it up into individual functions)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int main (int argc, const char *argv[])
{
	unsigned char checksum[128] = {0};
	const char *fName = "text.txt";
	unsigned int index = 0;
	cMedal medal;
	int fLength = medal.FileSize(fName);
	char *buffer = new char[fLength];
	int amountRead = 0;
	cout<<"--SIZE OF FILE:: "<<fLength<<"\n";

	ifstream file("text.txt", ios::binary);

	//Stash the contents of our file into our dynamically allocated buffer
	while(file>>buffer[amountRead] && amountRead < fLength)
	{
		amountRead++;
	}

	int i;
	//Process data while the index counter is less than the length of the file. 
	while(index <= fLength)
	{
		for(i=0;i<128;i++)
		{
			//First round is 0+128, second is 0+256, third is 0+384 and so on. 
			checksum[i] += medal.AA(buffer[(i+index)], 0xe0, 0xb8, 0x77);
			checksum[i] += medal.AA(buffer[(i+index)], 0x2f, 0x1b, 0x1f);
			checksum[i] += medal.AA(buffer[(i+index)], 0x88, 0xdd, 0x36);
			checksum[i] += medal.AA(buffer[(i+index)], 0x25, 0x90, 0x8f);
		};

		//Increment the index for the buffer
		index+=127;
		cout<<"\n"<<(i+index);
	};

	cout<<"\n\n\n";

	//Print the checksum
	for(int i=0;i<128;i++)
	{
		printf("%02X", checksum[i]);
	}
	return 0;
}


The A() function (AA just applies it to a block of data using a reference)
1
2
3
4
inline unsigned char cMedal::A(unsigned char w, unsigned char x, unsigned char y, unsigned char z)
{
	return w&x | ~w&y | w&z | ~w&z;
}


Now, I'm aware that I need several more rounds and more functions like A and AA, but this is just an experiment testing things out one small piece at a time. I've tried it using 5 rounds just to check to see if it's the fact that I'm only using one but it makes no difference, there is still that slight variation.

Thanks in advance.
Jul 28, 2011 at 4:22pm
Is

index+=127;

what you really need to use?

This doesn't agree with "First round is 0+128, second is 0+256, third is 0+384 and so on."
Jul 28, 2011 at 5:12pm
It's supposed to work something like this:

1. It's initialized with the value 0, so first round is 0.
2. It goes through the first for-loop as 0 and as such i+index equals 0 to 128.
3. The for-loop finishes and goes to the next part of the while-loop, increasing it by 127 (it was 128 but I thought maybe 127 was a better idea, it's not lol).
4. if it isn't equal to fLength yet then we go back to step 2, except it's now 128+0 and so on until it's 128+128 and exits the for loop.

This should repeat until index is the same as fLength. It's a rather choppy-hacky way of processing information in blocks by using an index counter. Because a checksum is always a fixed length and the buffer is a variable length depending on the size of the file, string or whatever else. I'll admit that it is far, far from perfect and it is really meant more as an experiment and getting my feet wet in basic cryptography.
Jul 28, 2011 at 9:23pm
I don't understand your file reading code.

1
2
3
4
	while(file>>buffer[amountRead] && amountRead < fLength)
	{
		amountRead++;
	}


Is there a reason you need to ignore spaces?

Or could you use just

file.read(buffer, fLength);
Last edited on Jul 28, 2011 at 9:23pm
Jul 29, 2011 at 12:26am
You mean the whitespace or the actual space character 0x20?

If you're referring to whitespace - Visual Studio sort of does it's own thing really. If you're referring to spaces, then please do explain.
Topic archived. No new replies allowed.