minisign_key_id.sh (1167B)
1 #!/bin/sh 2 # Decode the stable 8-byte signer identity from a Minisign public or secret 3 # key. Pure POSIX awk keeps release gating independent of host base64 variants. 4 5 set -eu 6 7 [ "$#" -eq 1 ] || { 8 printf 'usage: minisign_key_id.sh KEY\n' >&2 9 exit 2 10 } 11 12 LC_ALL=C awk ' 13 function b64(c) { 14 if (c == "=") return -1 15 return index("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", c) - 1 16 } 17 /^untrusted comment:/ { next } 18 /^[[:space:]]*$/ { next } 19 { 20 s = $0 21 gsub(/[[:space:]]/, "", s) 22 n = 0 23 for (i = 1; i <= length(s); i += 4) { 24 a = b64(substr(s, i, 1)) 25 b = b64(substr(s, i + 1, 1)) 26 c = b64(substr(s, i + 2, 1)) 27 d = b64(substr(s, i + 3, 1)) 28 if (a < 0 || b < 0) exit 1 29 byte[++n] = a * 4 + int(b / 16) 30 if (c >= 0) byte[++n] = (b % 16) * 16 + int(c / 4) 31 if (d >= 0) byte[++n] = (c % 4) * 64 + d 32 } 33 if (byte[1] != 69 || byte[2] != 100) exit 1 34 if (n == 42) 35 off = 3 36 else if (n == 158) 37 off = 55 38 else 39 exit 1 40 found = 1 41 for (i = off; i < off + 8; ++i) printf "%02x", byte[i] 42 printf "\n" 43 exit 44 } 45 END { if (!found) exit 1 } 46 ' "$1"