Hướng dẫn e signature in php - chữ ký điện tử trong php

Một XML được ký bằng cú pháp xmldsig có 3 phần quan trọng:

  1. Signature -> KeyInfo chứa thông tin về khóa công khai được lấy từ khóa cá nhân được sử dụng để ký dữ liệu
  2. Signature -> SignedInfochứa dữ liệu sẽ được ký bằng khóa cá nhân được đề cập ở trên; các dữ liệu chứa thông tin về cách thức xác minh phải được tính toán, chẳng hạn như: CanonicalizationMethod, SignatureMethod,Reference
  3. Signature -> SignatureValuechứa giá trị của chữ ký được tạo bằng cách ký Signature -> SignedInfobằng khóa cá nhân

Về mặt lý thuyết, đây là cách mã sẽ tìm kiếm thuật toán rsa-sha1 [được chỉ định bởi Signature -> SignedInfo -> SignatureMethod], có phương pháp chuẩn hóa sau: Quy tắc hóa XML độc quyền 1.0 [bỏ qua nhận xét] và chứng chỉ x509 được cung cấp:

$xmlDoc = new DOMDocument[];
$xmlDoc->loadXML[$xmlString];

$xpath = new DOMXPath[$xmlDoc];
$xpath->registerNamespace['secdsig', '//www.w3.org/2000/09/xmldsig#'];

// fetch Signature node from XML
$query = ".//secdsig:Signature";
$nodeset = $xpath->query[$query, $xmlDoc];
$signatureNode = $nodeset->item[0];

// fetch SignedInfo node from XML
$query = "./secdsig:SignedInfo";
$nodeset = $xpath->query[$query, $signatureNode];
$signedInfoNode = $nodeset->item[0];

// canonicalize SignedInfo using the method descried in
// ./secdsig:SignedInfo/secdsig:CanonicalizationMethod/@Algorithm
$signedInfoNodeCanonicalized = $signedInfoNode->C14N[true, false];

// fetch the x509 certificate from XML
$query = 'string[./secdsig:KeyInfo/secdsig:X509Data/secdsig:X509Certificate]';
$x509cert = $xpath->evaluate[$query, $signatureNode];
// we have to re-wrap the certificate from XML to respect the PEM standard
$x509cert = "-----BEGIN CERTIFICATE-----\n"
    . $x509cert . "\n"
    . "-----END CERTIFICATE-----";
// fetch public key from x509 certificate
$publicKey = openssl_get_publickey[$x509cert];

// fetch the signature from XML
$query = 'string[./secdsig:SignatureValue]';
$signature = base64_decode[$xpath->evaluate[$query, $signatureNode]];

// verify the signature
$ok = openssl_verify[$signedInfoNodeCanonicalized, $signature, $publicKey];    

Lib này thực hiện rất tốt việc triển khai xmldsig trong php: xmlseclibs ; có thể tìm thấy ví dụ về cách xác minh xmldsig tại đây: //github.com/roctureshards/xmlseclibs/blob/master/tests/xmlsec-verify.phpt . Thư viện này cũng xác nhận giá trị thông báo từ Signature -> SignedInfo -> Referencebước mà tôi đã bỏ qua ở trên.

20 hữu ích 3 bình luận chia sẻ 3 bình luận chia sẻ

Hướng dẫn dùng x 0 trong PHP

Trang chủHướng dẫn họcHọc PHPKiểu dữ liệu trong PHPĐịnh nghĩa và cách dùngPHP IntegerKiểu dữ liệu integer [viết tắt là int] là kiểu dạng số nguyênkhông là ...

Get oauth2 token in php

I need to connect with an API that is using oAuth2. I have never used oAuth2 before and im not really sure how to. The provider is giving this information:Obtaining an access token is done by sending ...

Php if shorthand without else

Im a fan if the short if-version, example:[$thisVar == $thatVar ? doThis[] : doThat[]]; Id like to cut out the else-statement though, example:[$thisVar == $thatVar ? doThis[]]; However, it wont ...

How to comment out php in html

In the middle my HTML code, I have a line of PHP. now, I want to make PHP line as a comment. I tried to ues but it seems not work for PHP.What should I do?Thanks asked Dec 30, 2010 ...

Hướng dẫn php shorthand if

Update for PHP 7 [thanks shock_gone_wild]Nội dung chínhPHP Ternary OperatorTernary SyntaxPHP Ternary Operator ExamplePHP Ternary Operator with Isset[] ExamplePHP Null Coalescing OperatorPHP Null ...

What are new features in php 7?

Scalar type declarations Scalar type declarations come in two flavours: coercive [default] and strict. The following types for parameters can now be enforced [either coercively or strictly]: strings ...

Hướng dẫn lcm in php

Hướng dẫn dùng prime loops trong PHPNội dung chínhTrong PHP hỗ trợ bốn loại vòng lặp khác nhau:2. Vòng lặp While trong PHP – While loop3. Vòng lặp do…while trong ...

Hướng dẫn restart php macos

I successfully installed homebrew by following the instructions of the official website [//brew.sh/]./bin/bash -c $[curl -fsSL ...

Hướng dẫn php string to int

Trong bài này, chúng ta sẽ tìm hiểu kỹ về các kiểu dữ liệu số [number] trong PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập ...

Hướng dẫn dùng period date trong PHP

Blog Tin tức 26/07/2021 03:01Ngày tháng là một phần của cuộc sống hàng ngày, do vậy việc xử lý ngày tháng cực kỳ quan trọng khi bạn xử lý các bài viết và ...

Hướng dẫn dùng email string trong PHP

Sử dụng hàm mail[]Gửi HTML trong mailGửi đính kèmGửi mail trong PHP với hàm mail[]PHP có hàm mail[] dùng để gửi email. Để gửi mail gọi hàm với cú pháp như ...

Hướng dẫn how to use firephp

FirePHP Cross-Browser Devtools Extension for PHP Development Why use FirePHP?FirePHP enables the logging of PHP variables to a browser devtools panel. As a page loads in the browser the ...

How to rotate image in php?

[PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8]imagerotate — Rotate an image with a given angleDescriptionimagerotate[ GdImage ...

Hướng dẫn gc to php

Memory leaks can happen in any language, including PHP. These memory leaks may happen in small increments that take time to accumulate, or in larger jumps that manifest quickly. Either way, if ...

Hướng dẫn php base64 encode image

Chuyển đến nội dungCó nhiều giải pháp để mã hoá hình ảnh, Encode Base64 là một trong số đó. Khi chuyển hình ảnh về dạng này ảnh được hiển thị trực ...

Hướng dẫn dùng get define trong PHP

Define - Defined và Const là gì trong PHP - Tìm hiểu về hằng số trong PHP, giống và khác nhau giữa Define - defined và const trong php. Bài viết này sẽ làm rõ các vấn ...

Hướng dẫn dùng mangodb trong PHP

Bài trước, mình đã giới thiệu với mọi người cách update documnent trong mongodb rồi. Tiếp tục với bài này mình sẽ giới thiệu với mọi người cách query lấy ...

Hướng dẫn dùng mkdir man trong PHP

Định nghĩa.Cú pháp.Cú pháp:Trong đó.Giá trị trả về.Ví dụ.Ví dụ 1.code.Ví dụ # 2 mkdir [] sử dụng tham số recursive Errors/Exceptions.Ghi chú.Hàm liên ...

Hướng dẫn date_format php

Cú phápDưới đây là cú pháp của hàm date_format[] trong PHP:string date_format [ DateTime $object, string $format ] string DateTime::format [ string $format ] Định nghĩa và cách ...

Hướng dẫn phpjabbers appointment scheduler

*** Important *** The knowledgebase below is for the previous version for Appointment Scheduler script. Weve recently launched a new version based on our latest framework and are currently working ...

Hướng dẫn dùng rounding value trong PHP

Hàm Round[] trong php giúp chúng ta có thể làm tròn số thực theo ý muốn của mình. Cú phápround[$val, $precision]Input: $val: Số thực cần làm tròn$precision: Độ chính ...

Hướng dẫn pspell php

Ive checked sources of PHP versions 5.3.27 and 5.5.4Both include pspell:php-5.5.4/ext/pspellphp-5.3.27/ext/pspellNevertheless, if you use precompiled versions you may have a different impression ...

Package php dom is a virtual package provided by

Im trying to install PHP extension php-ctype. When I doapt-get install php-ctype it returnsReading package lists... Done Building dependency tree Reading state information... Done Package php-ctype ...

Hướng dẫn dùng config ini trong PHP

Khi lập trình php, việc cấu hình file php.ini ảnh hưởng nhất định đến cách bạn viết code. Ngoài ra nó góp phần quan trọng khi bạn chạy các ứng dụng trên ...

Hướng dẫn ob_start [] trong php

Ob_start trong php là một trong những từ khóa được gg search nhiều nhất về chủ đề ob_start trong php. Trong bài viết này, cachthietkeweb.vn sẽ viết bài viết tổng ...

Hướng dẫn does exist in php?

Hàm file_exists[] sẽ kiểm tra xem file hoặc thư mục có tồn tại hay không.Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.Cú ...

How to add one month to current date in php?

Im coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time[] variable works, but I am not sure how to +1 month onto that? Any ...

Hướng dẫn dùng expression trong PHP

Regular Expression được dùng để xử lý chuỗi thông qua biểu thức riêng của nó. Để các biểu thức này có thể hoạt động được bạn cần tuân thủ nhiêm ...

Hướng dẫn php error_log stdout

Hướng dẫn dùng 64encode trong PHPChuyển đến nội dungCó nhiều giải pháp để mã hoá hình ảnh, Encode Base64 là một trong số đó. Khi chuyển hình ảnh về dạng ...

What are the main features of oops in php?

by Vincy. Last modified on July 1st, 2022.PHP supports both procedural and Object-Oriented programming. Object-Oriented methodologies will be useful and effective while working with complex or ...

Get first key of array php

[PHP 7 >= 7.3.0, PHP 8]array_key_first — Gets the first key of an arrayDescriptionarray_key_first[array $array]: int|string|nullParameters array An array. Return Values Returns the first key of ...

Variable and constants in php

View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticlePHP Constants: PHP Constants are the identifiers that remain the same. Usually, it does not change ...

Hướng dẫn dùng define classes trong PHP

Thuộc tính [property] và phương thức [method] là hai thành phần cơ bản tạo nên một class trong PHP. Trong đó, thuộc tính là thành phần chứa dữ liệu trong class ...

Bài Viết Liên Quan

Chủ Đề