Skip to content

Instantly share code, notes, and snippets.

@ryancdotorg
Created February 29, 2024 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryancdotorg/3be94feb58067b27f4326e27b72d351a to your computer and use it in GitHub Desktop.
Save ryancdotorg/3be94feb58067b27f4326e27b72d351a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import ssl
from hashlib import sha1
# need recent pycryptodome
try:
from Crypto.PublicKey import RSA
except ModuleNotFoundError:
from Cryptodome.PublicKey import RSA
def to_bytes(n, length=None):
if length is None:
length = (n.bit_length() + 7) // 8
return n.to_bytes(length, 'big')
def fmt_fingerprint(hexdigest):
h = hexdigest.upper()
return ' '.join(h[i:i+2] for i in range(0, len(h), 2))
if __name__ == '__main__':
source = sys.argv[1]
if ':' in source:
parts = source.split(':')
hostname = parts[0]
port = int(parts[1])
target = RSA.importKey(ssl.get_server_certificate((hostname, port)))
else:
with open(source, 'rb') as pem:
target = RSA.importKey(pem.read())
e_bytes = to_bytes(target.e)
e_len = to_bytes(len(e_bytes), 4)
n_bytes = to_bytes(target.n)
n_len = to_bytes(len(n_bytes), 4)
fp_old = sha1(b'ssh-rsa'+e_bytes+n_bytes).hexdigest()
fp_new = sha1(b'\0\0\0\7ssh-rsa'+e_len+e_bytes+n_len+n_bytes).hexdigest()
print('Tasmota fingerprint (old): ' + fmt_fingerprint(fp_old))
print('Tasmota fingerprint (new): ' + fmt_fingerprint(fp_new))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment