GPG從誕生開始,目的就是爲了加密而存在。到現在的 git,用 GPG 來簽名 commit
,html
能夠保證咱們提交不被篡改(固然密鑰暴露就另當別論了!)git
核心是命令行,也有對應的 GUI 客戶端,看本身喜歡了。redis
假設你已經知道 GPG 是個什麼東西,這裏只說明在 Mac下如何快速生成公鑰和密鑰。bash
我用的是 brew
包管理,能夠理解爲相似 yum
或 apt-get
這類的東西。ssh
brew install gpg gpg2
# 安裝後就能夠直接輸出 gpg 的幫助信息了
gpg --help
gpg (GnuPG) 2.2.17
libgcrypt 1.8.5
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: /Users/linqunhe/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
Syntax: gpg [options] [files]
Sign, check, encrypt or decrypt
Default operation depends on the input data
Commands:
-s, --sign make a signature
--clear-sign make a clear text signature
-b, --detach-sign make a detached signature
-e, --encrypt encrypt data
-c, --symmetric encryption only with symmetric cipher
-d, --decrypt decrypt data (default)
--verify verify a signature
-k, --list-keys list keys
--list-signatures list keys and signatures
--check-signatures list and check key signatures
--fingerprint list keys and fingerprints
-K, --list-secret-keys list secret keys
--generate-key generate a new key pair
--quick-generate-key quickly generate a new key pair
--quick-add-uid quickly add a new user-id
--quick-revoke-uid quickly revoke a user-id
--quick-set-expire quickly set a new expiration date
--full-generate-key full featured key pair generation
--generate-revocation generate a revocation certificate
--delete-keys remove keys from the public keyring
--delete-secret-keys remove keys from the secret keyring
--quick-sign-key quickly sign a key
--quick-lsign-key quickly sign a key locally
--sign-key sign a key
--lsign-key sign a key locally
--edit-key sign or edit a key
--change-passphrase change a passphrase
--export export keys
--send-keys export keys to a keyserver
--receive-keys import keys from a keyserver
--search-keys search for keys on a keyserver
--refresh-keys update all keys from a keyserver
--import import/merge keys
--card-status print the card status
--edit-card change data on a card
--change-pin change a card's PIN --update-trustdb update the trust database --print-md print message digests --server run in server mode --tofu-policy VALUE set the TOFU policy for a key Options: -a, --armor create ascii armored output -r, --recipient USER-ID encrypt for USER-ID -u, --local-user USER-ID use USER-ID to sign or decrypt -z N set compress level to N (0 disables) --textmode use canonical text mode -o, --output FILE write output to FILE -v, --verbose verbose -n, --dry-run do not make any changes -i, --interactive prompt before overwriting --openpgp use strict OpenPGP behavior (See the man page for a complete listing of all commands and options) Examples: -se -r Bob [file] sign and encrypt for user Bob --clear-sign [file] make a clear text signature --detach-sign [file] make a detached signature --list-keys [names] show keys --fingerprint [names] show fingerprints Please report bugs to <https://bugs.gnupg.org>. 複製代碼
gpg --full-generate-key
能夠很詳細的針對每一項輸入一些信息或者描述,一步一步的往下面引導,包括加密的方法等等測試
gpg --generate-key
:簡化版生成,主要輸入用戶名和郵箱以及密碼便可(最好設置),我用的就是這個ui
gpg -k
:能夠看到全部公鑰的概要信息,等同於 gpg --list-keys
gpg -K
: 能夠看到全部私鑰的概要信息,等同於 gpg --list-secret-keys
如果要對外網使用,通常都須要把公鑰發送到的 鑰匙管理局
, 能夠理解爲 CA
的中間管理機構加密
gpg --send-key F29D95D5FC2F0XXXXX # XXXXX 是我真實部分的替換
# out: gpg: sending key 4F8D4XXXX7B1F8 to hkps://keys.openpgp.org
複製代碼
GPG 簽名不是全部 GIT 服務商都提供這個支持,當前Github 和GitLab是支持的,spa
國內的Coding和 碼雲這類暫時尚未提供對應的支持。
咱們公司是部署的Gitlab Enterprise ,因此是可用的。
gpg -a --export F29D95D5FC2F05FE803AXXXXX # XXX是我替換了我本地真實的
複製代碼
通常都是我的設置裏面,無論 Github仍是 Gitlab , 如Gitlab設置。
用戶->設置->GPG密鑰
# 此處的 signingKey跟着的是你的 GPG 私鑰,能夠避免每次都手動輸入
git config --global user.signingkey <gpg-key-id>
# 提交是否強制 GPG,帶上--global 是做用全局,局部的去除--global
git config --global commit.gpgsign true
# 測試提交,只在 commit 的時侯帶上-S 參數便可,例如
git commit -m "Test GPG" -S
複製代碼
更多詳情請看此處:自定義 Git - 配置 Git
其實大致流程跟配置 ssh
密鑰差很少,只是用了不一樣的東西生成對應所需的東西。