cmbYouku_Api/app/util/sm/cmb/Sm.php

63 lines
1.7 KiB
PHP

<?php
namespace app\util\sm\cmb;
use app\util\sm\Exec;
class Sm extends Exec
{
/**
* sm4 加密
* @param string $key
* @param string $plaintext
* @return string
*/
public static function sm4Encrypt(string $key, string $plaintext): string
{
$content = base64_encode($plaintext);
$cmd = self::dirPath() . 'sm cmblife encrypt --input="' . $content . '" --sopPublicKey="' . $key . '" 2>&1';
return self::exec2($cmd);
}
/**
* sm4 解密
* @param string $key
* @param string $plaintext
* @return string
*/
public static function sm4decrypt(string $key, string $plaintext): string
{
$content = base64_encode($plaintext);
$cmd = self::dirPath() . 'sm cmblife decrypt --input="' . $content . '" --priKey="' . $key . '" 2>&1';
return self::exec2($cmd);
}
/**
* sm2 签名
* @param string $prk
* @param string $plaintext
* @return string
*/
public static function sign(string $prk, string $plaintext): string
{
$content = base64_encode($plaintext);
$cmd = self::dirPath() . 'sm cmblife sign --input="' . $content . '" --priKey="' . $prk . '" 2>&1';
return self::exec2($cmd);
}
/**
* 验证签名
* @param string $puk
* @param string $signStr
* @param string $plaintext
* @return bool
*/
public static function verify(string $puk, string $signStr, string $plaintext): bool
{
$content = base64_encode($plaintext);
$cmd = self::dirPath() . 'sm cmblife verify --input="' . $content . '" --sopPublicKey="' . $puk . '" --sign="' . $signStr . '" 2>&1';
return self::exec2($cmd) == 'ok';
}
}