GcaptchaZ/generate_captcha.sh

57 lines
1.3 KiB
Bash
Raw Normal View History

2012-11-08 19:18:12 +01:00
#!/bin/bash
#
# Simple script to generate a captcha and rename it with
# the captcha's value encrypted (AES 128) with a md5 of the AES key.
# This allow to retrieve captcha's value only with its name
# if you have the right AES key and also to use a passphrase.
#
# The script also contains a visual verification to avoid
# generating hard to resolve captchas.
#
# Parameters :
# generate_captcha.sh OUT_DIR PASSPHRASE
#
# Get parameters
out=$1
pass=$2
do_check=1
# Verify parameters
[ -z "$out" ] && out="."
[ -z "$pass" ] && pass="AAA"
[ ! -e $out ] && mkdir $out
# Generate captcha
res=`./gcaptchaz -f ./scrawl.ttf -o "$out/captcha.png"`
echo $res | grep "Captcha generated" > /dev/null
if [ ! $? -eq 0 ] ; then
echo $res
exit 1
fi
text=`echo $res | cut -d\" -f2`
# Do verification
if [ $do_check -eq 1 ] ; then
emacs "$out/captcha.png"
echo -n "Enter CAPTCHA value : "
read user
if [ "$user" != "$text" ] ; then
echo "CAPTCHA failed"
rm -f "$out/captcha.png"
exit 1
fi
echo "CAPTCHA OK"
fi
# Encrypt captcha's value to generate captcha's name
key=`echo -n $pass | md5sum | cut -d' ' -f1`
enc=`echo -n $text | openssl enc -aes-128-ecb -nosalt -K $key -iv $key`
enc=`echo -n $enc | hexdump -e '"%02x"'`
mv "$out/captcha.png" "$out/$enc.png"