I really give up

piter tojopiter at yahoo.com
Thu Oct 3 10:51:15 EDT 2002


Hey guys,

I really give up! How to convert the following C function into python?

//-----------------------------------------
unsigned int
make_hash(char *password, unsigned int seed)
{
    unsigned int x, y, z;

    y = seed;

    for (x = 0; *password; password++) {
        x = (x & 0xffffff00) | *password;
        y ^= x;
        y += x;
        x <<= 8;
        y ^= x;
        x <<= 8;
        y -= x;
        x <<= 8;
        y ^= x;

        z = y & 0x1f;
        y = (y << z) | (y >> (32 - z));
    }

    return y;
}

//-----------------------------------------

The obvious solution:

#----------------------------------------
def make_hash( password, seed ):
    y = seed
    x = 0
    for p in password:
        x = (x & 0xffffff00) | ord(p)
        y ^= x
        y += x
        x <<= 8
        y ^= x
        x <<= 8
        y -= x
        x <<= 8
        y ^= x

        z = y & 0x1f
        y = (y << z) | (y >> (32 - z))
    return y

#----------------------------------------
produces odd result due to python's signed arithmetic.

piter





More information about the Python-list mailing list