PHP code example of life2016 / guomi
1. Go to this page and download the library: Download life2016/guomi 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/ */
life2016 / guomi example snippets
function __construct($formatSign='hex', $randFixed = true) {
// 注意: 这个randFixed尽量取false, 如需要固定,请重新生成$foreignkey密码对
$this->adapter = RtEccFactory::getAdapter();
$this->generator = RtEccFactory::getSmCurves()->generatorSm2();
if(in_array($formatSign,$this->arrFormat)){
$this->formatSign = $formatSign;
} else {
$this->formatSign = 'hex';
}
if(!$randFixed){
$this->useDerandomizedSignatures = false;
$this->useDerandomizedEncrypt = false;
}
}
private function Reset() //注意,加密使用无符号的数组转换,以便与硬件相一致
{
$this->sm3keybase = new SM3Digest();
$this->sm3c3 = new SM3Digest();
$p = array();
$gmp_x = $this->p2->GetX();
$this->sm3keybase->BlockUpdate(array(4), 0, 1); // 添加: 这里添加个04
$x = Hex2ByteBuf::ConvertGmp2ByteArray($gmp_x);
$this->sm3keybase->BlockUpdate($x, 0, sizeof($x));
if($x[0]>127){ // >7F
$this->sm3c3->BlockUpdate(array(0), 0, 1); // 这里做了个判定,补00
}
$this->sm3c3->BlockUpdate($x, 0, sizeof($x));
$gmp_y = $this->p2->GetY();
$y = Hex2ByteBuf::ConvertGmp2ByteArray($gmp_y);
$this->sm3keybase->BlockUpdate($y, 0, sizeof($y));
$this->ct = 1;
$this->NextKey();
}
public function Dofinal()
{
$c3 = array();
$gmp_p = $this->p2->GetY();
$p = Hex2ByteBuf::ConvertGmp2ByteArray($gmp_p);
///========以下添加的======
if($p[0]>127){ // >7F
$this->sm3c3->BlockUpdate(array(0), 0, 1); // 这里做了个判定,补00
}
///========以上添加的======
$this->sm3c3->BlockUpdate($p, 0, sizeof($p));
$this->sm3c3->DoFinal($c3, 0);
$this->Reset();
return $c3;
}