From 4cf0b24390e699b5b36976f68522e9757e414a05 Mon Sep 17 00:00:00 2001 From: "Sybren A. St?vel" Date: Sat, 22 Feb 2014 11:12:09 +0100 Subject: Fixed compatibilty of pyrsa-priv2pub with Python 3 --- rsa/util.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/rsa/util.py b/rsa/util.py index 307bda5..5bbb70b 100644 --- a/rsa/util.py +++ b/rsa/util.py @@ -16,7 +16,7 @@ '''Utility functions.''' -from __future__ import with_statement +from __future__ import with_statement, print_function import sys from optparse import OptionParser @@ -49,14 +49,16 @@ def private_to_public(): # Read the input data if cli.infilename: - print >>sys.stderr, 'Reading private key from %s in %s format' % \ - (cli.infilename, cli.inform) - with open(cli.infilename) as infile: + print('Reading private key from %s in %s format' % \ + (cli.infilename, cli.inform), file=sys.stderr) + with open(cli.infilename, 'rb') as infile: in_data = infile.read() else: - print >>sys.stderr, 'Reading private key from stdin in %s format' % \ - cli.inform - in_data = sys.stdin.read() + print('Reading private key from stdin in %s format' % cli.inform, + file=sys.stderr) + in_data = sys.stdin.read().encode('ascii') + + assert type(in_data) == bytes, type(in_data) # Take the public fields and create a public key @@ -67,13 +69,13 @@ def private_to_public(): out_data = pub_key.save_pkcs1(cli.outform) if cli.outfilename: - print >>sys.stderr, 'Writing public key to %s in %s format' % \ - (cli.outfilename, cli.outform) - with open(cli.outfilename, 'w') as outfile: + print('Writing public key to %s in %s format' % \ + (cli.outfilename, cli.outform), file=sys.stderr) + with open(cli.outfilename, 'wb') as outfile: outfile.write(out_data) else: - print >>sys.stderr, 'Writing public key to stdout in %s format' % \ - cli.outform - sys.stdout.write(out_data) + print('Writing public key to stdout in %s format' % cli.outform, + file=sys.stderr) + sys.stdout.write(out_data.decode('ascii')) -- cgit v1.2.1