summaryrefslogtreecommitdiffstats
path: root/crxid
blob: e3f7c1b0755ed6fd5980f2bc0f13331135155826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#! /bin/sh
# Get CRX id from RSA key or CRX3 file
# usage: crxid FILE

set -efu

path=$1
type=$(file -b "$path")

case $type in
  'Google Chrome extension, version 3')
    # For details about CRX3 format see
    # https://chromium.googlesource.com/chromium/src/+/HEAD/components/crx_file/crx3.proto
    header_length=$(od -An -j8 -N4 -tu4 "$path")
    binary_crx_id_length=16
    binary_crx_id_offset=$(expr 12 + $header_length - $binary_crx_id_length)
    crx_id_hexstring=$(
      od -An -j"$binary_crx_id_offset" -N"$binary_crx_id_length" -tx1 "$path" |
      tr a-f A-F |
      tr -d ' '
    )
  ;;
  'PEM RSA private key')
    digest=$(
      openssl rsa  -in "$path" -pubout -outform der 2>/dev/null |
      openssl dgst -sha256 -hex |
      sed 's/^(stdin)= //;s/.*/\U&/'
    )
    crx_id_hexstring=$(
      echo "$digest" | cut -b-32
    )
  ;;
  *)
    echo "crxid: error: $path has unsupported type: $type" >&2
    exit 1
esac

script="
  obase=16;
  ibase=16;
  ascii_a=61;
  $(echo $crx_id_hexstring | sed 's/./ascii_a + &;/g')
"

crx_id=$(echo "$script" | bc | xxd -r -p)

echo "$crx_id"