GcaptchaZ

GcaptchaZ Git Source Tree

Root/generate_captcha.sh

1#!/bin/bash
2
3#
4# Simple script to generate a captcha and rename it with
5# the captcha's value encrypted (AES 128) with a md5 of the AES key.
6# This allow to retrieve captcha's value only with its name
7# if you have the right AES key and also to use a passphrase.
8#
9# The script also contains a visual verification to avoid
10# generating hard to resolve captchas.
11#
12# Parameters :
13# generate_captcha.sh OUT_DIR PASSPHRASE
14#
15
16# Get parameters
17out=$1
18pass=$2
19do_check=1
20
21# Verify parameters
22[ -z "$out" ] && out="."
23[ -z "$pass" ] && pass="AAA"
24[ ! -e $out ] && mkdir $out
25
26# Generate captcha
27res=`./gcaptchaz -f ./scrawl.ttf -o "$out/captcha.png"`
28echo $res | grep "Captcha generated" > /dev/null
29if [ ! $? -eq 0 ] ; then
30 echo $res
31 exit 1
32fi
33
34text=`echo $res | cut -d\" -f2`
35
36# Do verification
37if [ $do_check -eq 1 ] ; then
38 emacs "$out/captcha.png"
39
40 echo -n "Enter CAPTCHA value : "
41 read user
42
43 if [ "$user" != "$text" ] ; then
44echo "CAPTCHA failed"
45rm -f "$out/captcha.png"
46exit 1
47 fi
48
49 echo "CAPTCHA OK"
50fi
51
52# Encrypt captcha's value to generate captcha's name
53key=`echo -n $pass | md5sum | cut -d' ' -f1`
54enc=`echo -n $text | openssl enc -aes-128-ecb -nosalt -K $key -iv $key`
55enc=`echo -n $enc | hexdump -e '"%02x"'`
56mv "$out/captcha.png" "$out/$enc.png"

Archive Download this file

Branches