Sign in to follow this  
Followers 0
smashly

Help translating a hashing algorithm?

2 posts in this topic

Thank you for taking the time to read my post and or any feedback provided.

I found a function that hashes an input string.
But even simple bit operations always have me lost even in AutoIt native code.

Most parts of the algorithm function seem easy enough translate.

But the starting bit #define has me lost in how to translate it.

I gather the hashing function was used in asm (I'm sorta clueless as you can tell).
Most cases I can translate what I need from MSDN for native win api functions to AutoIt.

But I don't have an easy reference to compare or to translate the code below.

#define ROR(val, shift) ((val << (0x20 - shift)) | (val >> shift))
#define ROL(val, shift) ((val >> (0x20 - shift)) | (val << shift))
#define INIT_VAL 0xABCDEFAB

unsigned long hash32(char *str,unsigned long init)
{
    unsigned long i, m, len, c;
    unsigned long k[3];

    len = strlen(str);
    k[2] = k[1] = k[0] = init + len + 0xDEADBEEF;

    if ( len )
    {
        if ( len > 12 )
        {
            m = ((unsigned long long)(len - 13) * 0xAAAAAAAB  >> 35) + 1;

            while ( m-- )
            {
                for ( i = 0; i < 12; i++ )
                {
                    c = str[i];
                    if (c - 0x41 <= 0x19) c += 0x20;
                    k[(11 - i) / 4] += c << ((i % 4) * 8);
                }

                k[2] = (k[2] - k[0]) ^ ROL(k[0], 4);
                k[0] += k[1];
                k[1] = (k[1] - k[2]) ^ ROL(k[2], 6);
                k[2] += k[0];
                k[0] = (k[0] - k[1]) ^ ROL(k[1], 8);
                k[1] += k[2];
                k[2] = (k[2] - k[0]) ^ ROL(k[0], 16);
                k[0] += k[1];
                k[1] = (k[1] - k[2]) ^ ROR(k[2], 13);
                k[2] += k[0];
                k[0] = (k[0] - k[1]) ^ ROL(k[1], 4);
                k[1] += k[2];

                str += 12;
                len -= 12;
            }
        }
    
        for ( ; len > 0; len-- )
        {
            c = str[len - 1];
            if (c - 0x41 <= 0x19) c += 0x20;
            k[(11 - (len - 1)) / 4] += c << (( (len - 1) % 4) * 8);
        }

        k[0] = (k[0] ^ k[1]) - ROL(k[1], 14);
        k[2] = (k[2] ^ k[0]) - ROL(k[0], 11);
        k[1] = (k[1] ^ k[2]) - ROR(k[2], 7);
        k[0] = (k[0] ^ k[1]) - ROL(k[1], 16);
        k[2] = (k[2] ^ k[0]) - ROL(k[0], 4);
        k[1] = (k[1] ^ k[2]) - ROL(k[2], 14);
        k[0] = (k[0] ^ k[1]) - ROR(k[1], 8);
    }

    return k[0];
}

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi smashly,

long time not seen you here. Welcome back.

Can you provide an input and the expected output? Might be easier to test it.

Btw, might be that you are looking for _WinAPI_HashString()...

Br,
UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0