I was given the task of documenting an application that has no comments. However, I have been unable to understand the following function.
private uint GetVersionHash(int encver, int realver)
{
int EncryptedVersionNumber = encver;
int VersionNumber = realver;
int VersionHash = 0;
int DecryptedVersionNumber = 0;
string VersionNumberStr;
int a = 0, b = 0, c = 0, d = 0, l = 0;
VersionNumberStr = VersionNumber.ToString();
l = VersionNumberStr.Length;
// I am specifically struggling with the purpose and intent of this loop.
for (int i = 0; i < l; i++)
{
VersionHash = (32 * VersionHash) + (int)VersionNumberStr[i] + ;
}
a = (VersionHash >> 24) & 0xFF;
b = (VersionHash >> 16) & 0xFF;
c = (VersionHash >> 8) & 0xFF;
d = VersionHash & 0xFF;
DecryptedVersionNumber = (0xff ^ a ^ b ^ c ^ d);
if (EncryptedVersionNumber == DecryptedVersionNumber)
{
return Convert.ToUInt32(VersionHash);
}
else
{
return 0;
}
}
With my current understanding it bitshift 5 right and adds some value.
Also some further information:
encverappears to be an encrypted version as an int (it is read from a file)realverseems to be version we are testing for match. It is looped in another function to this function fromshort.MinValuetoshort.MaxValue
What is the purpose of this loop? How does the code achieve this purpose?
The loop is calculating a ‘hash’ of the characters (digits) in the string representing the real version number. The value of VersionHash is a number which depends on every character in the string, the length of the string, and their order.