I really give up

Tim Rowe tim at remove_if_not_spam.digitig.cix.co.uk
Wed Oct 2 12:24:57 EDT 2002


I've not looked at the detail, but that doesn't look as if it would
port easily between C platforms, never mind to Python.  Doesn't it
assume that unsigned int is exactly 32 bits? But a C compiler can
legally give you more, and it looks as if those left shifts could give
you different results in that case.  If you rewrite it to be portable
C then I'd guess it would go into Python directly using long integers,
although there may be an execution-time penalty for that.  Otherwise
I'd advise staying with C! 


On Thu, 3 Oct 2002 16:51:15 +0200, "piter" <tojopiter at yahoo.com>
wrote:


>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