RSA

相信很多同学还在使用 RSA 算法用于生成 SSH 公钥,可能还会纠结选择多少位才足够安全,一般建议是 4096 bits:

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

这样我们的公钥(public key) 就会非常长:

1
2
3
cat test_rsa_4096.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCXlH8OxqMFhv2+En10yV2ZorDzRFXQm9pPuWQ8G5iu+cUpyhwDoKnd+l6PCZTrCgcVJgLSsVAVbZ3CK6Qnoj3TDQl4yaj90UasmivWM2INc2hObr5P2y2AqWnnZBXmxpoUGZPz/9323JalC+m/EwXNcdrC5JzgD083BC0ykfB801vcAzrZwsnbKfCUsGfUNP9mco3+hFwTqgfJxEvmI3X6hbGIGY1d2QbGMLrs3JYVsfRzJDjFaYOSwXZR6pM5uUCKENt9hOmVUZfuZqvlzLZX95yc53a6qNgOJhzaFZYz3wD2gY0dNp1boGnAtXsLqEnqtm9skp05iMuT01B9WrKEOZG5rsRZDh3bYXJ8ZP0lO/RbStuBczd8ZgObb32NfUyHG2JObDpm9mjsvWZqJxJbT5l/6vMXu8hQ6ikDrf6R33PRcRdbUIrAOpDUrfBxjkUonxjqqEbHhpcAlMWNJ4qcjtjvSnLOhH9GBn5KCnFJ7VIbyXc+Gj9AAp9xuV/9jv1R7CathkS2QrC5s9pFY3I24mFevpkioEeJYPAYUTuFBenWg5MdFK99FYO44wjmFa/RxwEQtYFXV+RybTJTC0eDpjK1u3w7LVm2JjEVoSfOJIKt9yZQn5Fm0kmueBz5aQ4CzZNoZBMKr7TT0dX9cJoANzd19uM4uCV6HRVJmQyz4Q== your_email@example.com

Ed25519

其实,有一些更先进的算法,比 RSA 更安全,公钥更简短,随着区块链的普及,它们正慢慢被更多的人接受,比如 Ed25519,虽然它只有 256 bit,但安全性比 RSA 3072 还要高。

Ed25519 SSH Key 生成:

1
ssh-keygen -t ed25519 -C "your_email@example.com"

公钥:

1
2
3
cat test_ed25519.pub 
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGcMXqCXtcjny9gXV1NDmwArHy0AgJs+R7N6XpOutviw your_email@example.com

Github 已经默认推荐大家使用 Ed25519: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Ed25519 是一个椭圆曲线,非常优美,安全性经过数学严格证明:

By Deirdre Connolly in [State of the Curve] (2016)

这里有一份目前使用 Ed25519 的列表: https://ianix.com/pub/ed25519-deployment.html