我遇到了一些相當有趣的代碼來到今天在檢討SHA1的實施C.有趣的位運算
temp = SHA1CircularShift(5,A) + (B^C^D) + E + W[t] + K[1];
temp &= 0xFFFFFFFF;
我覺得有趣的部分是temp &= 0xFFFFFFFF;
。請注意0已被宣佈爲無符號整數。這個操作不會簡單地沒有效果嗎?我能想到的唯一的事情是設計者試圖強制使用32位整數,但這不是在編譯時完成的嗎?
我很想知道人們的想法。
我遇到了一些相當有趣的代碼來到今天在檢討SHA1的實施C.有趣的位運算
temp = SHA1CircularShift(5,A) + (B^C^D) + E + W[t] + K[1];
temp &= 0xFFFFFFFF;
我覺得有趣的部分是temp &= 0xFFFFFFFF;
。請注意0已被宣佈爲無符號整數。這個操作不會簡單地沒有效果嗎?我能想到的唯一的事情是設計者試圖強制使用32位整數,但這不是在編譯時完成的嗎?
我很想知道人們的想法。
在某些機器上,int
(因此也是unsigned int
)可能是64位類型。掩碼在機器上沒有任何操作,其中int
是32位類型,但對於64位類型的機器很重要。編譯器會知道並且在沒有任何用處時會優化操作。
此外,過去有機器36-bit int
types和其他與60-bit int
types;這對於這樣的機器也是很重要的。
SHA1的reference implementation,有評論如下注意事項:
/*
* sha1.c
*
* Description:
* This file implements the Secure Hashing Algorithm 1 as
* defined in FIPS PUB 180-1 published April 17, 1995.
*
* The SHA-1, produces a 160-bit message digest for a given
* data stream. It should take about 2**n steps to find a
* message with the same digest as a given message and
* 2**(n/2) to find any two messages with the same digest,
* when n is the digest size in bits. Therefore, this
* algorithm can serve as a means of providing a
* "fingerprint" for a message.
*
* Portability Issues:
* SHA-1 is defined in terms of 32-bit "words". This code
* uses <stdint.h> (included via "sha1.h" to define 32 and 8
* bit unsigned integer types. If your C compiler does not
* support 32 bit unsigned integers, this code is not
* appropriate.
*
* Caveats:
* SHA-1 is designed to work with messages less than 2^64 bits
* long. Although SHA-1 allows a message digest to be generated
* for messages of any number of bits less than 2^64, this
* implementation only works with messages with a length that is
* a multiple of the size of an 8-bit character.
*
*/
的Portability Issues
是在此實現SHA1的這種操作的情況下,允許其與大int
小號機器正常運行。
「temp」的確切類型*是什麼? – 2015-01-21 05:47:40
在某些機器上,'int'(或'unsigned int')可能是64位類型。掩碼在「int」是32位類型的計算機上是no-op,但對於64位類型的計算機很重要。編譯器會知道並且在沒有任何用處時會優化操作。 – 2015-01-21 05:47:51