Created
May 5, 2022 16:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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