#!/usr/bin/env sh
# A secure way to create SSHFP records (local) | Danny Fullerton - mantor.org | license: public domain
# Modified by Veselin Kolev <vlk@lcpe.uni-sofia.bg> - 20191229
if [ $# -ne 1 ] || [ "$1" = "-h" ]; then
  printf "Usage:\t$0 <hostname>\n\t$0 host.uni-sofia.bg.\n" >&2
  exit 1
fi
name=$1
cipher1='rsa'; cipher2='dsa'; cipher3='ecdsa'; cipher4='ed25519'
hash2='sha256'
for i in 3 4; do # cipher
  for j in 2; do # hash
    eval "key_path=/etc/ssh/ssh_host_\${cipher${i}}_key.pub"
    if [ -f $key_path ]; then
      eval "h=\${hash${j}}"
      sshfp=`awk '{print $2}' $key_path | openssl base64 -d -A | openssl dgst -${h} | sed 's/^.* //' | awk '{print $1}' | tr '[a-z]' '[A-Z]'`
      printf "${name} IN SSHFP ${i} ${j} ${sshfp}\n"
    fi
  done
done