add tool/get_raw_public_key.py
authorNIIBE Yutaka <gniibe@fsij.org>
Wed, 31 Oct 2012 07:23:19 +0000 (16:23 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Wed, 31 Oct 2012 07:23:19 +0000 (16:23 +0900)
ChangeLog
tool/get_raw_public_key.py [new file with mode: 0755]

index 2dde9bf..cbd7d26 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2012-10-31  Niibe Yutaka  <gniibe@fsij.org>
+
+       * tool/get_raw_public_key.py: New.
+
 2012-10-26  Niibe Yutaka  <gniibe@fsij.org>
 
        * GNUK_USB_DEVICE_ID (Product_STRING): It's considered better not
diff --git a/tool/get_raw_public_key.py b/tool/get_raw_public_key.py
new file mode 100755 (executable)
index 0000000..fb1272f
--- /dev/null
@@ -0,0 +1,32 @@
+#! /usr/bin/python
+
+import sys, binascii
+from subprocess import check_output
+
+def get_gpg_public_key(keygrip):
+    result = check_output(["gpg-connect-agent", "READKEY %s" % keygrip, "/bye"])
+    key = ""
+    while True:
+        i = result.find('%')
+        if i < 0:
+            key += result
+            break
+        hex_str = result[i+1:i+3]
+        key += result[0:i]
+        key += chr(int(hex_str,16))
+        result = result[i+3:]
+
+    pos = key.index("D (10:public-key(3:rsa(1:n257:") + 31 # skip NUL too
+    pos_last = key.index(")(1:e3:")
+    key = key[pos:pos_last]
+    if len(key) != 256:
+        raise ValueError, binascii.hexlify(key)
+    return key
+
+if __name__ == '__main__':
+    keygrip = sys.argv[1]
+    k = get_gpg_public_key(keygrip)
+    shorthand = keygrip[0:8] + ".bin"
+    f = open(shorthand,"w")
+    f.write(k)
+    f.close()