Skip to content

Instantly share code, notes, and snippets.

@paddycarver
Created February 10, 2011 11:26
Show Gist options
  • Save paddycarver/820351 to your computer and use it in GitHub Desktop.
Save paddycarver/820351 to your computer and use it in GitHub Desktop.
A bash script that compiles Google Chrome extension folders into .crx files
#!/bin/bash -e
#
# Purpose: Pack a Chromium extension directory into crx format
if test $# -ne 2; then
echo "Usage: crxmake.sh <extension dir> <pem path>"
exit 1
fi
dir=$1
key=$2
name=$(basename "$dir")
crx="$name.crx"
pub="$name.pub"
sig="$name.sig"
zip="$name.zip"
trap 'rm -f "$pub" "$sig" "$zip"' EXIT
# zip up the crx dir
cwd=$(pwd -P)
(cd "$dir" && zip -qr -9 -X "$cwd/$zip" .)
# 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"
@willdye
Copy link

willdye commented Feb 22, 2012

According to http://mywiki.wooledge.org/ParsingLs and some other sites, it's a bad idea to parse "ls" output in Bash scripts, in part because file names on many systems are allowed to have newlines and other strange characters in them. "stat -c%s" works OK for me in Ubuntu, but the above-referenced page says that "stat" is not very portable, and suggests using "wc" instead. So instead of:

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}')))

...the code might be more stable if it was something like:

pub_len_hex=$(byte_swap $(awk '{printf "%08x\n",$1}' <(wc -c "$pub")))
sig_len_hex=$(byte_swap $(awk '{printf "%08x\n",$1}' <(wc -c "$sig")))

Caveat: I have NOT tested the above example code, so apply grains of salt accordingly.

@Yogeshkarpe1
Copy link

This code worked for me. Thank you!!!

@danikaze
Copy link

This doesn't work anymore.
Apart than having to change the way of getting the file size as @willdye said, Chrome says Package is invalid: 'CRX_HEADER_INVALID' when trying to install it, I don't know why.
Has anyone found a workaround for this?

@pramod0
Copy link

pramod0 commented Sep 30, 2019

is PEM file necessary?

@pramod0
Copy link

pramod0 commented Sep 30, 2019

This doesn't work anymore.
Apart than having to change the way of getting the file size as @willdye said, Chrome says Package is invalid: 'CRX_HEADER_INVALID' when trying to install it, I don't know why.
Has anyone found a workaround for this?

Even I am getting the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment