#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# GNU GENERAL PUBLIC LICENSE
# Version 2, June 1991
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
import sys
import os
import re
import getopt
import glob
import base64
import codecs
import hashlib
def file_get_contents(fn):
f = open(fn, encoding='UTF-8')
d = f.read()
f.close()
return d
def bin2hex(raw):
hexval = codecs.encode(raw, 'hex')
l = []
for i in range(0, len(hexval), 2):
l.append(hexval[i:i+2].decode('UTF-8'))
return ':'.join(l)
def bin2b64(raw):
s = base64.b64encode(raw)
s = s.decode('UTF-8').replace('=','')
return s
def parse_pubkey(raw):
l = []
n = len(raw)
offs = 0
while offs < n:
nr_chars = (raw[offs+0] << 24) | \
(raw[offs+1] << 16) | \
(raw[offs+2] << 8) | \
raw[offs+3]
offs = offs + 4
l.append(raw[offs:offs+nr_chars])
offs = offs + nr_chars
return l
if __name__ == '__main__':
fn = sys.argv[1]
text = file_get_contents(fn)
records = re.split(r'\s+', text.splitlines()[0])
if records[0] != 'ssh-rsa':
print('Error: not openssh public key, which startswith "ssh-key"')
print(text)
sys.exit(-1)
pubkey = base64.b64decode(records[1])
l = parse_pubkey(pubkey)
bits = (len(l[2]) - 1) * 8
print('# %s:' % fn)
print('string:', l[0])
print('e(hex):', bin2hex(l[1]))
print('n(hex):', bin2hex(l[2]))
print('$ ssh-keygen -l -E md5 -f %s' % fn)
print('$ head -n 1 %s | gawk "{print $2}" | base64 -d | md5sum' % fn)
print('%d MD5:%s' % (bits, bin2hex(hashlib.md5(pubkey).digest())))
print('')
print('$ ssh-keygen -l -E sha1 -f %s' % fn)
print('$ head -n 1 %s | gawk "{print $2}" | base64 -d | sha1sum' % fn)
print('%d SHA1:%s' % (bits, bin2b64(hashlib.sha1(pubkey).digest())))
print('')
print('$ ssh-keygen -l -E sha256 -f %s' % fn)
print('$ head -n 1 %s | gawk "{print $2}" | base64 -d | sha256sum' % fn)
print('%d SHA256:%s' % (bits, bin2b64(hashlib.sha256(pubkey).digest())))
print('')