Skip to content

Instantly share code, notes, and snippets.

@sukso96100
Last active July 24, 2018 05:23
Show Gist options
  • Save sukso96100/a0191bb6c170dbd08afcd977ddd1c049 to your computer and use it in GitHub Desktop.
Save sukso96100/a0191bb6c170dbd08afcd977ddd1c049 to your computer and use it in GitHub Desktop.
GPG 공개키 임포터 스크립트. - CSV 파일에서 서명된 공개키 블록 로드하여 검증 후 임포트 처리
"""
The MIT License (MIT)
Copyright 2018-Present Youngbin Han<[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import csv
import sys
import subprocess
print("=-=-=-=-=-=")
print('Clearsigned Public GPG Key CSV Sheet importer for Keysigning Party')
print('Written by Youngbin Han<[email protected]>')
print("=-=-=-=-=-=")
if(len(sys.argv)<4):
print("""
사용 방법:
python3 importer.py (공개키_목록_파일.csv) (공개키가 있는 열) (공개키 보관할 Keyring 이름)
예: python3 importer.py publickeys.csv 1 ksprings.gpg
GPG 명령행 프로그램이 사전에 설치되어 있어야 본 스크립트를 정상적으로 사용 가능합니다.
Usage:
Python3 importer.py (list_of_public_keys.csv) (column_of_public_keys) (name_of_keyring_for_storing)
Example: python3 importer.py publickeys.csv 1 ksprings.gpg
Installation of GPG CLI Program is required to use this script properly.
""")
with open(sys.argv[1], newline='') as csvfile:
keyring_result = subprocess.run(['gpg', '--no-default-keyring','--keyring', str(sys.argv[3]), '--fingerprint'])
print(keyring_result.stdout)
print(keyring_result.stderr)
kspreader = csv.reader(csvfile)
for index, row in enumerate(kspreader):
print("======PROCESSING ITEM {}>=>=>=>".format(index))
item = row[int(sys.argv[2])]
done = subprocess.run(['gpg', '--verify'], input=item, encoding='utf8', capture_output=True)
print(done.stderr)
signcount = len(done.stderr.split("Signature made"))
goodcount = len(done.stderr.split("Good signature from"))
if(signcount>1 and goodcount>1 and signcount==goodcount):
print('SIGNATURE VERIFIED!')
if '- -----BEGIN PGP PUBLIC KEY BLOCK-----' in item:
sign_and_body = item.split('- -----BEGIN PGP PUBLIC KEY BLOCK-----')[1]
body_only = sign_and_body.split('-----BEGIN PGP SIGNATURE-----')[0]
publickey = '- -----BEGIN PGP PUBLIC KEY BLOCK-----'.join(['', body_only])
# print(publickey)
result = subprocess.run(['gpg','--no-default-keyring','--keyring',
sys.argv[3], '--import'], input=publickey.replace('- ',''), encoding='utf8')
print(result.stdout)
print(result.stderr)
else:
print('SIGNATURE VERIFICATION FAILED!')
print(">=>=>=>ITEM {} PROCESSED======".format(index))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment