2011-12-13, 18:15
In developing some supplemental tools for XBMC, I found I had to hash strings often when dealing with the Thumbnails directory.
XBMC provides a good example in the Wiki. But if you are developing in a different language, you need to know that the hashes you are generating are the same as what XBMC would generate.
For anyone who needs to quickly test hashes against XBMC's C# hashing function, use this quick tester I compiled. It provides the exact hash from XBMC's code.
Download it here
Usage: either provide the strings to be hashed as command-line parameters, or enter them line-by-line once the program starts
Requirements: .NET Framework 3.5
Here is the source code for the hashing function at the time this program was created:
XBMC provides a good example in the Wiki. But if you are developing in a different language, you need to know that the hashes you are generating are the same as what XBMC would generate.
For anyone who needs to quickly test hashes against XBMC's C# hashing function, use this quick tester I compiled. It provides the exact hash from XBMC's code.
Download it here
Usage: either provide the strings to be hashed as command-line parameters, or enter them line-by-line once the program starts
Requirements: .NET Framework 3.5
Here is the source code for the hashing function at the time this program was created:
Code:
public static string Hash(string input)
{
char[] chars = input.ToCharArray();
for (int index = 0; index < chars.Length; index++)
{
if (chars[index] <= 127)
{
chars[index] = System.Char.ToLowerInvariant(chars[index]);
}
}
input = new string(chars);
uint m_crc = 0xffffffff;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
foreach (byte myByte in bytes)
{
m_crc ^= ((uint)(myByte) << 24);
for (int i = 0; i < 8; i++)
{
if ((System.Convert.ToUInt32(m_crc) & 0x80000000) == 0x80000000)
{
m_crc = (m_crc << 1) ^ 0x04C11DB7;
}
else
{
m_crc <<= 1;
}
}
}
return String.Format("{0:x8}", m_crc);
}