landscape

landscape

Deep think, Easy go.

11 Feb 2022

GPGキー関連のコマンド

いつもコマンドを忘れるのでまとめる。

プライマリキー生成

ed25519でキーを生成には--expertオプションが必要。

プライマリキーは無期限にし、オフラインで管理する。

$ gpg --expert --full-gen-key

gpg (GnuPG) 2.2.20; Copyright (C) 2020 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)
   (7) DSA (set your own capabilities)
   (8) RSA (set your own capabilities)
   (9) ECC and ECC
  (10) ECC (sign only)
  (11) ECC (set your own capabilities)  <--- HERE
  (13) Existing key
  (14) Existing key from card
Your selection? 11

Possible actions for a ECDSA/EdDSA key: Sign Certify Authenticate
Current allowed actions: Sign Certify

   (S) Toggle the sign capability
   (A) Toggle the authenticate capability
   (Q) Finished

Your selection?
Please select which elliptic curve you want:
   (1) Curve 25519   <--- HERE
   (3) NIST P-256
   (4) NIST P-384
   (5) NIST P-521
   (6) Brainpool P-256
   (7) Brainpool P-384
   (8) Brainpool P-512
   (9) secp256k1
Your selection? 1
...

シークレットキーIDを確認する

$ gpg --list-secret-keys --keyid-format=long

-----------------------------------
pub   ed25519/<GPG_KEY_ID_1> 2021-10-24 [SC]
      09A70C02424CE85007F433834902D487FA3A276F
uid                 [  不明  ] jlandowner ...
sub   cv25519/<GPG_SUBKEY_ID_1> 2021-10-24 [E]

サブキーを生成する

$ gpg --export --edit-key <GPG_KEY_ID_1>

gpg> addkey

プロンプトに従ってサブキーを作成する。

その後作成したサブキーを確認する

$ gpg --list-secret-keys --keyid-format=long

-----------------------------------
pub   ed25519/<GPG_KEY_ID_1> 2021-10-24 [SC]
      09A70C02424CE85007F433834902D487FA3A276F
uid                 [  不明  ] jlandowner ...
sub   cv25519/<GPG_SUBKEY_ID_1> 2021-10-24 [E]
sub   ed25519/<GPG_SUBKEY_ID_2> 2022-02-11 [S] <--- ADDED

Gitのコミットに署名をつける

gitに設定する

# コミット時にサインする設定
git config --global commit.gpgsign true

# シークレットキーIDの設定
git config --global user.signingkey <GPG_SUBKEY_ID_2>

# 環境変数設定
export GPG_TTY=$(tty)

# bashに環境変数設定を入れておく
[[ -r ~/.bash_profile ]] && echo 'export GPG_TTY=$(tty)' >> ~/.bash_profile || echo 'export GPG_TTY=$(tty)' >> ~/.profile

# zshの場合はこっち
[[ -r ~/.zshrc ]] && echo 'export GPG_TTY=$(tty)' >> ~/.zshrc || echo 'export GPG_TTY=$(tty)' >> ~/.zprofile

Githubに公開鍵を設定する

ASCII armor 形式で GPG キーを出力する

gpg --armor --export <GPG_KEY_ID>

出力されたキー情報をGithubアカウントに設定する。

MacのGPG Tool

Macではデフォルトでgpgtoolがインストールされていないので、AppleのサポートページにあるようにGPGTOOLをインストールする。

https://support.apple.com/ja-jp/HT201214