Skip to content

Instantly share code, notes, and snippets.

@moriyoshi
Last active January 2, 2016 08:39
Show Gist options
  • Save moriyoshi/8278015 to your computer and use it in GitHub Desktop.
Save moriyoshi/8278015 to your computer and use it in GitHub Desktop.
import base64
import struct
import sys
def chunk(data):
return struct.pack('>L', len(data)) + data
data = chunk(b'ssh-rsa') + chunk(b'\x23') + chunk(b'\x00' + open(sys.argv[1]).read())
sys.stdout.write('ssh-rsa %s' % base64.b64encode(data))
# curl https://github.com/{user}.keys | python retrieval.py
import re
import sys
import base64
import struct
def get_chunk(s, i):
l, = struct.unpack('>L', s[i:i + 4])
i += 4
n = i + l
data = s[i:n]
if len(data) != l:
raise Exception('Unexpected EOS')
return data, n
def generate_chunk(s):
i = 0
while i < len(s):
data, i = get_chunk(s, i)
yield data
for lineno, key in enumerate(sys.stdin, 1):
key = key.rstrip()
g = re.match(r'^(?:ssh-rsa|ssh-dsa)\s+(.*)$', key)
if g:
chunks = base64.b64decode(g.group(1))
try:
gen = generate_chunk(chunks)
sig = gen.next()
if sig in (b'ssh-rsa', b'ssh-dsa'):
gen.next()
pix = gen.next()[1:]
format = None
if pix[0:2] == b'\xff\xd8':
format = 'jpg'
elif pix[0:4] == b'\x89PNG':
format = 'png'
elif pix[0:6] == b'GIF89a':
format = 'gif'
if format is not None:
filename = '%08d.%s' % (lineno, format)
print("got a picture! gonna save it as %s" % filename)
open(filename, 'w').write(pix)
continue
except Exception:
pass
print("invalid key at line %d" % lineno)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment