]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Rsa2048Sha256Sign/Rsa2048Sha256GenerateKeys.py
Revert BaseTools: PYTHON3 migration
[mirror_edk2.git] / BaseTools / Source / Python / Rsa2048Sha256Sign / Rsa2048Sha256GenerateKeys.py
index 7bdc460146f76626560efc8b86cbe876b836e14b..a34dac423b8c1eeb5698917e3e0b9422c12ea9dc 100644 (file)
@@ -22,6 +22,7 @@
 '''\r
 Rsa2048Sha256GenerateKeys\r
 '''\r
+from __future__ import print_function\r
 \r
 import os\r
 import sys\r
@@ -42,9 +43,8 @@ if __name__ == '__main__':
   #\r
   # Create command line argument parser object\r
   #\r
-  parser = argparse.ArgumentParser(prog=__prog__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r
+  parser = argparse.ArgumentParser(prog=__prog__, version=__version__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r
   group = parser.add_mutually_exclusive_group(required=True)\r
-  group.add_argument("--version", action='version', version=__version__)\r
   group.add_argument("-o", "--output", dest='OutputFile', type=argparse.FileType('wb'), metavar='filename', nargs='*', help="specify the output private key filename in PEM format")\r
   group.add_argument("-i", "--input", dest='InputFile', type=argparse.FileType('rb'), metavar='filename', nargs='*', help="specify the input private key filename in PEM format")\r
   parser.add_argument("--public-key-hash", dest='PublicKeyHashFile', type=argparse.FileType('wb'), help="specify the public key hash filename that is SHA 256 hash of 2048 bit RSA public key in binary format")\r
@@ -83,7 +83,7 @@ if __name__ == '__main__':
   if Process.returncode != 0:\r
     print('ERROR: Open SSL command not available.  Please verify PATH or set OPENSSL_PATH')\r
     sys.exit(Process.returncode)\r
-  print(Version[0].decode())\r
+  print(Version[0])\r
 \r
   args.PemFileName = []\r
 \r
@@ -118,19 +118,19 @@ if __name__ == '__main__':
       args.PemFileName.append(Item.name)\r
       Item.close()\r
 \r
-  PublicKeyHash = bytearray()\r
+  PublicKeyHash = ''\r
   for Item in args.PemFileName:\r
     #\r
     # Extract public key from private key into STDOUT\r
     #\r
     Process = subprocess.Popen('%s rsa -in %s -modulus -noout' % (OpenSslCommand, Item), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
-    PublicKeyHexString = Process.communicate()[0].split(b'=')[1].strip()\r
+    PublicKeyHexString = Process.communicate()[0].split('=')[1].strip()\r
     if Process.returncode != 0:\r
       print('ERROR: Unable to extract public key from private key')\r
       sys.exit(Process.returncode)\r
-    PublicKey = bytearray()\r
+    PublicKey = ''\r
     for Index in range (0, len(PublicKeyHexString), 2):\r
-      PublicKey = PublicKey + PublicKeyHexString[Index:Index + 2]\r
+      PublicKey = PublicKey + chr(int(PublicKeyHexString[Index:Index + 2], 16))\r
 \r
     #\r
     # Generate SHA 256 hash of RSA 2048 bit public key into STDOUT\r
@@ -156,14 +156,14 @@ if __name__ == '__main__':
   #\r
   PublicKeyHashC = '{'\r
   for Item in PublicKeyHash:\r
-    PublicKeyHashC = PublicKeyHashC + '0x%02x, ' % (Item)\r
+    PublicKeyHashC = PublicKeyHashC + '0x%02x, ' % (ord(Item))\r
   PublicKeyHashC = PublicKeyHashC[:-2] + '}'\r
 \r
   #\r
   # Write SHA 256 of 2048 bit binary public key to public key hash C structure file\r
   #\r
   try:\r
-    args.PublicKeyHashCFile.write (bytes(PublicKeyHashC))\r
+    args.PublicKeyHashCFile.write (PublicKeyHashC)\r
     args.PublicKeyHashCFile.close ()\r
   except:\r
     pass\r