# server address (todo: add an option to set it on command line)
server=127.0.0.1/stockmarket-server.cgi
# private key
# you can generate one with: openssl genrsa -out rsa-test 2048
# public key can be obtained with: openssl rsa -pubout -in rsa-test
privkey="rsa-test"
# message is given on command line
message="$@"
# Message authentication code is a random number appended to unix EPOC
mac=$(bc <<<"$(date +%s)+$RANDOM")
# base64 JSON data
data="$(base64 -w 0 <<<"{ \"message\": \"$message\", \"mac\": $mac }")"
# base64 whirlpool signature
signature="$(openssl dgst -whirlpool -sign $privkey <<< "$data" |base64 -w 0)"
# sending via POST method
wget -q -O - --post-data "data=\"$data\" signature=\"$signature\" pubkey=\"$(openssl rsa -pubout -in $privkey)\"" http://$server
And here is the code for the server (a CGI script):
echo "Content-type: text/plain"
echo
echo stock market exchange server
echo
if [[ "$REQUEST_METHOD" = "POST" ]] && [[ "$CONTENT_LENGTH" -gt 0 ]]
then
read -N $CONTENT_LENGTH POST_DATA <&0
# todo: retrieve post data more securely than using "eval"
eval "$POST_DATA"
if openssl dgst -whirlpool -signature <(base64 -d <<<"$signature") -verify <(echo "$pubkey") <<<"$data" 2>&1
then
data="$(base64 -d <<<"$data")"
echo "$data"
else
echo wrong signature
fi
else
echo no data received
fi
Several aspects of your specification for the server depend on the kind of database you want to use. Would you consider using mongodb?