Heim  >  Artikel  >  Backend-Entwicklung  >  php中有点加密算法中str_repeat,str_pad 为何使用 chr返回ascii码

php中有点加密算法中str_repeat,str_pad 为何使用 chr返回ascii码

WBOY
WBOYOriginal
2016-06-13 12:10:001283Durchsuche

php中有些加密算法中str_repeat,str_pad 为何使用 chr返回ascii码?
请看下面的加密算法:

function custom_hmac($algo, $data, $key, $raw_output = false)<br />{<br />$algo = strtolower($algo);<br />$pack = 'H'.strlen($algo('test'));<br />$size = 64;<br />$opad = str_repeat(chr(0x5C), $size);<br />$ipad = str_repeat(chr(0x36), $size);<br />//这样子的代码请详细解说一下<br />if (strlen($key) > $size) {<br />$key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));<br />} else {<br />$key = str_pad($key, $size, chr(0x00));<br />}<br /><br />for ($i = 0; $i < strlen($key) - 1; $i++) {<br />$opad[$i] = $opad[$i] ^ $key[$i];<br />$ipad[$i] = $ipad[$i] ^ $key[$i];<br />}// 这个也不太明白<br /><br />$output = $algo($opad.pack($pack, $algo($ipad.$data)));<br /><br />return ($raw_output) ? pack($pack, $output) : $output;<br />}

------解决思路----------------------
str_pad 用指定字符充填串到指定长度
在这里就是 $size 了
长度一样了,下面的循环中就不需要判断元素是否存在了

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn