LoginSignup
0
4

More than 3 years have passed since last update.

GitHubにGPG鍵を登録してみた

Last updated at Posted at 2019-11-08

はじめに

探せば似たような記事がいっぱい出てきそうな内容だけど、GitHubの
Managing commit signature verification のとおりにGPG鍵を登録してみた記録

実行環境

  • Homebrewでインストールしたgnupg
$ brew info gnupg
gnupg: stable 2.2.17 (bottled)
GNU Pretty Good Privacy (PGP) package
https://gnupg.org/
/usr/local/Cellar/gnupg/2.2.17 (134 files, 11MB) *
  Poured from bottle on 2019-08-02 at 10:40:23
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/gnupg.rb
  • gnupgのバージョンは2.2系
$ gpg --version
Warning: Failed to set locale category LC_NUMERIC to en_JP.
Warning: Failed to set locale category LC_TIME to en_JP.
Warning: Failed to set locale category LC_COLLATE to en_JP.
Warning: Failed to set locale category LC_MONETARY to en_JP.
Warning: Failed to set locale category LC_MESSAGES to en_JP.
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/shotakaha/.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
  • なんかロケールっぽいのに関するWarning出てしまうけれど、気にせず進めます

GitHubのメールアドレスを確認した

Verifying your email address

  • UnverifiedとラベルされていたアドレスはSend verification emailを押した

既存のGPG鍵があるかを確認した

Checking for existing GPG keys

$ gpg --list-secret-keys --keyid-format LONG
gpg: directory '/Users/shotakaha/.gnupg' created
gpg: keybox '/Users/shotakaha/.gnupg/pubring.kbx' created
gpg: /Users/shotakaha/.gnupg/trustdb.gpg: trustdb created
  • 「前にやったことあったかも?」という自分の記憶はあまり頼りにならないので確認してみた
  • 初めてだったみたいで、必要なディレクトリ、キーボックスなどが自動で作成された

新しいGPG鍵を生成した

Generating a new GPG key

$ gpg --full-generate-key
gpg (GnuPG) 2.2.17; Copyright (C) 2019 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.
  • gpg --full-generate-keyを実行すると、ターミナル上で下記のダイアログが開始されるので、指示通りに入力した

Q1. 鍵の種類はどうしますか?

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? 1
  • RSA and RSA (default) を選択
  • Enterを押すだけでもよかった

Q2. 鍵のサイズはどうしますか?

RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
Requested keysize is 4096 bits       
  • 最大値4096を入力した

Q3. 鍵の期限はどうしますか?

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) 
Key does not expire at all
  • 0: 無期限 を選んだ

Q4. 入力内容に間違いはないですか?

Is this correct? (y/N) y

Q5. ユーザー情報を教えて下さい

GnuPG needs to construct a user ID to identify your key.

Real name: 名前
Email address: メールアドレス
Comment: コメント        
You selected this USER-ID:
    "名前 (コメント) <メールアドレス>"
  • 必要な情報を入力
  • メールアドレスはverifiedなものを入力する
  • コメントは入力しなくてもOK

Q6. 鍵の生成しますね

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O

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, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.

gpg: key 鍵のID marked as ultimately trusted
gpg: directory '/Users/shotakaha/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/Users/shotakaha/.gnupg/openpgp-revocs.d/鍵のID.rev'
public and secret key created and signed.

pub   rsa4096 2019-11-08 [SC] 鍵のID
uid                      名前 (コメント) <メールアドレス>
sub   rsa4096 2019-11-08 [E]
  • ユーザ情報に間違いがなければO(オー)を入力
  • 入力後すぐにマウスをぐるぐる動かした(ランダムさを作るために必要な操作らしい)

鍵が生成されたことを確認した

$ gpg --list-secret-keys --keyid-format LONG
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
/Users/shotakaha/.gnupg/pubring.kbx
-----------------------------------
sec   rsa4096/鍵のID 2019-11-08 [SC]
uid                 [ultimate] 名前 (コメント) <メールアドレス>
ssb   rsa4096/鍵のID 2019-11-08 [E]
  • 最初にはなかったアイテムがあることを確認した
  • 鍵のIDと書いた部分の文字列をこのあと使うのでコピペした

GitHubにGPG鍵を登録した

Adding a new GPG key to GitHub account

$ gpg --armor --export 鍵のID
-----BEGIN PGP PUBLIC KEY BLOCK-----
鍵の中身 鍵の中身 鍵の中身 鍵の中身 鍵の中身
鍵の中身 鍵の中身 鍵の中身 鍵の中身 鍵の中身
-----END PGP PUBLIC KEY BLOCK-----
  • オプションの役割(よく分かっていない)
    • -a, --armor : create ascii armored output
    • --export : export keys
  • ターミナルに表示された中身を選択して、GitHub設定の該当箇所にコピペした

まとめ

  • GPG鍵の作成はとても簡単にできた
  • 鍵生成はWe need to generate a lot of random bytes...の説明を読んでいる間に終わってしまうので、マウスぐるぐるの準備はしておいたほうがいい

おまけ:GPG鍵の削除

$ gpg --delete-secret-keys 鍵のID
$ gpg --delete-keys 鍵のID
  • 秘密鍵の削除 → 公開鍵の削除 の順番で行う
  • 公開鍵の削除を先にやると怒られた
  • --delete-secret-and-public-keyというオプションもある
0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4