PHP code example of jiangwang / one-sm

1. Go to this page and download the library: Download jiangwang/one-sm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jiangwang / one-sm example snippets



 = new \OneSm\Sm3();

// 字符串签名
echo $sm3->sign('abc') . PHP_EOL;
echo $sm3->sign(str_repeat("adfas哈哈哈", 100)) . PHP_EOL;


// 文件签名
echo $sm3->signFile(__FILE__) . PHP_EOL;


use OneSm\Sm4;
'阿斯顿发到付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);

shell
php bench.php