GnuPG チートシート

最終更新: 2023-08-31 20:10:33 +0800 +0800
tags: Gpg | Security
原文は英語で書かれています。あなたはAI翻訳版を読んでいます。

gpg コマンドには、いくつかの異なる役割がある。

  • OpenPGP 鍵を管理し、受信者向けにデータを暗号化する
  • 鍵の所有者向けにデータを復号する
  • 署名を作成または検証する

GPG の最も一般的な用途には、ソフトウェアリリースに付属する .asc ファイルを開発者の公開鍵で検証することや、 秘密鍵で Git のコミットとタグに署名することが含まれる。

インストール #

sh
# archlinux
# gnupg and pinentry should already be installed on your system as it is a dependency of pacman.

# debian/ubuntu
sudo apt install gnupg pinentry-curses pinentry-gtk-2

# redhat/fedora/centos
sudo dnf install gnupg2 pinentry  # fedora/rhel 8+
sudo yum install gnupg2 pinentry  # rhel 7

# mac
brew install gnupg pinentry-mac

GPG は、秘密鍵をアンロックする際にパスフレーズの入力を求めるため、pinentry というプログラムを使用する。設計上の原則は分離である。pinentry は独立したプロセスとして動作するため、復号を要求するアプリケーションがパスフレーズを見ることはなく、GPG エージェントだけがパスフレーズを受け取る。これにより、ユーザーと暗号処理の間に信頼できる経路が確立される。pinentry-curses(CLI)、pinentry-gtk(GTK)、pinentry-qt(Qt)、pinentry-mac(macOS ネイティブ)など、複数の種類が存在する。使用するプログラムは、~/.gnupg/gpg-agent.conf の pinentry-program ディレクティブで設定できる。

鍵の管理 #

「Armor」とは、コピー&ペーストしやすくするためにバイナリデータを Base64 形式にしたものを意味する。

sh
# Generate a new key 
gpg --full-generate-key

# import a public key so GPG knows who he is
gpg --import public-or-private-key.asc

# export a public key
gpg --export you@example.com            # (binary key to stdout)
gpg --export --armor you@example.com    # (ASCII armor to be copied around)

# Export a secret key (you know the danger)
gpg --export-secret-keys you@example.com            # (binary key to stdout)
gpg --armor --export-secret-keys you@example.com    # (ASCII armor to be copied around)

# List locals keys
gpg --list-keys                             # public
gpg --list-secret-keys --keyid-format=long  # public-secret key pair

# Edit key metadata, expiration, trust, or subkeys
gpg --edit-key alice@example.com

# Delete a public key from your local keyring
gpg --delete-key alice@example.com

# When removing one of your own identities,
# delete the secret key first, then the public key
gpg --delete-secret-key you@example.com
gpg --delete-key you@example.com

# Show the fingerprint of a key
# the short key ID is not enough for identity verification:
gpg --fingerprint alice@example.com

暗号化と復号 #

sh
# Encrypt a file for one recipient
# writes secret.txt.gpg by default
gpg --encrypt --recipient alice@example.com 

# Encrypt a file for multiple recipients
# every intended reader must be listed
gpg --encrypt \
  --recipient alice@example.com \
  --recipient bob@example.com \
  report.pdf

# Decrypt a file to stdout
gpg --decrypt secret.txt.gpg

# Decrypt to a specific output path
gpg --output secret.txt --decrypt secret.txt.gpg

署名と検証 #

sh
# Create a detached signature
# use this when the receiver already has the original file
# writes release.tar.gz.asc
gpg --detach-sign --armor release.tar.gz

# Verify a detached signature
gpg --verify release.tar.gz.asc release.tar.gz

# Create a cleartext signature
# useful for text that should remain human-readable
gpg --clearsign notes.txt

# Verify a clearsigned file
gpg --verify notes.txt.asc

その他のメンテナンスコマンド #

sh

# Reload the agent after changing configuration
gpgconf --kill gpg-agent

標準入力および標準出力との併用 #

sh
# Encrypt piped data and emit ASCII armor
printf '%s\n' 'hello' | gpg --armor --encrypt --recipient alice@example.com

# Sign piped data without creating an input file
printf '%s\n' 'release approved' | gpg --clearsign

コメント

GitHub で閲覧・コメントする(投稿にはログインが必要です)