0. 產生測試用的文件
echo "hello world" >hello.txt
1. 做一下sha256 hash
$ openssl dgst -sha256 hello.txt
SHA256(hello.txt)= a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
或者,來個二進制輸出
$ openssl dgst -sha256 -binary -out hello.sha256 hello.txt
$ od -v -An -t x1 hello.sha256
a9 48 90 4f 2f 0f 47 9b 8f 81 97 69 4b 30 18 4b
0d 2e d1 c1 cd 2a 1e c0 fb 85 d2 99 a1 92 a4 47
2. 創建rsa密鑰
$ openssl genrsa -out key.pri -f4 2048
Generating RSA private key, 2048 bit long modulus
......................................................+++
..........................................+++
e is 65537 (0x10001)
3. 看一下密鑰內容
$ openssl rsa -inform PEM -in key.pri -text
....
4. 導出公鑰
$ openssl rsa -inform PEM -outform PEM -in key.pri -out key.pub -pubout
writing RSA key
5. 查看公鑰
$ openssl rsa -inform PEM -in key.pub -pubin -text
....
6. hash并用私鑰簽名
$ openssl dgst -sha256 -out hello.sig -sign key.pri -keyform PEM hello.txt
$ od -v -An -t x1 hello.sig
....
7. 公鑰校驗
$ openssl dgst -sha256 -keyform PEM -verify key.pub -signature hello.sig hello.txt
Verified OK
上面這個命令只能輸出"Verification OK"或者"Verification Failure"。如果想看到恢復的數據,可以用下面的命令:
$ openssl rsautl -in hello.sig -inkey key.pub -pubin -verify -hexdump
0000 - 30 31 30 0d 06 09 60 86-48 01 65 03 04 02 01 05 010...`.H.e.....
0010 - 00 04 20 a9 48 90 4f 2f-0f 47 9b 8f 81 97 69 4b .. .H.O/.G....iK
0020 - 30 18 4b 0d 2e d1 c1 cd-2a 1e c0 fb 85 d2 99 a1 0.K.....*.......
0030 - 92 a4 47 ..G
或者不去掉pkcs填充的字節:
$ openssl rsautl -in hello.sig -inkey key.pub -pubin -verify -hexdump -raw
0000 - 00 01 ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0010 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0020 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0030 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0040 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0050 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0060 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0070 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0080 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
0090 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
00a0 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
00b0 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff ................
00c0 - ff ff ff ff ff ff ff ff-ff ff ff ff 00 30 31 30 .............010
00d0 - 0d 06 09 60 86 48 01 65-03 04 02 01 05 00 04 20 ...`.H.e.......
00e0 - a9 48 90 4f 2f 0f 47 9b-8f 81 97 69 4b 30 18 4b .H.O/.G....iK0.K
00f0 - 0d 2e d1 c1 cd 2a 1e c0-fb 85 d2 99 a1 92 a4 47 .....*.........G
或者來個二進制的文件:
$ openssl rsautl -in hello.sig -out hello.rec -inkey key.pub -pubin -verify
$ od -v -An -t x1 hello.rec
30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05
00 04 20 a9 48 90 4f 2f 0f 47 9b 8f 81 97 69 4b
30 18 4b 0d 2e d1 c1 cd 2a 1e c0 fb 85 d2 99 a1
92 a4 47
最后32個字節是hello.txt的sha256值,前面19個字節是按照pkcs規范添加的用來標示hash算法的數據。
有時候你拿到的公鑰可能不是PEM格式的,而只是一個n和e,openssl的標準命令是不能直接用的,也沒有提供轉換命令(存疑,反正我是沒發現比自己寫個小程序更簡單的辦法)。這時候就需要用openssl庫自己寫個小程序來做轉換了。好在這個程序寫起來不麻煩,用到的幾個函數在下面幾個頭文件中定義:
openssl/bn.h、openssl/rsa.h、openssl/pem.h
|