Home >Backend Development >PHP Tutorial >16位md5压缩为8位字符串算法疑惑

16位md5压缩为8位字符串算法疑惑

WBOY
WBOYOriginal
2016-06-06 20:06:552902browse

生成一个8位的随机字符串

<code>function make_coupon_card() {    
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    
    $rand = $code[rand(0,25)]    
        .strtoupper(dechex(date('m')))    
        .date('d').substr(time(),-5)    
        .substr(microtime(),2,5)    
        .sprintf('%02d',rand(0,99));    
    for(    
        $a = md5( $rand, true ),    
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',    
        $d = '',    
        $f = 0;    
        $f </code>

对于( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F 的按位异或 减去本身 再进行的与运算,最终的范围是0-31之间,这个是如何确定的?

回复内容:

生成一个8位的随机字符串

<code>function make_coupon_card() {    
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    
    $rand = $code[rand(0,25)]    
        .strtoupper(dechex(date('m')))    
        .date('d').substr(time(),-5)    
        .substr(microtime(),2,5)    
        .sprintf('%02d',rand(0,99));    
    for(    
        $a = md5( $rand, true ),    
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',    
        $d = '',    
        $f = 0;    
        $f </code>

对于( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F 的按位异或 减去本身 再进行的与运算,最终的范围是0-31之间,这个是如何确定的?

( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F
简而言之有个关键点,运算符-的优先级要比&高。
所以整体来看应该是( ( $g ^ ord( $a[ $f + 8 ] ) ) - $g )0x1F进行与运算,
0x1F就是十进制的31,取与的结果范围就限定在了0 - 31之间。

Statement:
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