承接 evit/php-gm-crypto 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

evit/php-gm-crypto

最新稳定版本:1.0.4

Composer 安装命令:

composer require evit/php-gm-crypto

包简介

Fully compatible with the sm4-cbc and sm4-ecb state secret SM algorithms of openssl. When openssl >= 1.1.1 supports the state secret algorithm, directly call openssl for SM4 encryption and decryption, otherwise call the custom algorithm.

README 文档

README

  • PHP后端:composer require evit/php-gm-crypto
  • 对应前端:npm install evit-gm-crypt

php-gm-crypto

PHP Style GuideLicense: MIT

基于PHP的国密加密算法实现。

Implement of Chinese encrypt algorithm in PHP.

完全兼容openssl的sm4-cbc和sm4-ecb国密SM算法,openssl >= 1.1.1支持国密算法时直接调用openssl进行SM4加解密,否则调用自定义算法。

Fully compatible with the sm4-cbc and sm4-ecb state secret SM algorithms of openssl. When openssl >= 1.1.1 supports the state secret algorithm, directly call openssl for SM4 encryption and decryption, otherwise call the custom algorithm.

新增SM3实现,openssl >= 1.1.1支持国密算法时直接调用openssl进行SM3杂凑,否则调用自定义算法。

openssl_encryptopenssl_decrypt保持一致性,当密码长度小于16时,静默填充NUL;当密码长度大于16时,静默截断。

Consistent with openssl_encrypt and openssl_decrypt, NUL is silently filled when the password length is less than 16; When the password length is greater than 16, it is truncated silently.

要使用openssl国密算法,openssl库需 >= 1.1.1。

To use openssl state secret algorithm, openssl library needs > = 1.1.1.

Roadmap

  • SM4
  • SM3
  • SM2

Documentation

Install

composer require evit/php-gm-crypto

SM4

Init

<?php
// If you are using a framework that does not support psr-4 autoloader, you need to explicitly import package from the vendor directory.
require_once __DIR__ . "/vendor/autoload.php";

use Evit\PhpGmCrypto\Encryption\EvitSM4Encryption;

$config = [
    // mode string 'cbc' or 'ecb' is supported, default is 'cbc'.
    'mode'  => 'cbc',
    // password, will be processed by substr(md5($key), 0, 16) if $config['hash']
    'key'   => '{replace-your-key-here}',
    // the iv used by 'cbc' mode, will be will be processed by substr(md5($iv), 0, 16) if $config['hash']
    'iv'    => '{replace-your-iv-here}',
    // weather do md5 to key and iv or not
    'hash'  => false
];

$sm4 = new EvitSM4Encryption($config);

Encrypt

// Encrypt
$start = microtime(true);
$encypted = $sm4->sm4encrypt('{replace-your-plaintext-here}');
$end = microtime(true);
var_dump('Encrypt time elapsed:' . number_format($end - $start, 8) . ' s');
var_dump("Cipher text:{$encypted}");

Decrypt

// Decrypt
$decStart = microtime(true);
$decrypted = $sm4->sm4decrypt($encypted);
$end = microtime(true);
var_dump('Decrypt time elapsed:' . number_format($end - $decStart, 8) . ' s');
var_dump("Plain text:{$decrypted}");

Algorithm

// Determine whether openssl library is used
$algorithm = $sm4->isOpenssl() ? 'openssl' : 'php-gm-crypto';
var_dump("Algrithm is:{$algorithm}");
var_dump('Total time elapsed:' . number_format($end - $start, 8) . ' s');

SM3

// If you are using a framework that does not support psr-4 autoloader, you need to explicitly import package from the vendor directory.
require_once __DIR__ . "/vendor/autoload.php";

use Evit\PhpGmCrypto\Encryption\EvitSM3Encryption;

$sm3 = new EvitSM3Encryption();
$input = 'abc';

$output = $sm3->sm3($input);
var_dump("SM3 hash result of '{$input}' is '{$output}'");

对应的前端安装

npm install evit-gm-crypt

传送门:evit-gm-crypt

License

MIT

统计信息

  • 总下载量: 3.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-07