Home > Backend Development > PHP Tutorial > php中有点加密算法中str_repeat,str_pad 为何使用 chr返回ascii码

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

WBOY
Release: 2016-06-13 12:10:00
Original
1335 people have browsed it

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 />}
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template