(7) openssl dgst(產生和驗證數位簽章)

來源:互聯網
上載者:User

標籤:作用   algo   工具   ati   bsp   標準輸出   資訊   加密工具   stdin   

該偽命令是單向加密工具,用於組建檔案的摘要資訊,也可以進行數位簽章,驗證數位簽章

首先要明白的是,數位簽章的過程是計算出數字摘要,然後使用私密金鑰對數字摘要進行簽名,而摘要是使用md5、sha512等演算法計算得出的(而通過私密金鑰密碼編譯摘要資訊得到數位簽章),理解了這一點,openssl dgst命令的用法就完全掌握了。

openssl dgst [-md5|-sha1|...] [-hex | -binary] [-out filename] [-sign filename] [-passin arg] [-verify filename] [-prverify filename] [-signature filename]   [file...]

選項說明:

file...:指定待簽名的檔案。

-hex:以hex格式輸出數字摘要。如果不以-hex顯示,簽名或驗證簽名時很可能亂碼。

-binary:以二進位格式輸出數字摘要,或以二進位格式進行數位簽章。這是預設格式。

-out filename:指定輸出檔案,若不指定則輸出到標準輸出。

-sign filename:使用filename中的私密金鑰對file數位簽章。簽名時絕對不能加-hex等格式的選項,否則驗證簽名必失敗,親測。

 -signature filename:指定待驗證的簽名檔案。

-verify filename:使用filename中的公開金鑰驗證簽名。

-prverify filename:使用filename中的私密金鑰驗證簽名。

-passin arg:傳遞解密密碼。若驗證簽名時實用的公開金鑰或私密金鑰檔案是被加密過的,則需要傳遞密碼來解密。

支援如下幾種單向密碼編譯演算法,即簽名時使用的hash演算法。

-md4            to use the md4 message digest algorithm

-md5            to use the md5 message digest algorithm

-ripemd160      to use the ripemd160 message digest algorithm

-sha            to use the sha message digest algorithm

-sha1           to use the sha1 message digest algorithm

-sha224         to use the sha224 message digest algorithm

-sha256         to use the sha256 message digest algorithm

-sha384         to use the sha384 message digest algorithm

-sha512         to use the sha512 message digest algorithm

-whirlpool      to use the whirlpool message digest algorithm

注意:openssl dgst -md5和openssl md5的作用是一樣的,其他單向密碼編譯演算法也一樣,例如openssl dgst -sha等價於openssl sha。

例如:

(1).隨機產生一段摘要資訊(即單向加密)

[[email protected] ~]# echo 123456 | openssl md5(stdin)= f447b20a7fcbf53a5d5be013ea0b15af

(2).對/tmp/a.txt檔案產生MD5和sha512摘要資訊。

[[email protected] ~]# openssl dgst -md5 rsa.pub
MD5(rsa.pub)= 0803103e6685ad6ab4b37402a680e205

[[email protected] ~]# openssl md5 rsa.pub
MD5(rsa.pub)= 0803103e6685ad6ab4b37402a680e205

[[email protected] ~]# openssl sha512 rsa.pub
SHA512(rsa.pub)= aed7de92f0f4a3545a1afb29fbac75e16577e3edbb65f2a526ceedc28663f56b4b488ccf7b809f1e22369b7f2c5438b90daee70e3af8126a471acbd92278ddd7
[[email protected] ~]# openssl dgst -sha512 rsa.pub
SHA512(rsa.pub)= aed7de92f0f4a3545a1afb29fbac75e16577e3edbb65f2a526ceedc28663f56b4b488ccf7b809f1e22369b7f2c5438b90daee70e3af8126a471acbd92278ddd7

(3).產生一個私密金鑰genrsa.pri,然後使用該私密金鑰對/tmp/a.txt檔案簽名。使用-hex選項,否則預設輸出格式為二進位會亂碼。

[[email protected] tmp]# openssl genrsa -out genrsa.pri[[email protected] tmp]# openssl dgst -md5 -hex -sign genrsa.pri a.txtRSA-MD5(a.txt)= 7a6930b06dc6980d1a1fee872df5c8c9c887633c8e2f8b951d40aff4e934b206423914129f66651344859981e33c448f3a61274bded973b387065e9c7909bfcfc1d844e35af1453cc248d58170eb27e948a8de862f21a2b7ee34f512b3cc3cb44537e26c62a409e211320b87f74a8fa5ec1bcc790a7c13ffaa9df9aa8c5ddb64

如果要驗證簽名,那麼這個產生的簽名要儲存到一個檔案中,且一定不能使用"-hex"選項,否則驗證簽名必失敗。以下分別產生使用和不使用hex格式的簽名檔案以待驗證簽名測試。

[[email protected] tmp]# openssl dgst -md5 -hex -out md5_hex.sign    -sign genrsa.pri   a.txt               [[email protected] tmp]# openssl dgst -md5      -out md5_nohex.sign  -sign genrsa.pri   a.txt

(4).驗證簽名。驗證簽名的過程實際上是對待驗證檔案新產生簽名,然後與已有簽名檔案進行比對,如果比對結果相同,則驗證通過。所以,在驗證簽名時不僅要給定待驗證的簽名檔案,也要給定相同的演算法,相同的私密金鑰或公開金鑰檔案以及待簽名檔案以產生新簽名資訊。

以下先測試以私密金鑰來驗證數位簽章檔案。

首先對未使用hex格式的簽名檔案md5_nohex.sign進行驗證。由於產生md5_nohex.sign時使用的是md5演算法,所以這裡必須也要指定md5演算法。

[[email protected] tmp]# openssl dgst -md5 -prverify genrsa.pri -signature md5_nohex.sign a.txtVerified OK

再對使用了hex格式的簽名檔案md5_hex.sign進行驗證,不論在驗證時是否使用了hex選項,結果都是驗證失敗。

[[email protected] tmp]# openssl dgst -md5 -prverify genrsa.pri -signature md5_hex.sign a.txt  Verification Failure[[email protected] tmp]# openssl dgst -md5 -hex -prverify genrsa.pri -signature md5_hex.sign a.txtVerification Failure

再測試使用公開金鑰來驗證數位簽章。

[[email protected] tmp]# openssl rsa -in genrsa.pri -pubout -out rsa.pub[[email protected] tmp]# openssl dgst -md5 -verify rsa.pub -signature md5_nohex.sign a.txtVerified OK

(7) openssl dgst(產生和驗證數位簽章)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.