基于go-gmsm静态库编写的SM2椭圆曲线公钥密码算法PHP扩展 相较于openssl-ext-sm2编译更方便 增加了密文指定排序、识别ans1编码等功能

news/2024/7/8 4:46:50 标签: golang, 算法, php

go-ext-sm2

介绍

基于go-gmsm静态库编写的SM2椭圆曲线公钥密码算法PHP扩展
相较于openssl-ext-sm2编译更方便
增加了密文指定排序、识别ans1编码等功能

特性:非对称加密

git地址:https://gitee.com/state-secret-series/go-ext-sm2.git

软件架构

zend 常规PHP扩展结构

依赖要求

1,包内在lib文件目录下已包含了mac、liunx编译后的gosm2静态库,自动加载对应环境的静态库,可直接编译

2,编译时注意使用make clean时会把编译好的静态库删除

3,如静态库在编译时不可用、请手动编译go-gogmsm库,完成后放到对应环境的目录下 地址:https://gitee.com/state-secret-series/go-gmsm.git

安装教程

解压进入openssl-ext-sm2目录

cd go-ext-sm2
phpize

检查依赖

./configure --with-php-config=/www/server/php/*你的版本*/bin/php-config

检查结果

[root@6309fccb9e17 go-ext-sm2]# ./configure --with-php-config=/www/server/php/74/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether cc accepts -g... yes
checking for cc option to enable C11 features... -std=gnu11
checking how to run the C preprocessor... cc -std=gnu11 -E
checking for egrep -e... (cached) /usr/bin/grep -E
checking for icc... no
checking for suncc... no
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /www/server/php/74
checking for PHP includes... -I/www/server/php/74/include/php -I/www/server/php/74/include/php/main -I/www/server/php/74/include/php/TSRM -I/www/server/php/74/include/php/Zend -I/www/server/php/74/include/php/ext -I/www/server/php/74/include/php/ext/date/lib
checking for PHP extension directory... /www/server/php/74/lib/php/extensions/no-debug-non-zts-20190902
checking for PHP installed headers prefix... /www/server/php/74/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... gawk
checking whether to enable go_sm2 support... yes, shared
checking for gcc... (cached) cc
checking whether the compiler supports GNU C... (cached) yes
checking whether cc accepts -g... (cached) yes
checking for cc option to enable C11 features... (cached) -std=gnu11
checking whether cc -std=gnu11 supports C99... yes
checking for macOS... no
checking for a sed that does not truncate output... /usr/bin/sed
checking for ld used by cc -std=gnu11... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc -std=gnu11 object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc -std=gnu11 supports -fno-rtti -fno-exceptions... no
checking for cc -std=gnu11 option to produce PIC... -fPIC
checking if cc -std=gnu11 PIC flag -fPIC works... yes
checking if cc -std=gnu11 static flag -static works... no
checking if cc -std=gnu11 supports -c -o file.o... yes
checking whether the cc -std=gnu11 linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: patching config.h.in
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged

安装编译

make&&make install

修改php.ini

extension="go_sm2.so"

重启php-fpm或者apache

使用说明

全局定义常量

php">非ans1加密
GO_SM2_MODE_DEFAULT 
结果ans1加密
GO_SM2_MODE_ASN1

密文排列顺序
老规则
GO_SM2_ORDER_C1C2C3
新规则
GO_SM2_ORDER_C1C3C2
  1. 创建公钥和私钥
php">$pub_key 取地址 结果为二进制
$pri_key 结果为二进制

$key_pair=go_sm2_key_pair();
dump($key_pair);

array:3 [
  "code" => 200
  "msg" => "Key generation successful!"
  "data" => array:2 [
    "publicKey" =>二进制
    "privateKey" =>二进制
  ]
]

返回值code int 200 成功 其他状态失败

  1. 签名
php">$msg 信息
$pri_key 私钥 二进制
$mode 签名返回模式 默认 GO_SM2_MODE_DEFAULT
$iv userid 默认为1234567812345678 可不传
返回 GO_SM2_MODE_DEFAULT r,s 拼接的642进制
返回 GO_SM2_MODE_ANS1 r,s 编码的30开头的2进制
$sign=go_sm2_sign($msg, $pri_key)

array:3 [
  "code" => 200
  "msg" => "Key generation successful!"
  "data" => array:1 [
    "signature" => 二进制
  ]
]
//转成hex16进制

$iv='1224222232424333';
$sign=go_sm2_sign($msg,$pri_key,GO_SM2_MODE_ANS1)

$sign=go_sm2_sign($msg, $pri_key,GO_SM2_MODE_ANS1,$iv);

返回值code int 200 成功 其他状态失败

  1. 验签 自动识别是否是ans1 无需传mode
php">$msg 信息
$signature 输入签名结果
$pub_key 公钥 二进制
$iv userid 默认为1234567812345678

go_sm2_verify($msg, $pub_key,$signature)go_sm2_verify($msg, $pub_key,$signature,$iv);

返回值code int 200 成功 其他状态失败
  1. 公钥加密
php">$msg 信息
$encrypt 输出加密结果 二进制 
$pub_key 公钥 二进制
$order 排序方式 默认C1C3C2
$mode  是否ans1编码 默认不编码

$encrypt=go_sm2_encrypt($msg, $pub_key);

$encrypt=go_sm2_encrypt($msg, $pub_key,GO_SM2_ORDER_C1C2C3);

$encrypt=go_sm2_encrypt($msg, $pub_key,GO_SM2_ORDER_C1C2C3,GO_SM2_MODE_ASN1);

返回值code int 200 成功 其他状态失败
  1. 私钥解密 自动识别ans1编码格式的密文
php">$encrypt 加密信息 二进制
$string 输出结果 明文
$pri_key 私钥
$order 排序方式 默认C1C3C2

go_sm2_decrypt($encrypt, $string, $pri_key);
go_sm2_decrypt($encrypt, $string, $pri_key,GO_SM2_ORDER_C1C2C3);

返回值code int 200 成功 其他状态失败
  1. 演示
php">$key_pair = go_sm2_key_pair();

if ($key_pair['code'] != 200) {
    echo '生成钥匙对失败:' . $key_pair['msg'] . PHP_EOL;
    exit;
}
$msg     = "这是测试";
$pub_key = $key_pair['data']['publicKey'];
$pri_key = $key_pair['data']['privateKey'];
echo '公钥:' . bin2hex($pub_key) . PHP_EOL;
echo '私钥:', bin2hex($pri_key) . PHP_EOL;
$sign = go_sm2_sign($msg, $pri_key);
if ($sign['code'] != 200) {
    echo '签名失败:' . $sign['msg'] . PHP_EOL;
    exit;
}
echo '签名:' . bin2hex($sign['data']['signature']) . PHP_EOL;

$res = go_sm2_verify($msg, $pub_key, $sign['data']['signature']);
if ($res['code'] != 200) {
    echo '验签失败:' . $res['msg'] . PHP_EOL;
    exit;
}
echo '验签:' . $res['msg'] . PHP_EOL;

$encrypt = go_sm2_encrypt($msg, $pub_key);
if ($encrypt['code'] != 200) {
    echo '加密失败:' . $encrypt['msg'] . PHP_EOL;
    exit;
}
echo '密文:' . bin2hex($encrypt['data']['ciphertext']) . PHP_EOL;

$decrypt = go_sm2_decrypt($pri_key, $encrypt['data']['ciphertext']);
if ($decrypt['code'] != 200) {
    echo '解密失败:' . $encrypt['msg'] . PHP_EOL;
    exit;
}
echo '明文:' . $decrypt['data']['plaintext'] . PHP_EOL;

输出结果

php">公钥:0460fda1fe45d5dfcb84c7b7f4199ebca7bff423c7f1ca1eeb2e2abc1a004df3cd22aab950e476eef30fcff54ee0404d6fea09979a23bc9ee5dd883b05863f2f92
私钥:81905b16e44b267d6a179c5a09774cd698f8eac168f4429d793eb89931e9e7d0
签名:58cc705f11aef46db6469eefff2c26be386cc697d9f6d283f740ef3a116adb38a0a3e6a5e71afc9cd05973723678f25f339c1e38cdccb886e85dfb2a5c8795d9
验签:message verify successful!
密文:0427f56b468097cd763ac108079abb8e7b750d628fbc53c286c21270947e5885eda79451ba6416ebd8dcb4dee70c4e80cfc16971694b5d717ceab88def2d6586b98179419cfcf534bba12767540fc765e0b04a3576fba2cff67b5fe410b98c90672a98761f17e4725d4cde7
明文:这是测试


http://www.niftyadmin.cn/n/5536502.html

相关文章

浅谈http协议及常见的面试题

1、浅谈http协议 HTTP(Hypertext Transfer Protocol)超文本传输协议,是互联网上应用最为广泛的一种网络协议,所有的WWW文件都必须遵守这个标准。它是基于TCP/IP通信协议来传递数据(HTML文件、图片文件、查询结果等&am…

快速了解-注解Annotation

描述 Annotation定义:注解是Java语言从JDK 5.0版本开始引入的一种技术。 Annotation作用: 注解不是程序本身,但可以对程序作出解释。这与注释(comment)类似,但注解可以被其他程序(如编译器&…

使用vue3-treeselect问题

1.当vue3-treeselect是单选时,使用watch监听绑定value,无法监听到值清空 对照后将:value改为v-model,如图 2.使用vue3-treeselect全部清空按钮如何置空select的值,使用watch监听 多选:pageInfo.officeName(val) {// …

#Vue 3 + ts + antd table表格的使用(嵌套 子表格版)

1. 嵌套子表格的使用 <template><a-table :columns"columns" :data-source"data" class"components-table-demo-nested"><template #bodyCell"{ column }"><template v-if"column.key operation">…

WCCI 2024第三弹:忍者表演惊艳全场,盛大晚宴不容错过

WCCI 2024第三弹&#xff1a;忍者表演惊艳全场&#xff0c;盛大晚宴不容错过&#xff01; 会议之眼 快讯 会议介绍 IEEE WCCI&#xff08;World Congress on Computational Intelligence&#xff09;2024&#xff0c;即2024年IEEE世界计算智能大会&#xff0c;于6月30日至7月…

LabVIEW航空用电缆检测

系统通过集成LabVIEW平台&#xff0c;实现了航空用电缆检测过程中的自动数据收集、处理和报告生成&#xff0c;显著提升了检测效率和数据准确性&#xff0c;降低了人工干预&#xff0c;提高了电缆检测的可靠性。 项目背景 在航空领域&#xff0c;电缆的质量检测对确保飞机及其…

地铁车厢火灾3D模拟逃生演习减少了资源损耗和风险

在消防安全领域&#xff0c;为了更好地提升安全实训效果&#xff0c;我们在VR安全培训领域打造了多款消防安全VR模拟实训系统&#xff0c;不仅实现了与现实世界无异的交互操作&#xff0c;更在虚拟空间中超越了现实的限制&#xff0c;模拟出那些现实中难以搭建的复杂场景。 利用…

【Unity navmeshaggent 组件】

【Unity navmeshaggent 组件】 组件概述&#xff1a; NavMeshAgent是Unity AI系统中的一个组件&#xff0c;它允许游戏对象&#xff08;通常是一个角色或AI&#xff09;在导航网格&#xff08;NavMesh&#xff09;上自动寻路。 组件属性&#xff1a; Radius&#xff1a;导航…