You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yangsai@163.com
cf3d792c05
|
6 months ago | |
---|---|---|
.. | ||
.github | 6 months ago | |
src | 6 months ago | |
tests | 6 months ago | |
.gitignore | 6 months ago | |
.php_cs.dist | 6 months ago | |
README.md | 6 months ago | |
bench.php | 6 months ago | |
composer.json | 6 months ago | |
test.php | 6 months ago |
README.md
php国密算法
- sm3
- 字符串签名
- 文件签名
- sm4
- ecb
- cbc
- cfb
- ofb
- ctr
安装
composer require lizhichao/one-sm
SM3签名
<?php
require __DIR__ . '/vendor/autoload.php';
$sm3 = new \OneSm\Sm3();
// 字符串签名
echo $sm3->sign('abc') . PHP_EOL;
echo $sm3->sign(str_repeat("adfas哈哈哈", 100)) . PHP_EOL;
// 文件签名
echo $sm3->signFile(__FILE__) . PHP_EOL;
性能测试
php bench.php
结果
openssl:4901d7181a1024b8c0f59b8d3c5c6d96b4b707ad10e8ebc8ece5dc49364a3067
one-sm3:4901d7181a1024b8c0f59b8d3c5c6d96b4b707ad10e8ebc8ece5dc49364a3067
SM3-PHP:4901d7181a1024b8c0f59b8d3c5c6d96b4b707ad10e8ebc8ece5dc49364a3067
openssl time:6.3741207122803ms
one-sm3 time:8.1770420074463ms
SM3-PHP time:1738.5928630829ms
SM4加密
<?php
use OneSm\Sm4;
require __DIR__ . '/vendor/autoload.php';
$data = str_repeat('阿斯顿发到付eeee', 160);
$str_len = strlen($data);
// md5 签名
$sign = md5($data);
// 加密key必须为16位
$key = hex2bin(md5(1));
$sm4 = new Sm4($key);
// ECB加密
$d = $sm4->enDataEcb($data);
// 加密后的长度和原数据长度一致
var_dump(strlen($d) === $str_len);
// ECB解密
$d = $sm4->deDataEcb($d);
// 解密后和原数据相等
var_dump(md5($d) === $sign);
// 初始化向量16位
$iv = hex2bin(md5(2));
// CBC加密
$d = $sm4->enDataCbc($data, $iv);
// 加密后的长度和原数据长度一致
var_dump(strlen($d)===$str_len);
// CBC解密
$d = $sm4->deDataCbc($d, $iv);
// 解密后和原数据相等
var_dump(md5($d)===$sign);