Sha-256
如何生成 SHA-2 常量?
SHA 規範指出——例如,在 SHA-224 和 SHA-256的情況下——常數“代表前 64 個素數的立方根的小數部分的前 32 位”。這些值是如何“表示”的?從質數開始,以十六進制常量結束,生成規範中提供的所有常量的正確步驟順序是什麼?
這正是它的聲音。注意:
428a2f98
當解釋為十六進制分數時,其值4/16 + 2/(16^2) + 8/(16^3) + 10/(16^4) + ...
大約等於0.2599210496991873
,因此當您添加回非小數部分時,您會得到1.2599210496991873
第一個素數 (2) 的立方根。在蟒蛇中:
In [1]: 1+sum([int(c,16)/(16.**(i+1)) for (i,c) in enumerate('428a2f98')]) Out[1]: 1.2599210496991873 In [2]: 2**(1/3.) Out[2]: 1.2599210498948732
要生成常量,您可以使用以下程式碼使用gmpy2 python 庫:
from gmpy2 import mpfr, floor, next_prime def convert_primes_cube_fractional_part_to_hex_constant(prime, hex_chars=8): """ Note if you want the first 8 decimal (base=10) digits of a number, you multiply the fractional part by 10**8 and then look at the integer part In this case we want first 8 hex digits, so multiply fractional part by 16**8 and then look at integer part (and return in hexadecimal). """ cube_root = mpfr(prime)**(1/mpfr(3)) frac_part = cube_root - floor(cube_root) format_str = '%%0%dx' % hex_chars # format_str will be '%08x' if hex_chars=8 so always emits # 8 zero-padded hex digits return format_str % floor(frac_part*(16**hex_chars)) def generate_n_primes(n=64): p = 2 i = 0 while i < n: yield p p = next_prime(p) i += 1
After defining those functions you can run, the following to recreate the table:
>>> for i,p in enumerate(generate_n_primes(64)): if i % 8 == 0: print "" print convert_primes_cube_fractional_part_to_hex_constant(p, hex_chars=8), 428a2f98 71374491 b5c0fbcf e9b5dba5 3956c25b 59f111f1 923f82a4 ab1c5ed5 d807aa98 12835b01 243185be 550c7dc3 72be5d74 80deb1fe 9bdc06a7 c19bf174 e49b69c1 efbe4786 0fc19dc6 240ca1cc 2de92c6f 4a7484aa 5cb0a9dc 76f988da 983e5152 a831c66d b00327c8 bf597fc7 c6e00bf3 d5a79147 06ca6351 14292967 27b70a85 2e1b2138 4d2c6dfc 53380d13 650a7354 766a0abb 81c2c92e 92722c85 a2bfe8a1 a81a664b c24b8b70 c76c51a3 d192e819 d6990624 f40e3585 106aa070 19a4c116 1e376c08 2748774c 34b0bcb5 391c0cb3 4ed8aa4a 5b9cca4f 682e6ff3 748f82ee 78a5636f 84c87814 8cc70208 90befffa a4506ceb bef9a3f7 c67178f2
Note this exactly matches the table in the NIST publication.
You can generate the other SHA-512 constants with the following code. Note you first have to increase the multiple precision floating point math in gmpy2 to ~100 digits from the default 53 digits, or you’ll find the last few hexdigits are always
0
due to loss of precision.>>> gmpy2.get_context().precision=100 >>> for i,p in enumerate(generate_n_primes(80)): if i % 4 == 0: print "" print convert_primes_cube_fractional_part_to_hex_constant(p, hex_chars=16),