Skip to content

Instantly share code, notes, and snippets.

@LoveDuckie
Created June 23, 2023 10: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 LoveDuckie/f1798160c01e3184b29558a2320d3c15 to your computer and use it in GitHub Desktop.
Save LoveDuckie/f1798160c01e3184b29558a2320d3c15 to your computer and use it in GitHub Desktop.
A simple shell script function for retrieving the fingerprint of a certificate. Useful for determining whether a certificate is already present in a keychain on macOS.
write_error() {
MSG=$2
echo -e "\033[1;31m$1\033[0m \033[0;37m${MSG}\033[0m" 1>&2
return 0
}
get_certificate_fingerprint() {
if [ -z "$1" ]; then
write_error "shared_functions" "The certificate file path was not defined."
return 1
fi
if [ ! -e "$1" ]; then
write_error "shared_functions" "The certificate could not be found or does not exist. (\"$1\")"
return 2
fi
# echo $(openssl x509 -inform der -in $1 | openssl dgst -sha256 | awk '{print toupper($0)}')
echo $(openssl x509 -inform der -in $1 -sha256 -fingerprint -noout | sed -e 's/SHA256 Fingerprint=//' | tr -d ':' | xargs)
return 0
}
get_certificate_fingerprint certificate_goes_here.cer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment