Byt*_*der 16 encryption signature gnupg self-signed
我想对我写的一些 Python 代码文件进行签名,因为它们是我的一个项目的插件模块。为了分发它,我希望用户可以确保插件经过验证和安全(因为由我或我信任的人编写)并且没有被修改。
该软件只是一个开源的爱好项目,因此我不想花任何钱购买官方证书。相反,我假设主程序始终有效并且无需额外验证即可信任。如果有人从我的 GitHub 存储库以外的任何其他位置下载它,那是他们的错。
从我读过的内容来看,签名通常是通过创建一个非对称密钥对、从代码文件中计算一个强加密哈希值(例如 SHA-512)、使用我的私钥加密哈希并将该签名存储在一个单独的文件中来完成的与原始代码文件一起提供。
然后,主程序必须使用以明文形式保存在主程序源代码中的公钥解密签名,计算代码文件的相同哈希函数,然后将其与解密后的哈希函数进行比较。如果它们匹配,则可以信任该插件。
所以现在我的问题是:
如何使用 Ubuntu 工具轻松创建强大的非对称密钥对,以及如何轻松计算文件的加密哈希值?
在脚本中自动化签名过程(始终使用相同的密钥)会很棒。
mur*_*uru 21
这个答案的大部分内容来自Arch Wiki和GnuPG 文档。这个答案中的任何建议纯粹是我的意见,并且应该加一吨盐。
打开Passwords and Keys应用程序(又名seahorse),然后单击+(或转到File -> New,或按CtrlN)以查看:
选择PGP 密钥,然后输入您的详细信息。我在冒充字节指挥官:
RSA 和 2048 位适用于大多数用途。如果您只想将其用于签名,请从下拉菜单中选择RSA(仅签名)选项,但您不需要 - 这可以使用subkeys处理。您可以留言。在您的密钥上保留到期日期也很有用。单击Create。
输入一个适当长的密码(我的意思是long,我的例子很短,IMO),然后单击Ok:
与 CLI 不同,Seahorse 似乎没有任何反馈。等待一段时间,做任何你想做的事情,同时它会收集熵并创建一个密钥。这可能需要一段时间。之后,您将在PGP 密钥部分看到它们的密钥:
要从命令行生成密钥,只需运行gpg --gen-key. 它会询问您与 GUI 相同的详细信息:
$ gpg --gen-key
gpg (GnuPG) 1.4.16; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0) 1y
Key expires at Tuesday 27 September 2016 03:45:19 PM IST
Is this correct? (y/N) y
You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and E-mail Address in this form:
"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"
Real name: Byte Commander
E-mail address: byte@command.er
Comment:
You selected this USER-ID:
"Byte Commander <byte@command.er>"
Change (N)ame, (C)omment, (E)-mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.
passphrase not correctly repeated; try again.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, use the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 186 more bytes)
.....+++++
+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, use the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 80 more bytes)
....+++++
Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 83 more bytes)
...+++++
gpg: key 8AE670A6 marked as ultimately trusted
public and secret key created and signed.
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0 valid: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u
gpg: next trustdb check due at 2016-09-26
pub 2048R/8AE670A6 2015-09-28 [expires: 2016-09-27]
Key fingerprint = 82D9 0644 B265 8E75 1E01 538B B479 3CF4 8AE6 70A6
uid Byte Commander <byte@command.er>
sub 2048R/0E2F4FD8 2015-09-28 [expires: 2016-09-27]
Run Code Online (Sandbox Code Playgroud)
请注意 GnuPG 如何告诉我们它需要更多的熵。希望海马也这样做。但话说回来,感觉 GnuPG 的行为就像 Oliver Twist。:P
现在,我们需要在那里获取我们的公钥,以便人们可以使用它来验证事物。
回到seahorse应用程序中的 PGP 密钥列表(见最后一个屏幕截图)。选择要导出的密钥,然后在Remote菜单中,选择Sync and Publish Keys:
Sync如果您尚未选择要发布到的服务器,则该按钮将被禁用。这样做,通过单击Key Servers按钮:
我选择了 Ubuntu 的服务器。
现在,您可以单击该Sync按钮并将其发布到Ubuntu 的密钥服务器(抱歉,垃圾邮件,Ubuntu!)。
使用 CLI,您需要要发布的密钥的密钥 ID。这是创建键 ( 8AE670A6)时输出的最后一行。如果您不记得它是什么,只需运行gpg --list-keys. 发布:
$ gpg --keyserver pgp.mit.edu --send-keys 8AE670A6
gpg: sending key 8AE670A6 to hkp server pgp.mit.edu
Run Code Online (Sandbox Code Playgroud)
我还不知道签署文档的便捷 GUI 方法。
创建要签名的文件后,请前往终端。尝试gpg --list-keys:
$ gpg --list-keys
/home/muru/.gnupg/pubring.gpg
---------------------------
pub 2048R/F7878B0C 2015-09-28 [expires: 2016-09-26]
uid Byte Commander <byte@command.er>
sub 2048R/345B9A4F 2015-09-28 [expires: 2016-09-26]
Run Code Online (Sandbox Code Playgroud)
您可以使用两种方法签署文件:
$ gpg --sign --output examples.sig examples.desktop
You need a passphrase to unlock the secret key for
user: "Byte Commander <byte@command.er>"
2048-bit RSA key, ID F7878B0C, created 2015-09-28
gpg: Invalid passphrase; please try again ...
You need a passphrase to unlock the secret key for
user: "Byte Commander <byte@command.er>"
2048-bit RSA key, ID F7878B0C, created 2015-09-28
Run Code Online (Sandbox Code Playgroud)
如果您在桌面会话中,很可能会看到图形密码提示。例如,在 GNOME 中:
如果收件人有您的公钥,他们可以验证它,或获取解密的内容:
$ gpg --verify examples.sig
gpg: Signature made Monday 28 September 2015 03:25:00 PM IST using RSA key ID F7878B0C
gpg: Good signature from "Byte Commander <byte@command.er>"
$ gpg --decrypt examples.sig
[Desktop Entry]
Version=1.0
Type=Link
Name=Examples
Name[aa]=Ceelallo
...
URL=file:///usr/share/example-content/
Icon=folder
X-Ubuntu-Gettext-Domain=example-content
gpg: Signature made Monday 28 September 2015 03:25:00 PM IST using RSA key ID F7878B0C
gpg: Good signature from "Byte Commander <byte@command.er>"
Run Code Online (Sandbox Code Playgroud)
您可能不想加密内容,例如,在发送邮件时。在这种情况下,请使用以下--clearsign选项:
$ gpg --clearsign examples.desktop
You need a passphrase to unlock the secret key for
user: "Byte Commander <byte@command.er>"
2048-bit RSA key, ID F7878B0C, created 2015-09-28
$ cat examples.desktop.asc
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[Desktop Entry]
Version=1.0
Type=Link
Name=Examples
Name[aa]=Ceelallo
...
URL=file:///usr/share/example-content/
Icon=folder
X-Ubuntu-Gettext-Domain=example-content
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBAgAGBQJWCRAaAAoJEGUZkqX3h4sMBWsH/1yw+G0v5Ck+T3PBS90SkvC8
5C0FJeGVr0AgYQohhsE3zEGQ7nn53N7JsvNlF6VccvN99DZIp18JbrJ+qs5hWjtg
KU/ACleR5dvVrJgfjppkuC8Q3cAudvqciKlLjA7Xycr3P49oCNCy8k/ue2TrgCvS
mMb5IS/kqpO7wrOMBAR0c/2CjQsA91S1/YK7DbuUqeNgEzW1grsI7XZPhiDGpAib
D20HWrbdLhklAEJuo1EvuOIggW6MF6ksxDoVapsUzQalD0TWEq6OnvzIS5qhITrc
XaDPQJpiHyCyINnL5aZCUwr2uon7osJ+2a8Ahp1REpzIZTdND9jA5NWSel5+yAs=
=ZrtB
-----END PGP SIGNATURE-----
Run Code Online (Sandbox Code Playgroud)
最后,对于某些文件,您不能在文档中签名。例如,打包文件或存储库的元数据都具有不容易允许嵌入签名的特定性质的内容。在这种情况下,您可以使用以下--detached-sig选项:
$ gpg --output examples.desktop.sig --detach-sign examples.desktop
You need a passphrase to unlock the secret key for
user: "Byte Commander <byte@command.er>"
2048-bit RSA key, ID F7878B0C, created 2015-09-28
$ gpg --verify examples.desktop.sig examples.desktop
gpg: Signature made Monday 28 September 2015 03:35:55 PM IST using RSA key ID F7878B0C
gpg: Good signature from "Byte Commander <byte@command.er>"
Run Code Online (Sandbox Code Playgroud)
在加密+签名和分离签名中,输出gpg是二进制的。您可以使用--armor选项 (ASCII-armoured)让 GnuPG 输出 base64 编码的数据。
要脚本签名,您可以:
stdin. 有关一些选项,请参阅此 Unix 和 Linux 帖子。使用 gpg 创建非对称密钥
gpg --gen-key
Run Code Online (Sandbox Code Playgroud)使用 gpg 对您的文件进行签名(使用您的私钥)
gpg --output foo.sig --detach-sig foo.py
Run Code Online (Sandbox Code Playgroud)测试签名文件(使用你的公钥)
gpg --verify foo.sig foo.py
Run Code Online (Sandbox Code Playgroud)
示例输出
% gpg --verify foo.sig foo.py
gpg: Signature made Mo 28 Sep 2015 12:46:04 CEST using RSA key ID 89B30DEC
gpg: Good signature from "Your Name <your.name@host.com>"
% echo "bad" >> foo.py
% gpg --verify foo.sig foo.py
gpg: Signature made Mo 28 Sep 2015 12:46:04 CEST using RSA key ID 89B30DEC
gpg: BAD signature from "Your Name <your.name@host.com>"
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
5514 次 |
| 最近记录: |