我正在使用字符串在PHP中实现SHA 256。你在哪里截断算法?
有很多地方发生了二元加法,所以没有。 W(i)中的比特数保持增加,与压缩函数相同。那么我在哪里截断这些值?
当我var_dump我的数组时,我得到了这个:
array(64) { [0]=> string(32) "01100001001000000111010001100101" [1]=> string(32) "01110011011101001000000000000000" [2]=> string(32) "00000000000000000000000000000000" [3]=> string(32) "00000000000000000000000000000000" [4]=> string(32) "00000000000000000000000000000000" [5]=> string(32) "00000000000000000000000000000000" [6]=> string(32) "00000000000000000000000000000000" [7]=> string(32) "00000000000000000000000000000000" [8]=> string(32) "00000000000000000000000000000000" [9]=> string(32) "00000000000000000000000000000000" [10]=> string(32) "00000000000000000000000000000000" [11]=> string(32) "00000000000000000000000000000000" [12]=> string(32) "00000000000000000000000000000000" [13]=> string(32) "00000000000000000000000000000000" [14]=> string(32) "00000000000000000000000000000000" [15]=> string(32) "00000000000000000000000000110000" [16]=> string(32) "10001111101010001101101001000010" [17]=> string(32) "01110011100100101000000000000000" [18]=> string(32) "01110110010010101111110000010111" [19]=> string(32) "00010000000111001101001100011011" [20]=> string(32) "00100001100101001100011101010011" [21]=> string(32) "11110011111010101110110100111001" [22]=> string(32) "11111011010010111001000111111001" [23]=> string(32) "10111010101100000001011101110101" [24]=> string(33) "100101110100011111001000000101000" [25]=> string(32) "01111111110001011100001000100010" [26]=> string(33) "010000100100001011110010010001101" [27]=> string(32) "01111010110111110100100010111101" [28]=> string(33) "110010001011110100010000011100100" [29]=> string(33) "101001000101000110011011111011111" [30]=> string(33) "111000011011101011011101110010101" [31]=> string(34) "1111001011100001001101111011110110" [32]=> string(34) "1010010011000001000100101100000010" [33]=> string(35) "10101101001100111001111100111111010" [34]=> string(34) "1110100101001100001000011110000110" [35]=> string(36) "100101010100100001101110101111000011" [36]=> string(34) "1110001101001101001101110111011000" [37]=> string(36) "110101001100000100001110011000000101" [38]=> string(36) "100110010110100011010111011101100110" [39]=> string(36) "101110011100110000110001010010110000" [40]=> string(36) "100000101011010110001001000100011101" [41]=> string(37) "1001110001010110110001110110101011101" [42]=> string(37) "1000000101001011010111010000001100100" [43]=> string(37) "1001000011101111000001111110010100100" [44]=> string(38) "10010000101101110001100100010100010101" [45]=> string(37) "1011111001101101011111001000110010000" [46]=> string(39) "100101011110011011000000001001110010001" [47]=> string(37) "1001101011101110111000000100111010000" [48]=> string(39) "010001011100001101101000011010011100010" [49]=> string(38) "10011100010110000011111110010101101000" [50]=> string(39) "110011100111000010101100100101110011001" [51]=> string(38) "11001101111101010001000010110001100100" [52]=> string(40) "1001010000101100000100110100110000100011" [53]=> string(40) "1000010101011111001110000100101100111000" [54]=> string(40) "1111011111011011100000000001110000011101" [55]=> string(40) "0101110001001001110010001011110111010010" [56]=> string(40) "0100111101101110101111101011110000000011" [57]=> string(40) "1100101000101110000000101100000111111110" [58]=> string(40) "1101100110111011011010010001101100000100" [59]=> string(41) "10000100101011000111001100110101010010001" [60]=> string(41) "10111001010111100010000110001111101100110" [61]=> string(42) "110001101011011010100110111110101100101111" [62]=> string(42) "101001011000100100011001011101100100101111" [63]=> string(42) "101110011001100001001100001010110101001111" }
在将数据推送到数组之前或数组完成之后,是否要截断这些值? 如果我之前截断,相应的迭代将占用截断值并更改其值。
另外
$tmp=$q4;
$tr1=substr($tmp,-32);
$tmp2=binary_add($s0,$maj);
$tr2=substr($tmp2,-32);
$H=$G;
$G=$F;
$F=$E;
$E=binary_add($tr1,$D);
$D=$C;
$C=$B;
$B=$A;
$A=binary_add($tr1,$tr2);
这里也注意到我在添加临时变量之前已经截断(并将var A更新为H)这是正确的吗?
答案 0 :(得分:1)
首先,你不应该自己实现SHA-256。你可能最终得到了一些有缺陷和缓慢的东西。您应该在平台的库中使用该实现。请记住加密规则:
SHA-256产生256位输出。看起来您的转储包含算法的内部状态,该状态远远超过256位。如果这是你在SHA-256函数结束时所拥有的,那么你就错过了一个步骤。您不应该截断任何内容以获得最终输出。