Skip to content

Instantly share code, notes, and snippets.

@MuntashirAkon
Created May 5, 2022 16:29
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 MuntashirAkon/36d6e1ca3b8e105828796a565b30000b to your computer and use it in GitHub Desktop.
Save MuntashirAkon/36d6e1ca3b8e105828796a565b30000b to your computer and use it in GitHub Desktop.
Get Base64 encoded SHA-256 checksum of a public key from a DER encoded certificate. This is useful for creating certificate pinning on Android. See https://developer.android.com/training/articles/security-config#CertificatePinning
#!/usr/bin/env bash
if [[ "$(uname)" == "Darwin" ]]; then
alias sha256sum="shasum -a 256"
fi
function extract_pubkey_hash() {
openssl x509 -inform der -in "$1" -pubkey -noout | sed -e 's/-----BEGIN PUBLIC KEY-----//' -e 's/-----END PUBLIC KEY-----//' | tr '\n' ',' | sed s/,//g | base64 -d | sha256sum - | awk '{ print $1 }' | xxd -r -p | base64
}
extract_pubkey_hash "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment