I'm attempting to implement MD5 (for curiosity's sake) in Python/Java, and am effectively translating the wikipedia MD5 page's pseudocode into either language. First, I used Java, only to encounter frustration with its negative/positive integer overflow (because unsigned ints aren't an option, for-all integer,-2147483648 <= integer <= 2147483647). I then employed Python, after deciding that it's better suited for heavy numerical computation, but realized that I wouldn't be able to overcome the unsigned 32-bit integer requirement, either (as Python immediately casts wrapped ints to longs).

Is there any way to hack around Java/Python's lack of unsigned 32-bit integers, which are required by the aforementioned MD5 pseudocode?

有帮助吗?

解决方案 2

As a note beforehand - I don't know if this is a good solution, but it appears to give the behaviour you want.

Using the ctypes module, you can access the underlying low-level data-type directly, and hence have an unsigned int in Python.

Specifically, ctypes.c_uint:

>>> i = ctypes.c_uint(0)
>>> i.value -= 1
>>> i                                                            
c_uint(4294967295)
>>> i.value += 1
>>> i
c_uint(0)

This is arguably abuse of the module - it's designed for using C code easily from within Python, but as I say, it appears to work. The only real downside I can think of is that I assume ctypes is CPython specific.

其他提示

Since all the operations are bitwise operations, they wouldn't suffer from sign extension (which would cause you problems), except for right shift.

Java has a >>> operator for this purpose.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top