Skip to content

Instantly share code, notes, and snippets.

@dale-c-anderson
Forked from SharkyRawr/authkeys_to_sha256.sh
Last active June 7, 2024 17:23
Show Gist options
  • Save dale-c-anderson/eeeb7967141680ed4a4e243c70303010 to your computer and use it in GitHub Desktop.
Save dale-c-anderson/eeeb7967141680ed4a4e243c70303010 to your computer and use it in GitHub Desktop.
Convert OpenSSH authorized_keys entries in to sha256 sums used in /var/log/auth.log
#!/bin/bash
set -eu -o pipefail
function main() {
AUTHORIZED_KEYS=${1:-~/.ssh/authorized_keys}
while read -r p; do
echo -e "\n$p ->"
echo "$p" | awk '{ print $2 }' | # Only the actual key data without prefix or comments
base64 -d | # decode as base64
sha256sum | # SHA256 hash (returns hex)
awk '{ print $1 }' | # only the hex data
xxd -r -p | # hex to bytes
base64 # encode as base64
done <<< "$(grep -v '^#' "$AUTHORIZED_KEYS")"
}
main "$@"
# thanks to https://serverfault.com/a/888300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment