这几天对接中国联通物联网IoT Gateway门户API系统,其中用到国密SM3,其提供的token需使用国密SM3对源串进行加密处理,网上搜索也没找到什么函数之类的,只有c和java语言写的,看来要使用这个SM3加密就必须进行扩展了;由于之前没搞过再者又没找到,都想放弃了,偶然发现了:a php extension for sm3(实现sm3算法的php扩展)【git 地址】 OK了,就他了,进行测试!
php-sm3
a php extension for sm3(实现sm3算法的php扩展)
安装步骤
git clone该项目到你的linux php源码ext目录下(可以是任意目录 ).
如:[root@localhost home]# git clone https://github.com/luzhuqun/php-sm3.git
进入sm3扩展,执行/usr/local/php/bin/phpize
如:[root@localhost home]# cd php-sm3
[root@localhost php-sm3]# /www/server/php/72/bin/phpize (我使用的宝塔面板)
./configure --with-php-config=/usr/local/php/bin/php-config
如:[root@localhost php-sm3]# ./configure --with-php-config=/www/server/php/72/bin/php-config
make && make install
如:[root@localhost php-sm3]# make && make install
添加扩展到你的php.ini文件 extension=sm3.so
登陆宝塔面板,找到PHP版本
测试/usr/local/php/bin/php -r "var_dump(sm3('abc'));" 输出string(64) "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0"
重启下php,这下就好了,可以在你的程序中使用了:
PHP
var_dump(sm3('edxdf'));
?>
1
2
3
var_dump(sm3('edxdf'));
?>
到此结束,以后有类似的扩展都可以这样安装!今天又上了一课!
重要提示:
之前安装的是在PHP7.2上面一切都是那么的自然,今天在PHP5.5上面安装就提示错误了:
Python
/root/php-sm3/sm3.c: In function ‘zif_confirm_sm3_compiled’:
/root/php-sm3/sm3.c:457: error: ‘zend_string’ undeclared (first use in this function)
/root/php-sm3/sm3.c:457: error: (Each undeclared identifier is reported only once
/root/php-sm3/sm3.c:457: error: for each function it appears in.)
/root/php-sm3/sm3.c:457: error: ‘strg’ undeclared (first use in this function)
/root/php-sm3/sm3.c:499:30: error: macro "RETURN_STRINGL" requires 3 arguments, but only 2 given
/root/php-sm3/sm3.c: In function ‘zif_sm3’:
/root/php-sm3/sm3.c:499: error: ‘RETURN_STRINGL’ undeclared (first use in this function)
make: *** [sm3.lo] Error 1
1
2
3
4
5
6
7
8
9
/root/php-sm3/sm3.c:Infunction‘zif_confirm_sm3_compiled’:
/root/php-sm3/sm3.c:457:error:‘zend_string’undeclared(firstuseinthisfunction)
/root/php-sm3/sm3.c:457:error:(Eachundeclaredidentifierisreportedonlyonce
/root/php-sm3/sm3.c:457:error:foreachfunctionitappearsin.)
/root/php-sm3/sm3.c:457:error:‘strg’undeclared(firstuseinthisfunction)
/root/php-sm3/sm3.c:499:30:error:macro"RETURN_STRINGL"requires3arguments,butonly2given
/root/php-sm3/sm3.c:Infunction‘zif_sm3’:
/root/php-sm3/sm3.c:499:error:‘RETURN_STRINGL’undeclared(firstuseinthisfunction)
make:***[sm3.lo]Error1
最终发现只有PHP>=7.0才可正常使用,否则出错!如果你在低于7.0上安装成功的话,麻烦您告诉我下!求教,谢谢!