OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu> |
| 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at |
| 8 # |
| 9 # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. |
| 16 |
| 17 """Utility functions.""" |
| 18 |
| 19 from __future__ import with_statement, print_function |
| 20 |
| 21 import sys |
| 22 from optparse import OptionParser |
| 23 |
| 24 import rsa.key |
| 25 |
| 26 |
| 27 def private_to_public(): |
| 28 """Reads a private key and outputs the corresponding public key.""" |
| 29 |
| 30 # Parse the CLI options |
| 31 parser = OptionParser(usage='usage: %prog [options]', |
| 32 description='Reads a private key and outputs the ' |
| 33 'corresponding public key. Both private an
d public keys use ' |
| 34 'the format described in PKCS#1 v1.5') |
| 35 |
| 36 parser.add_option('-i', '--input', dest='infilename', type='string', |
| 37 help='Input filename. Reads from stdin if not specified') |
| 38 parser.add_option('-o', '--output', dest='outfilename', type='string', |
| 39 help='Output filename. Writes to stdout of not specified') |
| 40 |
| 41 parser.add_option('--inform', dest='inform', |
| 42 help='key format of input - default PEM', |
| 43 choices=('PEM', 'DER'), default='PEM') |
| 44 |
| 45 parser.add_option('--outform', dest='outform', |
| 46 help='key format of output - default PEM', |
| 47 choices=('PEM', 'DER'), default='PEM') |
| 48 |
| 49 (cli, cli_args) = parser.parse_args(sys.argv) |
| 50 |
| 51 # Read the input data |
| 52 if cli.infilename: |
| 53 print('Reading private key from %s in %s format' % |
| 54 (cli.infilename, cli.inform), file=sys.stderr) |
| 55 with open(cli.infilename, 'rb') as infile: |
| 56 in_data = infile.read() |
| 57 else: |
| 58 print('Reading private key from stdin in %s format' % cli.inform, |
| 59 file=sys.stderr) |
| 60 in_data = sys.stdin.read().encode('ascii') |
| 61 |
| 62 assert type(in_data) == bytes, type(in_data) |
| 63 |
| 64 # Take the public fields and create a public key |
| 65 priv_key = rsa.key.PrivateKey.load_pkcs1(in_data, cli.inform) |
| 66 pub_key = rsa.key.PublicKey(priv_key.n, priv_key.e) |
| 67 |
| 68 # Save to the output file |
| 69 out_data = pub_key.save_pkcs1(cli.outform) |
| 70 |
| 71 if cli.outfilename: |
| 72 print('Writing public key to %s in %s format' % |
| 73 (cli.outfilename, cli.outform), file=sys.stderr) |
| 74 with open(cli.outfilename, 'wb') as outfile: |
| 75 outfile.write(out_data) |
| 76 else: |
| 77 print('Writing public key to stdout in %s format' % cli.outform, |
| 78 file=sys.stderr) |
| 79 sys.stdout.write(out_data.decode('ascii')) |
OLD | NEW |