PHP code example of xgrug / laravel-gm-crypto

1. Go to this page and download the library: Download xgrug/laravel-gm-crypto 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/ */

    

xgrug / laravel-gm-crypto example snippets


return [
    'default' => env('GM_CRYPTO_DRIVER', 'sm4'),
    
    'sm4' => [
        'key' => env('GM_SM4_KEY', '0123456789abcdeffedcba9876543210'),
        'mode' => env('GM_SM4_MODE', 'CBC'),
        'iv' => env('GM_SM4_IV', 'fedcba98765432100123456789abcdef'),
        'padding' => env('GM_SM4_PADDING', 'pkcs7'),
    ],
    
    'sm3' => [
        'hmac' => env('GM_SM3_HMAC', false),
        'hmac_key' => env('GM_SM3_HMAC_KEY', ''),
    ],
];

use Xgrug\LaravelGmCrypto\Facades\GmCrypto;

// 计算 SM3 哈希
$hash = GmCrypto::sm3Hash('abc');
// 输出: 66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0

// 验证哈希
$isValid = GmCrypto::sm3Verify('abc', $hash);
// 返回: true

// HMAC-SM3
$hmac = GmCrypto::sm3Hmac('abc', 'secret-key');

use Xgrug\LaravelGmCrypto\Facades\GmCrypto;

$plaintext = '0123456789abcdeffedcba9876543210';

// 加密(返回 Base64)
$encrypted = GmCrypto::sm4Encrypt($plaintext);

// 解密
$decrypted = GmCrypto::sm4Decrypt($encrypted);

// 十六进制格式
$encryptedHex = GmCrypto::sm4EncryptHex($plaintext);
$decryptedHex = GmCrypto::sm4DecryptHex($encryptedHex);

use Xgrug\LaravelGmCrypto\GmCryptoManager;

class UserController extends Controller
{
    public function __construct(
        private GmCryptoManager $crypto
    ) {}

    public function store(Request $request)
    {
        $encryptedData = $this->crypto->sm4Encrypt($request->sensitive_data);
        
        User::create([
            'data' => $encryptedData
        ]);
    }
}

use Illuminate\Database\Eloquent\Model;
use Xgrug\LaravelGmCrypto\Facades\GmCrypto;

class User extends Model
{
    // 自动加密
    public function setPhoneAttribute($value)
    {
        $this->attributes['phone'] = GmCrypto::sm4Encrypt($value);
    }

    // 自动解密
    public function getPhoneAttribute($value)
    {
        return GmCrypto::sm4Decrypt($value);
    }
}

// 使用 SM3 驱动
$sm3 = GmCrypto::driver('sm3');
$hash = $sm3->hash('数据');

// 使用 SM4 驱动
$sm4 = GmCrypto::driver('sm4');
$encrypted = $sm4->encrypt('明文');

use Xgrug\LaravelGmCrypto\SM3;

$sm3 = new SM3();

$encrypted = $sm3->hash('abc');

use Xgrug\LaravelGmCrypto\SM4;

$sm4 = new SM4([
    'key' => '自定义密钥',
    'mode' => 'ECB',
    'padding' => 'pkcs7'
]);

$encrypted = $sm4->encryptBase64('明文');

// 加密用户身份证号、手机号等敏感信息
$user->id_card = GmCrypto::sm4Encrypt($request->id_card);
$user->phone = GmCrypto::sm4Encrypt($request->phone);

// 生成数据签名
$data = ['user_id' => 123, 'amount' => 1000];
$signature = GmCrypto::sm3Hash(json_encode($data));

// 验证签名
if (GmCrypto::sm3Verify(json_encode($data), $signature)) {
    // 数据未被篡改
}

// 中间件验证请求签名
public function handle(Request $request, Closure $next)
{
    $signature = $request->header('X-Signature');
    $payload = $request->getContent();
    
    if (!GmCrypto::sm3Verify($payload, $signature)) {
        return response()->json(['error' => '签名验证失败'], 403);
    }
    
    return $next($request);
}
bash
php artisan vendor:publish --tag=gmcrypto-config