了解 MD5 算法以及如何不发生溢出

Understanding MD5 algorithm and how overflow doesn't occur

我一直在查看维基百科上的 MD5 算法伪代码,但我似乎无法真正理解如何不发生整数溢出。

特别是这部分:

//Add this chunk's hash to result so far:
    a0 := a0 + A
    b0 := b0 + B
    c0 := c0 + C
    d0 := d0 + D

我认为最终会发生整数溢出,尤其是在输入较大的情况下。价值不会继续膨胀和膨胀吗?

https://en.wikipedia.org/wiki/MD5

正如维基百科伪代码顶部的评论所说:

//Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating

因此您需要确保无符号 32 位整数在您的平台上以 2^32 为模进行换行。许多语言都是这种情况,例如C。否则,您可能需要手动执行取模。