Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sign a hex string using a 32 byte hex string with secp256k1
#!/bin/bash
## Command Line parsing
#######################
if [[ $# -lt 2 ]]; then
echo "Usage: $ ec_sign_hex <input-hex> <priv-key-hex>"
exit 1
fi
inputHex=$1
privKeyHex=$2
## Create .pem and .pub files
#############################
pubKeyHex="$(openssl ec -inform DER -text -noout -in <(cat <(echo -n "302e0201010420") <(echo -n "${privKeyHex}") <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null | tail -6 | head -5 | sed 's/[ :]//g' | tr -d '\n')"
asnFormatKey="30740201010420${privKeyHex}a00706052b8104000aa144034200${pubKeyHex}"
echo "-----BEGIN EC PRIVATE KEY-----" > tmp.pem
echo $asnFormatKey | xxd -r -p | base64 | fold -w 64 >> tmp.pem
echo "-----END EC PRIVATE KEY-----" >> tmp.pem
openssl ec -in tmp.pem -pubout -out tmpPub.pem &>/dev/null
## Sign message
# sign:
openssl pkeyutl -sign -inkey tmp.pem -in <(printf $inputHex | xxd -r -p) -out tmp.sig
echo "Signature"
echo "####################"
echo ""
openssl pkeyutl -sign -inkey tmp.pem -in <(printf $inputHex | xxd -r -p) | xxd -p #-hexdump #| xxd -p
echo ""
echo "####################"
# verify:
openssl pkeyutl -verify -pubin -inkey tmpPub.pem -sigfile tmp.sig -in <(printf $inputHex | xxd -r -p)
rm tmp.pem tmpPub.pem tmp.sig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.