thinkPHP 的 CRC(32/16)算法实现

嘿!哈喽,骚骚的飞猪又来写文章了,开不开心,激不激动!

哎!那位帅哥,不是!这位美女,请冷静一点,不要太过激动,冷静、冷静......

此情此景,我虽然早已习惯,可不得不佩服我的想象,居然能凭空虚化,无中生有地造出一段场景!骚!嗯...

生活是风景,日久会生情,人生是路,不过曲折与直,思想的强大是孤独之本吗?孤独不过是自我回避罢了,不过,人们总希望拿丧当借口罢了,并非心理不够强大!——悟极

php.net 官网有一个例子:https://www.php.net/manual/en/function.crc32.php

CRC16的用法:

function crc16($string) {
  $crc = 0xFFFF;
  for ($x = 0; $x < strlen ($string); $x++) {
    $crc = $crc ^ ord($string[$x]);
    for ($y = 0; $y < 8; $y++) {
      if (($crc & 0x0001) == 0x0001) {
        $crc = (($crc >> 1) ^ 0xA001);
      } else { $crc = $crc >> 1; }
    }
  }
  return $crc;
}

function demo(){
    //字符串形式
    echo dechex($this-> crc16("1234567890qwertyuiopasdfghjkklzxcvbnm");

    //上传文件形式
    $files = $_FILES["file"];  //获取上传文件
    $file = $files["tmp_name"];
    $code = dechex($this-> crc16(file_get_contents($file)));
    echo $code;
}

CRC32的用法:

function demo(){
    $files = $_FILES["file"];  //获取上传文件
    $file = $files["tmp_name"];
    $code = hash_file("crc32", $file);
    //$code = md5_file("crc32", $file);
    echo $code;
}
//如果你要以检验文件内容的
function demo2(){
    $files = $_FILES["file"];  //获取上传文件
    $file = $files["tmp_name"];
    $code= dechex(crc32(file_get_contents($file)));
    echo $code;
}

该方法是借用封装器进行散列/md5文件校验的
参考:https://www.cnblogs.com/mslagee/p/6223140.html

CRC16的多种用法:

在效率要求不高的情况下,可以使用php来实现crc16算法:

/**
 * 将一个字符按比特位进行反转 eg: 65 (01000001) --> 130(10000010)
 */
function reverseChar($char) {
    $byte = ord($char);
    $tmp = 0;
    for ($i = 0; $i < 8; ++$i) {
        if ($byte & (1 << $i)) {
            $tmp |= (1 << (7 - $i));
        }
    }
    return chr($tmp);
}
 
/**
 * 将一个字节流按比特位反转 eg: 'AB'(01000001 01000010)  --> '\x42\x82'(01000010 10000010)
 */
function reverseString($str) {
    $m = 0;
    $n = strlen($str) - 1;
    while ($m <= $n) {
        if ($m == $n) {
            $str{$m} = reverseChar($str{$m});
            break;
        }
        $ord1 = reverseChar($str{$m});
        $ord2 = reverseChar($str{$n});
        $str{$m} = $ord2;
        $str{$n} = $ord1;
        $m++;
        $n--;
    }
    return $str;
}
 
/**
 * @param string $str 待校验字符串
 * @param int $polynomial 二项式
 * @param int $initValue 初始值
 * @param int $xOrValue 输出结果前异或的值
 * @param bool $inputReverse 输入字符串是否每个字节按比特位反转
 * @param bool $outputReverse 输出是否整体按比特位反转
 */
function crc16($str, $polynomial, $initValue, $xOrValue, $inputReverse = false, $outputReverse = false) {
    $crc = $initValue;
 
    for ($i = 0; $i < strlen($str); $i++) {
        if ($inputReverse) {
            // 输入数据每个字节按比特位逆转
            $c = ord(reverseChar($str{$i}));
        } else {
            $c = ord($str{$i});
        }
        $crc ^= ($c << 8);
        for ($j = 0; $j < 8; ++$j) {
            if ($crc & 0x8000) {
                $crc = (($crc << 1) & 0xffff) ^ $polynomial;
            } else {
                $crc = ($crc << 1) & 0xffff;
            }
        }
    }
    if ($outputReverse) {
        // 把低地址存低位,即采用小端法将整数转换为字符串
        $ret = pack('cc', $crc & 0xff, ($crc >> 8) & 0xff);
        // 输出结果按比特位逆转整个字符串
        $ret = reverseString($ret);
        // 再把结果按小端法重新转换成整数
        $arr = unpack('vshort', $ret);
        $crc = $arr['short'];
    }
    return $crc ^ $xOrValue;
}

 下面列出一些常用的crc16算法实现,是一些大厂所采用的或者是某某协议中用到的,可以使用网站 http://www.ip33.com/crc.html 进行在线计算

/* 列举一些常用的crc16算法 */
 
// CRC-16/IBM
printf("%x\n", crc16('1234567890', 0x8005, 0, 0, true, true));
 
// CRC-16/MAXIM
printf("%x\n", crc16('1234567890', 0x8005, 0, 0xffff, true, true));
 
// CRC-16/USB
printf("%x\n", crc16('1234567890', 0x8005, 0xffff, 0xffff, true, true));
 
// CRC-16/MODBUS
printf("%x\n", crc16('1234567890', 0x8005, 0xffff, 0, true, true));
 
// CRC-16/CCITT
printf("%x\n", crc16('1234567890', 0x1021, 0, 0, true, true));
 
// CRC-16/CCITT-FALSE
printf("%x\n", crc16('1234567890', 0x1021, 0xffff, 0, false, false));
 
// CRC-16/X25
printf("%x\n", crc16('1234567890', 0x1021, 0xffff, 0xffff, true, true));
 
// CRC-16/XMODEM
printf("%x\n", crc16('1234567890', 0x1021, 0, 0, false, false));
 
// CRC-16/DNP
printf("%x\n", crc16('1234567890', 0x3d65, 0, 0xffff, true, true));

上列方法的 CRC-16/MODBUS 实现的算法结果值 与 上面的 “CRC16的用法” 的算法结果值是一样的!

参考2:PHP实现crc算法

ok,全部骚完,就可以飞了!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸曦穆泽

您的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值