search
HomeBackend DevelopmentPHP TutorialDetailed explanation of how openssl uses DSA algorithm to generate signature examples_PHP tutorial

Detailed explanation of how openssl uses DSA algorithm to generate signature examples_PHP tutorial

Jul 13, 2016 am 10:48 AM
dsaopensslintroduceusebased onExamplearticlegeneratesignalgorithmDetailed explanation

The article introduces you to an example of using the DSA algorithm to generate a signature based on openssl. The method of generating a signature is very simple, but we need to understand the middle principle, which is more complicated. Let's take a look.

Command:

openssl> dgst -dss1 -sign C.pri -out signature.bin s.txt

Explanation
C.pri is the private key file generated by the DSA algorithm
s.txt is the original text for making the signature
signature.bin is the generated signature file

You can use the following method to view the signature content in php

# Use the sha1 algorithm to sign the file file.txt and output it to the file rsasign.bin # The private key of the signature is the file rsaprivate.pem
The code is as follows
 代码如下 复制代码
echo bin2hex(file_get_contents('signature.bin'));
?>
Copy code


echo bin2hex(file_get_contents('signature.bin'));
?>

Reference content
Message Digest Algorithm
Supported algorithms include: MD2, MD4, MD5, MDC2, SHA1 (sometimes called DSS1), RIPEMD-160. SHA1 and RIPEMD-160 produce 160-bit hashes, the others produce 128-bit hashes. Unless for compatibility reasons, it is recommended to use SHA1 or RIPEMD-160.
Except for RIPEMD-160, which requires the rmd160 command, other algorithms can be executed with the dgst command.
OpenSSL's handling of SHA1 is a bit strange, and sometimes it must be referred to as DSS1.
In addition to calculating hash values, the message digest algorithm can also be used to sign and verify signatures. When signing, the private key generated by DSA must be matched with DSS1 (ie SHA1). For private keys generated by RSA, any message digest algorithm can be used.

# Message Digest Algorithm Application Example
# Use SHA1 algorithm to calculate the hash value of file file.txt and output it to stdout
$ openssl dgst -sha1 file.txt
# Use the SHA1 algorithm to calculate the hash value of the file file.txt and output it to the file digest.txt
$ openssl sha1 -out digest.txt file.txt
# Use the DSS1 (SHA1) algorithm to sign the file file.txt and output it to the file dsasign.bin
# The private key of the signature must be generated by the DSA algorithm and stored in the file dsakey.pem
$ openssl dgst -dss1 -sign dsakey.pem -out dsasign.bin file.txt
# Use dss1 algorithm to verify the digital signature dsasign.bin of file.txt,
# The private key to be verified is the file dsakey.pem

generated by the DSA algorithm $ openssl dgst -dss1 -prverify dsakey.pem -signature dsasign.bin file.txt
generated by the RSA algorithm $ openssl sha1 -sign rsaprivate.pem -out rsasign.bin file.txt # Use sha1 algorithm to verify the digital signature of file.txt rsasign.bin,

# The verified public key is rsapublic.pem

generated by RSA algorithm $ openssl sha1 -verify rsapublic.pem -signature rsasign.bin file.txt http://www.bkjia.com/PHPjc/632818.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632818.html
TechArticle
The article introduces an example of using the DSA algorithm to generate a signature based on openssl. The method of generating a signature is very simple. We need to understand the middle The principle is more complicated, let’s take a look together. ...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools