php实现电子签章
PHP实现电子签章的方法
电子签章是一种通过数字技术对电子文档进行签名和认证的方式,确保文档的真实性和完整性。以下是使用PHP实现电子签章的几种方法:
使用OpenSSL扩展进行数字签名
PHP的OpenSSL扩展提供了数字签名的功能,可以用于生成和验证电子签章。
// 生成密钥对
$config = array(
"digest_alg" => "sha256",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $privateKey);
$publicKey = openssl_pkey_get_details($res)["key"];
// 签名数据
$data = "需要签名的数据";
openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA256);
// 验证签名
$valid = openssl_verify($data, $signature, $publicKey, OPENSSL_ALGO_SHA256);
if ($valid === 1) {
echo "签名验证成功";
} else {
echo "签名验证失败";
}
使用TCPDF生成PDF并添加电子签章
TCPDF是一个流行的PHP库,用于生成PDF文件,并支持添加电子签章。
require_once('tcpdf/tcpdf.php');
// 创建PDF对象
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 添加页面
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Write(0, '电子签章示例文档', '', 0, 'C', true, 0, false, false, 0);
// 添加签章
$pdf->setSignature('file://path/to/cert.pem', 'file://path/to/key.pem', 'password', '', 2, array('Name' => 'Test User', 'Location' => 'Office', 'Reason' => 'Testing', 'ContactInfo' => 'test@example.com'));
// 输出PDF
$pdf->Output('example.pdf', 'D');
使用第三方API实现电子签章
许多第三方服务提供电子签章API,可以通过PHP调用这些API实现电子签章功能。以下是使用DocuSign API的示例:
$client = new \DocuSign\eSign\Client([
'username' => 'your_username',
'password' => 'your_password',
'integrator_key' => 'your_integrator_key',
]);
$envelopeDefinition = new \DocuSign\eSign\Model\EnvelopeDefinition();
$envelopeDefinition->setEmailSubject('请签署此文档');
$envelopeDefinition->setStatus('sent');
$document = new \DocuSign\eSign\Model\Document();
$document->setDocumentBase64(base64_encode(file_get_contents('document.pdf')));
$document->setName('example.pdf');
$document->setDocumentId('1');
$envelopeDefinition->setDocuments([$document]);
$signer = new \DocuSign\eSign\Model\Signer();
$signer->setEmail('signer@example.com');
$signer->setName('Signer Name');
$signer->setRecipientId('1');
$signHere = new \DocuSign\eSign\Model\SignHere();
$signHere->setDocumentId('1');
$signHere->setPageNumber('1');
$signHere->setXPosition('100');
$signHere->setYPosition('100');
$tabs = new \DocuSign\eSign\Model\Tabs();
$tabs->setSignHereTabs([$signHere]);
$signer->setTabs($tabs);
$recipients = new \DocuSign\eSign\Model\Recipients();
$recipients->setSigners([$signer]);
$envelopeDefinition->setRecipients($recipients);
$envelopesApi = new \DocuSign\eSign\Api\EnvelopesApi($client);
$result = $envelopesApi->createEnvelope('your_account_id', $envelopeDefinition);
使用PHP的GD库生成图像签章
如果需要将签章以图像形式嵌入文档,可以使用PHP的GD库生成签章图像。
// 创建图像
$image = imagecreatetruecolor(200, 100);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// 填充背景
imagefilledrectangle($image, 0, 0, 200, 100, $white);
// 添加文字
imagettftext($image, 20, 0, 10, 50, $black, 'path/to/font.ttf', '电子签章');
// 保存图像
imagepng($image, 'signature.png');
imagedestroy($image);
注意事项
- 电子签章涉及法律效力,确保符合当地法律法规。
- 使用强加密算法(如RSA 2048位或更高)保证安全性。
- 妥善保管私钥,防止泄露。
- 考虑使用时间戳服务(TSA)为签章添加时间戳。
以上方法可以根据具体需求选择使用,或组合多种技术实现更完善的电子签章解决方案。




