Skip to content

Instantly share code, notes, and snippets.

@EntranceJew
Created September 1, 2017 19:12
Show Gist options
  • Save EntranceJew/7f0e0e98ff69d16ff482890326979902 to your computer and use it in GitHub Desktop.
Save EntranceJew/7f0e0e98ff69d16ff482890326979902 to your computer and use it in GitHub Desktop.
bash script to convert a .zip to a chrome .crx w/o building the zip
#!/bin/bash -e
#
# Purpose: Convert a Chromium extension zip into crx format
# Source: https://developer.chrome.com/extensions/crx
if test $# -ne 2; then
echo "Usage: zip2crx.sh <zip file> <pem path>"
exit 1
fi
zip=$1
key=$2
name=$(basename "$zip" .zip)
dir=$(dirname "$zip")
crx="$name.crx"
pub="$name.pub"
sig="$name.sig"
trap 'rm -f "$pub" "$sig"' EXIT
# zip up the crx dir
cwd=$(pwd -P)
cd "$dir"
# signature
openssl sha1 -sha1 -binary -sign "$key" < "$zip" > "$sig"
# public key
openssl rsa -pubout -outform DER < "$key" > "$pub" 2>/dev/null
byte_swap () {
# Take "abcdefgh" and return it as "ghefcdab"
echo "${1:6:2}${1:4:2}${1:2:2}${1:0:2}"
}
crmagic_hex="4372 3234" # Cr24
version_hex="0200 0000" # 2
pub_len_hex=$(byte_swap $(printf '%08x\n' $(ls -l "$pub" | awk '{print $5}')))
sig_len_hex=$(byte_swap $(printf '%08x\n' $(ls -l "$sig" | awk '{print $5}')))
(
echo "$crmagic_hex $version_hex $pub_len_hex $sig_len_hex" | xxd -r -p
cat "$pub" "$sig" "$zip"
) > "$crx"
echo "Wrote $crx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment