a little nicer wrt pep8
[sfa.git] / keyconvert / keyconvert.py
index de904ee..f8e9589 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 import sys
 import base64
@@ -12,7 +12,8 @@ class RSA_pub_fix(RSA.RSA_pub):
     def save_key_bio(self, bio, *args, **kw):
         return self.save_pub_key_bio(bio)
 
-def rsa_new_pub_key((e, n)):
+def rsa_new_pub_key(couple):
+    (e, n)=couple
     rsa = m2.rsa_new()
     m2.rsa_set_e(rsa, e)
     m2.rsa_set_n(rsa, n)
@@ -34,7 +35,7 @@ def decode_key(fname):
             continue
         elif in_key:
             return base64.b64decode(f)
-        
+
     return None
 
 
@@ -42,8 +43,8 @@ def decode_key(fname):
 #
 # a section:
 # length = 4 bytes (32-bit big-endian integer)
-# data = length bytes of string 
-# 
+# data = length bytes of string
+#
 # sections of the key ( for RSA )
 # [key-type (in ASCII)] [public exponent (bignum)] [primes (bignum)]
 #
@@ -52,12 +53,12 @@ def decode_key(fname):
 #
 # - baris
 def read_key(key):
-    
+
     def read_length(key):
         length = key[0:4]
         length = struct.unpack(">l", length)[0]
         return length, key
-        
+
     def read_values(key, count):
         v = []
         for i in range(count):
@@ -72,13 +73,13 @@ def read_key(key):
     key_type = key[:length]
     key = key[length:]
 
-    if key_type == "ssh-rsa":
+    if key_type == b"ssh-rsa":
         # prepare parameters for RSA.new_pub_key
         v = read_values(key, 2)
         e, n = v[0], v[1]
         return key_type, e, n
 
-    elif key_type == "ssh-dss":
+    elif key_type == b"ssh-dss":
         # prepare parameters for DSA.set_params
         v = read_values(key, 4)
         p, q, g, y = v[0], v[1], v[2], v[3]
@@ -90,24 +91,24 @@ def convert(fin, fout):
     ret = read_key(key)
     key_type = ret[0]
 
-    if key_type == "ssh-rsa":
+    if key_type == b"ssh-rsa":
         e, n = ret[1:]
         rsa = rsa_new_pub_key((e, n))
         rsa.save_pem(fout)
 
-    elif key_type == "ssh-dss":
+    elif key_type == b"ssh-dss":
         p, q, g, y = ret[1:]
         dsa = DSA.set_params(p, q, g)
         dsa.gen_key()
         dsa.save_pub_key(fout)
         # FIXME: This is wrong.
         # M2Crypto doesn't allow us to set the public key parameter
-        raise(Exception, "DSA keys are not supported yet: M2Crypto doesn't allow us to set the public key parameter")
+        raise Exception("DSA keys are not supported yet: M2Crypto doesn't allow us to set the public key parameter")
 
 
 if __name__ == "__main__":
     if len(sys.argv) != 3:
-        print "Usage: %s <input-file> <output-file>"
+        print("Usage: %s <input-file> <output-file>")
         sys.exit(1)
 
     fin = sys.argv[1]